in custom-targets/terraform/terraform-deployer/deploy.go [76:133]
func (d *deployer) deploy(ctx context.Context) (*clouddeploy.DeployResult, error) {
// Download the Terraform configuration uploaded at render time and unarchive it in the same
// directory that was used at render time.
fmt.Printf("Downloading Terraform configuration archive to %s\n", srcArchivePath)
inURI, err := d.req.DownloadInput(ctx, d.gcsClient, renderedArchiveName, srcArchivePath)
if err != nil {
return nil, fmt.Errorf("unable to download deploy input with object suffix %s: %v", renderedArchiveName, err)
}
fmt.Printf("Downloaded Terraform configuration archive from %s\n", inURI)
archiveFile, err := os.Open(srcArchivePath)
if err != nil {
return nil, fmt.Errorf("unable to open archive file %s: %v", srcArchivePath, err)
}
fmt.Printf("Unarchiving Terraform configuration in %s to %s\n", srcArchivePath, srcPath)
if err := archiver.NewTarGz().Unarchive(archiveFile.Name(), srcPath); err != nil {
return nil, fmt.Errorf("unable to unarchive terraform configuration: %v", err)
}
terraformConfigPath := path.Join(srcPath, d.params.configPath)
fmt.Println("Initializing Terraform configuration to install providers")
if _, err := terraformInit(terraformConfigPath, &terraformInitOptions{disableBackendInitialization: true, disableModuleDownloads: true}); err != nil {
return nil, fmt.Errorf("error running terraform init to install providers: %v", err)
}
if _, err := terraformApply(terraformConfigPath, &terraformApplyOptions{applyParallelism: d.params.applyParallelism, lockTimeout: d.params.lockTimeout}); err != nil {
return nil, fmt.Errorf("error running terraform apply: %v", err)
}
fmt.Println("Finished applying Terraform configuration")
fmt.Println("Getting the Terraform state to provide as a deploy artifact")
ts, err := terraformShowState(terraformConfigPath)
if err != nil {
return nil, fmt.Errorf("error getting terraform state after apply: %v", err)
}
fmt.Println("Extracting Terraform output values from the Terraform state")
metadata, err := extractOutputsFromTfState(ts)
if err != nil {
return nil, fmt.Errorf("error extracting terraform outputs from the terraform state: %v", err)
}
fmt.Println("Uploading Terraform state as a deploy artifact")
stateGCSURI, err := d.req.UploadArtifact(ctx, d.gcsClient, "deployed-state.json", &clouddeploy.GCSUploadContent{Data: ts})
if err != nil {
return nil, fmt.Errorf("error uploading terraform state deploy artifact: %v", err)
}
fmt.Printf("Uploaded Terraform state deploy artifact to %s\n", stateGCSURI)
// Metadata consists of the Terraform output values and an indicator that the deploy was handled by the
// cloud deploy terraform sample.
metadata[clouddeploy.CustomTargetSourceMetadataKey] = tfDeployerSampleName
metadata[clouddeploy.CustomTargetSourceSHAMetadataKey] = clouddeploy.GitCommit
deployResult := &clouddeploy.DeployResult{
ResultStatus: clouddeploy.DeploySucceeded,
ArtifactFiles: []string{stateGCSURI},
Metadata: metadata,
}
return deployResult, nil
}