浏览代码

namespace for deploy branches

Mohammed Nafees 3 年之前
父节点
当前提交
ac6c6c5ca3

+ 7 - 6
api/server/handlers/webhook/github_incoming.go

@@ -390,19 +390,20 @@ func (c *GithubIncomingWebhookHandler) processPushEvent(event *github.PushEvent,
 	client, err := getGithubClientFromEnvironment(c.Config(), env)
 
 	if err != nil {
-		return fmt.Errorf("[webhookID: %s, owner: %s, repo: %s] error creating github client: %w",
-			webhookID, owner, repo, err)
+		return fmt.Errorf("[webhookID: %s, owner: %s, repo: %s] error creating github client: %w", webhookID, owner, repo, err)
 	}
 
+	namespace := fmt.Sprintf("previewbranch-%s-%s-%s", branch, strings.ReplaceAll(strings.ToLower(owner), "_", "-"),
+		strings.ReplaceAll(strings.ToLower(repo), "_", "-"))
+
 	depl := &models.Deployment{
 		EnvironmentID: env.ID,
-		Namespace:     "",
+		Namespace:     namespace[:63], // Kubernetes' DNS 1123 label requirement
 		Status:        types.DeploymentStatusCreating,
-		PullRequestID: 0,
 		PRName:        fmt.Sprintf("Deployment for branch %s", branch),
 		RepoName:      repo,
 		RepoOwner:     owner,
-		CommitSHA:     event.GetHeadCommit().GetSHA()[:7],
+		CommitSHA:     event.GetAfter()[:7],
 		PRBranchFrom:  branch,
 		PRBranchInto:  branch,
 	}
@@ -420,7 +421,7 @@ func (c *GithubIncomingWebhookHandler) processPushEvent(event *github.PushEvent,
 		github.CreateWorkflowDispatchEventRequest{
 			Ref: branch,
 			Inputs: map[string]interface{}{
-				"pr_number":      depl.ID,
+				"pr_number":      fmt.Sprintf("%d", depl.ID),
 				"pr_title":       fmt.Sprintf("Deployment for branch %s", branch),
 				"pr_branch_from": branch,
 				"pr_branch_into": branch,

+ 1 - 1
api/types/environment.go

@@ -74,7 +74,7 @@ type CreateDeploymentRequest struct {
 	*GitHubMetadata
 
 	Namespace     string `json:"namespace" form:"required"`
-	PullRequestID uint   `json:"pull_request_id" form:"required"`
+	PullRequestID uint   `json:"pull_request_id"`
 }
 
 type SuccessfullyDeployedResource struct {

+ 6 - 0
cli/cmd/apply.go

@@ -788,6 +788,12 @@ func (t *DeploymentHook) PreApply() error {
 		return fmt.Errorf("could not find environment for deployment")
 	}
 
+	if t.isBranchDeploy() {
+		t.namespace = fmt.Sprintf("previewbranch-%s-%s-%s", t.branchFrom,
+			strings.ReplaceAll(strings.ToLower(t.repoOwner), "_", "-"),
+			strings.ReplaceAll(strings.ToLower(t.repoName), "_", "-"))[:63] // Kubernetes' DNS 1123 label requirement
+	}
+
 	nsList, err := t.client.GetK8sNamespaces(
 		context.Background(), t.projectID, t.clusterID,
 	)

+ 1 - 0
dashboard/src/main/home/cluster-dashboard/preview-environments/environments/EnvironmentSettings.tsx

@@ -68,6 +68,7 @@ const EnvironmentSettings = () => {
       setBaseBranches(environment.git_repo_branches);
       setNewCommentsDisabled(environment.new_comments_disabled);
       setDeploymentMode(environment.mode);
+      setDeployBranches(environment.git_deploy_branches);
 
       if (environment.namespace_labels) {
         const labels: KeyValueType[] = Object.entries(

+ 1 - 0
dashboard/src/main/home/cluster-dashboard/preview-environments/types.ts

@@ -45,6 +45,7 @@ export type Environment = {
   deployment_count: number;
   mode: EnvironmentDeploymentMode;
   namespace_labels: Record<string, string>;
+  git_deploy_branches: string[];
 };
 
 export type PullRequest = {

+ 1 - 1
internal/integrations/ci/actions/preview.go

@@ -241,7 +241,7 @@ func getPreviewApplyActionYAML(opts *EnvOpts) ([]byte, error) {
 				"inputs": map[string]interface{}{
 					"pr_number": map[string]interface{}{
 						"description": "Pull request number",
-						"type":        "number",
+						"type":        "string",
 						"required":    true,
 					},
 					"pr_title": map[string]interface{}{