func()

in custom-targets/git-ops/git-deployer/deploy.go [93:156]


func (d *deployer) deploy(ctx context.Context) (*clouddeploy.DeployResult, error) {
	fmt.Printf("Accessing SecretVersion %s\n", d.params.gitSecret)
	s, err := d.accessSecretVersion(ctx, d.params.gitSecret)
	if err != nil {
		return nil, fmt.Errorf("unable to access git secret: %v", err)
	}
	fmt.Printf("Accessed SecretVersion %s\n", d.params.gitSecret)
	secret := string(s)

	repoParts := strings.Split(d.params.gitRepo, "/")
	if len(repoParts) != 3 {
		return nil, fmt.Errorf("invalid git repository reference: %q", d.params.gitRepo)
	}
	hostname, owner, repoName := repoParts[0], repoParts[1], repoParts[2]
	gitRepo := newGitRepository(hostname, owner, repoName, d.params.gitEmail, d.params.gitUsername)
	if err := d.setupGitWorkspace(ctx, secret, gitRepo); err != nil {
		return nil, fmt.Errorf("unable to set up git workspace: %v", err)
	}

	localManifest := "manifest.yaml"
	fmt.Printf("Downloaded rendered manifest to %s\n", localManifest)
	mURI, err := d.req.DownloadManifest(ctx, d.gcsClient, localManifest)
	if err != nil {
		return nil, fmt.Errorf("unable to download rendered manifest: %v", err)
	}
	fmt.Printf("Downloaded rendered manifest from %s\n", mURI)

	fmt.Println("Copying rendered manifest into local Git repository")
	gitManifestPath, err := copyToLocalGitRepo(localManifest, repoName, d.params.gitPath)
	if err != nil {
		return nil, fmt.Errorf("unable to copy manifest to local git repository: %v", err)
	}
	op, err := gitRepo.detectDiff()
	if err != nil {
		return nil, fmt.Errorf("unable to run git status: %v", err)
	}
	if len(op) == 0 {
		return nil, fmt.Errorf("no diff detected between the rendered manifest and the manifest on branch %s", d.params.gitSourceBranch)
	}
	fmt.Printf("Committing and pushing changes to branch %s\n", d.params.gitSourceBranch)
	if err := d.commitPushGitWorkspace(ctx, gitRepo); err != nil {
		return nil, fmt.Errorf("unable to commit and push changes: %v", err)
	}

	if err := d.handleDestinationBranch(ctx, gitRepo, secret); err != nil {
		return nil, err
	}

	fmt.Println("Uploading rendered manifest as a deploy artifact")
	dURI, err := d.req.UploadArtifact(ctx, d.gcsClient, "manifest.yaml", &clouddeploy.GCSUploadContent{LocalPath: gitManifestPath})
	if err != nil {
		return nil, fmt.Errorf("error uploading deploy artifact: %v", err)
	}
	fmt.Printf("Uploaded deploy artifact to %s\n", dURI)

	return &clouddeploy.DeployResult{
		ResultStatus:  clouddeploy.DeploySucceeded,
		ArtifactFiles: []string{dURI},
		Metadata: map[string]string{
			clouddeploy.CustomTargetSourceMetadataKey:    gitDeployerSampleName,
			clouddeploy.CustomTargetSourceSHAMetadataKey: clouddeploy.GitCommit,
		},
	}, nil
}