Sfoglia il codice sorgente

case on gitlab.com vs self hosted gitlab for gitlab-ci file

Mohammed Nafees 4 anni fa
parent
commit
294af665d6

+ 11 - 0
api/server/handlers/project_integration/create_gitlab.go

@@ -1,7 +1,9 @@
 package project_integration
 
 import (
+	"fmt"
 	"net/http"
+	"net/url"
 
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
@@ -35,6 +37,15 @@ func (p *CreateGitlabIntegration) ServeHTTP(w http.ResponseWriter, r *http.Reque
 		return
 	}
 
+	_, err := url.Parse(request.InstanceURL)
+
+	if err != nil {
+		p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
+			fmt.Errorf("malformed gitlab instance URL"), http.StatusBadRequest,
+		))
+		return
+	}
+
 	gitlabIntegration, err := p.Repo().GitlabIntegration().CreateGitlabIntegration(&ints.GitlabIntegration{
 		ProjectID:       project.ID,
 		InstanceURL:     request.InstanceURL,

+ 89 - 29
internal/integrations/ci/gitlab/ci.go

@@ -3,6 +3,7 @@ package gitlab
 import (
 	"fmt"
 	"net/http"
+	"net/url"
 	"strings"
 
 	"github.com/porter-dev/porter/api/server/shared/commonutils"
@@ -32,8 +33,9 @@ type GitlabCI struct {
 	FolderPath       string
 	PorterToken      string
 
-	defaultGitBranch string
-	pID              string
+	defaultGitBranch  string
+	pID               string
+	gitlabInstanceURL string
 }
 
 func (g *GitlabCI) Setup() error {
@@ -346,42 +348,100 @@ func (g *GitlabCI) getClient() (*gitlab.Client, error) {
 		return nil, err
 	}
 
+	g.gitlabInstanceURL = gi.InstanceURL
+
 	return client, nil
 }
 
-func (g *GitlabCI) getCIJob(jobName string) map[string]interface{} {
-	return map[string]interface{}{
-		"image": "docker:latest",
-		"services": []string{
-			"docker:dind",
+func (g *GitlabCI) getCIJob(jobName string) yaml.MapSlice {
+	res := yaml.MapSlice{}
+	url, _ := url.Parse(g.gitlabInstanceURL)
+
+	res = append(res,
+		yaml.MapItem{
+			Key: "rules",
+			Value: []map[string]string{
+				{
+					"if": fmt.Sprintf("$CI_COMMIT_BRANCH == \"%s\" && $CI_PIPELINE_SOURCE == \"push\"", g.GitBranch),
+				},
+			},
 		},
-		"stage":   jobName,
-		"timeout": "20 minutes",
-		"variables": map[string]string{
-			"GIT_STRATEGY": "clone",
+	)
+
+	if url.Hostname() == "gitlab.com" || url.Hostname() == "www.gitlab.com" {
+		res = append(res,
+			yaml.MapItem{
+				Key:   "image",
+				Value: "docker:latest",
+			},
+			yaml.MapItem{
+				Key: "services",
+				Value: []string{
+					"docker:dind",
+				},
+			},
+			yaml.MapItem{
+				Key: "script",
+				Value: []string{
+					fmt.Sprintf(
+						"docker run --rm --workdir=\"/app\" "+
+							"-v /var/run/docker.sock:/var/run/docker.sock "+
+							"-v $(pwd):/app "+
+							"public.ecr.aws/o1j4x7p4/porter-cli:latest "+
+							"update --host \"%s\" --project %d --cluster %d "+
+							"--token \"$%s\" --app \"%s\" "+
+							"--tag \"$(echo $CI_COMMIT_SHA | cut -c1-7)\" --namespace \"%s\" --stream",
+						g.ServerURL, g.ProjectID, g.ClusterID, g.getPorterTokenSecretName(),
+						g.ReleaseName, g.ReleaseNamespace,
+					),
+				},
+			},
+		)
+	} else {
+		res = append(res,
+			yaml.MapItem{
+				Key:   "image",
+				Value: "public.ecr.aws/o1j4x7p4/porter-cli:latest",
+			},
+			yaml.MapItem{
+				Key: "script",
+				Value: []string{
+					fmt.Sprintf(
+						"update --host \"%s\" --project %d --cluster %d "+
+							"--token \"$%s\" --app \"%s\" "+
+							"--tag \"$(echo $CI_COMMIT_SHA | cut -c1-7)\" --namespace \"%s\" --stream",
+						g.ServerURL, g.ProjectID, g.ClusterID, g.getPorterTokenSecretName(),
+						g.ReleaseName, g.ReleaseNamespace,
+					),
+				},
+			},
+		)
+	}
+
+	res = append(res,
+		yaml.MapItem{
+			Key:   "stage",
+			Value: jobName,
 		},
-		"rules": []map[string]string{
-			{
-				"if": fmt.Sprintf("$CI_COMMIT_BRANCH == \"%s\" && $CI_PIPELINE_SOURCE == \"push\"", g.GitBranch),
+		yaml.MapItem{
+			Key: "tags",
+			Value: []string{
+				"porter-runner",
 			},
 		},
-		"script": []string{
-			fmt.Sprintf(
-				"docker run --rm --workdir=\"/app\" "+
-					"-v /var/run/docker.sock:/var/run/docker.sock "+
-					"-v $(pwd):/app "+
-					"public.ecr.aws/o1j4x7p4/porter-cli:latest "+
-					"update --host \"%s\" --project %d --cluster %d "+
-					"--token \"$%s\" --app \"%s\" "+
-					"--tag \"$(echo $CI_COMMIT_SHA | cut -c1-7)\" --namespace \"%s\" --stream",
-				g.ServerURL, g.ProjectID, g.ClusterID, g.getPorterTokenSecretName(),
-				g.ReleaseName, g.ReleaseNamespace,
-			),
+		yaml.MapItem{
+			Key:   "timeout",
+			Value: "20 minutes",
 		},
-		"tags": []string{
-			"porter-runner",
+		yaml.MapItem{
+			Key: "variables",
+			Value: map[string]string{
+				"GIT_STRATEGY": "clone",
+			},
 		},
-	}
+	)
+
+	return res
 }
 
 func (g *GitlabCI) createGitlabSecret(client *gitlab.Client) error {