Skip to content

Commit a9c76c7

Browse files
authored
Merge pull request #370 from GoogleCloudPlatform/issue369
feat: use cloud deploy env variables
2 parents 9795ca8 + 18dcd1e commit a9c76c7

File tree

8 files changed

+77
-55
lines changed

8 files changed

+77
-55
lines changed

cmd/integrationcli/integrationcli.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package main
1616

1717
import (
1818
"fmt"
19+
"internal/apiclient"
1920
"internal/cmd"
2021
"os"
2122
)
@@ -29,6 +30,7 @@ var (
2930

3031
func main() {
3132
rootCmd := cmd.GetRootCmd()
33+
apiclient.SetBuildParams(version, commit, date)
3234
rootCmd.Version = fmt.Sprintf("%s date: %s [commit: %.7s]", version, date, commit)
3335

3436
if err := rootCmd.Execute(); err != nil {

internal/apiclient/bundles.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ func ExtractTgz(gcsURL string) (folder string, err error) {
225225
return folder, nil
226226
}
227227

228-
func GetCloudDeployGCSLocations(pipeline string, release string) (skaffoldConfigUri string, err error) {
228+
func GetCloudDeployGCSLocations(cloudDeployProjectId string, cloudDeployLocation string,
229+
pipeline string, release string) (skaffoldConfigUri string, err error) {
229230
type cloudDeployRelease struct {
230231
SkaffoldConfigUri string `json:"skaffoldConfigUri"`
231232
TargetArtifacts map[string]struct {
@@ -242,7 +243,7 @@ func GetCloudDeployGCSLocations(pipeline string, release string) (skaffoldConfig
242243
r := cloudDeployRelease{}
243244

244245
cloudDeployURL := fmt.Sprintf("https://clouddeploy.googleapis.com/v1/projects/%s/locations/%s/deliveryPipelines/%s/releases/%s",
245-
GetProjectID(), GetRegion(), pipeline, release)
246+
cloudDeployProjectId, cloudDeployLocation, pipeline, release)
246247
u, _ := url.Parse(cloudDeployURL)
247248

248249
ClientPrintHttpResponse.Set(false)

internal/apiclient/options.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ type clientPrintHttpResponse struct {
9898

9999
var ClientPrintHttpResponse = &clientPrintHttpResponse{enable: true}
100100

101+
var cliVersion, cliCommitSha, cliBuildDate string
102+
101103
// NewIntegrationClient sets up options to invoke Integration APIs
102104
func NewIntegrationClient(o IntegrationClientOptions) {
103105
if options == nil {
@@ -495,3 +497,19 @@ func GetMetadataToken() bool {
495497
func SetMetadataToken(b bool) {
496498
options.MetadataToken = b
497499
}
500+
501+
func SetBuildParams(version string, commit string, date string) {
502+
cliVersion = version
503+
cliCommitSha = commit
504+
cliBuildDate = date
505+
}
506+
507+
func GetBuildParams() (version string, commit string, date string) {
508+
if cliVersion == "" {
509+
cliVersion = "unknown"
510+
}
511+
if cliCommitSha == "" {
512+
cliCommitSha = "unknown"
513+
}
514+
return cliVersion, cliCommitSha, cliBuildDate
515+
}

internal/cmd/integrations/apply.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,6 @@ var ApplyCmd = &cobra.Command{
4949
if err = apiclient.SetRegion(utils.GetStringParam(cmdRegion)); err != nil {
5050
return err
5151
}
52-
if folder == "" && (pipeline == "" || release == "" || outputGCSPath == "") {
53-
return fmt.Errorf("atleast one of folder or pipeline, release and outputGCSPath must be supplied")
54-
}
55-
if folder != "" && (pipeline != "" || release != "" || outputGCSPath != "") {
56-
return fmt.Errorf("both folder and pipeline, release and outputGCSPath cannot be supplied")
57-
}
58-
if (pipeline != "" && (release == "" || outputGCSPath == "")) ||
59-
(release != "" && (pipeline == "" && outputGCSPath == "")) ||
60-
(outputGCSPath != "" && (pipeline == "" && release == "")) {
61-
return fmt.Errorf("release, pipeline and outputGCSPath must be set")
62-
}
6352
cmd.Flags().VisitAll(func(f *pflag.Flag) {
6453
clilog.Debug.Printf("%s: %s\n", f.Name, f.Value)
6554
})
@@ -71,7 +60,7 @@ var ApplyCmd = &cobra.Command{
7160
var skaffoldConfigUri string
7261

7362
if folder == "" {
74-
skaffoldConfigUri, err = apiclient.GetCloudDeployGCSLocations(pipeline, release)
63+
skaffoldConfigUri, err = apiclient.GetCloudDeployGCSLocations(cloudDeployProjectId, cloudDeployLocation, pipeline, release)
7564
if err != nil {
7665
return err
7766
}
@@ -90,6 +79,7 @@ var ApplyCmd = &cobra.Command{
9079
}
9180

9281
createSecret, _ := strconv.ParseBool(utils.GetStringParam(cmd.Flag("create-secret")))
82+
cloudDeploy, _ := strconv.ParseBool(utils.GetStringParam(cmd.Flag("cloud-deploy")))
9383
grantPermission, _ := strconv.ParseBool(utils.GetStringParam(cmd.Flag("grant-permission")))
9484
userLabel := utils.GetStringParam(cmd.Flag("user-label"))
9585
wait, _ := strconv.ParseBool(utils.GetStringParam(cmd.Flag("wait")))
@@ -110,6 +100,12 @@ var ApplyCmd = &cobra.Command{
110100

111101
apiclient.DisableCmdPrintHttpResponse()
112102

103+
if cloudDeploy {
104+
if err = storeCloudDeployVariables(); err != nil {
105+
return err
106+
}
107+
}
108+
113109
if !skipAuthconfigs {
114110
if err = processAuthConfigs(authconfigFolder); err != nil {
115111
return err
@@ -160,20 +156,17 @@ Apply scaffold configuration, but skip connectors: ` + GetExample(12) + `
160156
Apply scaffold configuration and run functional tests: ` + GetExample(18),
161157
}
162158

163-
var serviceAccountName, serviceAccountProject, encryptionKey, pipeline, release, outputGCSPath string
159+
var serviceAccountName, serviceAccountProject, encryptionKey, pipeline string
160+
var release, outputGCSPath, cloudDeployProjectId, cloudDeployLocation string
164161

165162
func init() {
166163
var userLabel string
167-
grantPermission, createSecret, wait, runTests := false, false, false, false
164+
grantPermission, createSecret, wait, runTests, cloudDeploy := false, false, false, false, false
168165

169166
ApplyCmd.Flags().StringVarP(&folder, "folder", "f",
170167
"", "Folder containing scaffolding configuration")
171-
ApplyCmd.Flags().StringVarP(&pipeline, "pipeline", "",
172-
"", "Cloud Deploy Pipeline name")
173-
ApplyCmd.Flags().StringVarP(&release, "release", "",
174-
"", "Cloud Deploy Release name")
175-
ApplyCmd.Flags().StringVarP(&outputGCSPath, "output-gcs-path", "",
176-
"", "Upload a file named results.json containing the results")
168+
ApplyCmd.Flags().BoolVarP(&cloudDeploy, "cloud-deploy", "",
169+
false, "Deploy using Cloud Deploy; default is false")
177170
ApplyCmd.Flags().BoolVarP(&grantPermission, "grant-permission", "g",
178171
false, "Grant the service account permission to the GCP resource; default is false")
179172
ApplyCmd.Flags().StringVarP(&userLabel, "userlabel", "u",
@@ -748,3 +741,17 @@ func processTestCases(testsFolder string, integrationName string, version string
748741
}
749742
return nil
750743
}
744+
745+
func storeCloudDeployVariables() (err error) {
746+
pipeline = os.Getenv("CLOUD_DEPLOY_DELIVERY_PIPELINE")
747+
release = os.Getenv("CLOUD_DEPLOY_RELEASE_NAME")
748+
outputGCSPath = os.Getenv("CLOUD_DEPLOY_OUTPUT_GCS_PATH")
749+
cloudDeployProjectId = os.Getenv("CLOUD_DEPLOY_PROJECT_ID")
750+
cloudDeployLocation = os.Getenv("CLOUD_DEPLOY_LOCATION")
751+
752+
if pipeline == "" || release == "" || outputGCSPath == "" || cloudDeployProjectId == "" {
753+
return fmt.Errorf("CLOUD_DEPLOY_DELIVERY_PIPELINE, CLOUD_DEPLOY_RELEASE_NAME, CLOUD_DEPLOY_OUTPUT_GCS_PATH, " +
754+
"CLOUD_DEPLOY_PROJECT_ID, CLOUD_DEPLOY_LOCATION must be set")
755+
}
756+
return nil
757+
}

internal/cmd/render/render.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ var Cmd = &cobra.Command{
3030
cmd.SilenceUsage = true
3131

3232
outputGCSPath := utils.GetStringParam(cmd.Flag("output-gcs-path"))
33-
34-
err = apiclient.WriteManifest(outputGCSPath, utils.GetCLIVersion())
33+
v, _, _ := apiclient.GetBuildParams()
34+
err = apiclient.WriteManifest(outputGCSPath, v)
3535
return
3636
},
3737
}

internal/cmd/utils/utils.go

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"internal/apiclient"
2020
"io"
2121
"os"
22-
"runtime/debug"
2322
"strconv"
2423
"strings"
2524

@@ -51,7 +50,7 @@ const cloudBuild = `# Copyright 2023 Google LLC
5150
5251
steps:
5352
- id: 'Apply Integration scaffolding configuration'
54-
name: us-docker.pkg.dev/appintegration-toolkit/images/integrationcli:latest
53+
name: us-docker.pkg.dev/appintegration-toolkit/images/integrationcli:%s
5554
args:
5655
- integrations
5756
- apply
@@ -104,26 +103,27 @@ metadata:
104103
name: appint-%s-pipeline
105104
serialPipeline:
106105
stages:
107-
- targetId: %s-env
106+
- targetId: %s
108107
---
109108
110109
apiVersion: deploy.cloud.google.com/v1
111110
kind: Target
112111
metadata:
113-
name: %s-env
112+
name: %s
114113
customTarget:
115114
customTargetType: appint-%s-target
116115
deployParameters:
117116
APP_INTEGRATION_PROJECT_ID: "%s"
117+
APP_INTEGRATION_REGION: "%s"
118118
---
119119
120120
apiVersion: deploy.cloud.google.com/v1
121121
kind: CustomTargetType
122122
metadata:
123123
name: appint-%s-target
124124
customActions:
125-
renderAction: render-app-integration
126-
deployAction: deploy-app-integration`
125+
renderAction: render-%s-integration
126+
deployAction: deploy-%s-integration`
127127

128128
var skaffold = `# Copyright 2024 Google LLC
129129
#
@@ -145,7 +145,7 @@ customActions:
145145
- name: render-%s-integration
146146
containers:
147147
- name: render
148-
image: us-docker.pkg.dev/appintegration-toolkit/images/integrationcli:latest
148+
image: us-docker.pkg.dev/appintegration-toolkit/images/integrationcli:%s
149149
command: ['sh']
150150
args:
151151
- '-c'
@@ -154,12 +154,12 @@ customActions:
154154
- name: deploy-%s-integration
155155
containers:
156156
- name: deploy
157-
image: us-docker.pkg.dev/appintegration-toolkit/images/integrationcli:latest
157+
image: us-docker.pkg.dev/appintegration-toolkit/images/integrationcli:%s
158158
command: ['sh']
159159
args:
160160
- '-c'
161161
- |-
162-
integrationcli integrations apply --env=dev --reg=$CLOUD_DEPLOY_LOCATION --proj=$APP_INTEGRATION_PROJECT_ID --pipeline=$CLOUD_DEPLOY_DELIVERY_PIPELINE --release=$CLOUD_DEPLOY_OUTPUT_GCS_PATH --output-gcs-path=$CLOUD_DEPLOY_TARGET --metadata-token`
162+
integrationcli integrations apply --env=$CLOUD_DEPLOY_TARGET --reg=$CLOUD_DEPLOY_LOCATION --proj=$APP_INTEGRATION_PROJECT_ID --reg=$APP_INTEGRATION_REGION --cloud-deploy=true --run-tests=true --wait=true --metadata-token`
163163

164164
var githubActionApply = `# Copyright 2025 Google LLC
165165
#
@@ -223,23 +223,26 @@ jobs:
223223
224224
- name: Create and Publish Integration
225225
id: 'publish-integration'
226-
uses: docker://us-docker.pkg.dev/appintegration-toolkit/images/integrationcli:v0.79.0 #pin to version of choice
226+
uses: docker://us-docker.pkg.dev/appintegration-toolkit/images/integrationcli:%s #pin to version of choice
227227
with:
228228
args: integrations apply --env=${{ env.ENVIRONMENT}} --folder=. --userlabel=${{ steps.calc-vars.outputs.SHORT_SHA }} --wait=true --proj=${{ env.PROJECT_ID }} --reg=${{ env.REGION }} --token ${{ steps.gcp-auth.outputs.access_token }}`
229229

230230
func GetCloudDeployYaml(integrationName string, env string) string {
231231
if env == "" {
232232
env = "dev"
233233
}
234-
return fmt.Sprintf(cloudDeploy, integrationName, env, env, integrationName, apiclient.GetProjectID(), integrationName)
234+
return fmt.Sprintf(cloudDeploy, integrationName, env, env, integrationName, apiclient.GetProjectID(), apiclient.GetRegion(),
235+
integrationName, integrationName, integrationName)
235236
}
236237

237238
func GetSkaffoldYaml(integrationName string) string {
238-
return fmt.Sprintf(skaffold, integrationName, integrationName)
239+
v, _, _ := apiclient.GetBuildParams()
240+
return fmt.Sprintf(skaffold, integrationName, v, integrationName, v)
239241
}
240242

241243
func GetCloudBuildYaml() string {
242-
return cloudBuild
244+
v, _, _ := apiclient.GetBuildParams()
245+
return fmt.Sprintf(cloudBuild, v)
243246
}
244247

245248
func ReadFile(filePath string) (byteValue []byte, err error) {
@@ -271,19 +274,8 @@ func GetGithubAction(environment string, integrationName string) string {
271274
} else {
272275
githubAction = githubActionApply
273276
}
274-
v := GetCLIVersion()
275-
if v != "" {
276-
githubAction = strings.ReplaceAll(githubAction, "v0.79.0", v)
277-
}
278-
return fmt.Sprintf(githubAction, integrationName)
279-
}
280-
281-
func GetCLIVersion() string {
282-
bi, ok := debug.ReadBuildInfo()
283-
if ok && bi.Main.Version != "" {
284-
return bi.Main.Version
285-
}
286-
return "latest"
277+
v, _, _ := apiclient.GetBuildParams()
278+
return fmt.Sprintf(githubAction, integrationName, v)
287279
}
288280

289281
func GetBasicInfo(cmd *cobra.Command, flag string) bool {

samples/scaffold-sample/clouddeploy.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ metadata:
1818
name: appint-sample-pipeline
1919
serialPipeline:
2020
stages:
21-
- targetId: dev-env
22-
- targetId: staging-env
21+
- targetId: dev
22+
- targetId: staging
2323
---
2424
apiVersion: deploy.cloud.google.com/v1
2525
kind: Target
2626
metadata:
27-
name: dev-env
27+
name: dev
2828
customTarget:
2929
customTargetType: appint-sample-target
3030
deployParameters:
3131
APP_INTEGRATION_PROJECT_ID: "dev-integration-project"
32+
APP_INTEGRATION_REGION: "us-central1"
3233
#executionConfigs:
3334
#- usages:
3435
# - DEPLOY
@@ -44,11 +45,12 @@ deployParameters:
4445
apiVersion: deploy.cloud.google.com/v1
4546
kind: Target
4647
metadata:
47-
name: staging-env
48+
name: staging
4849
customTarget:
4950
customTargetType: appint-sample-target
5051
deployParameters:
5152
APP_INTEGRATION_PROJECT_ID: "staging-integration-project"
53+
APP_INTEGRATION_REGION: "us-central1"
5254
#executionConfigs:
5355
#- usages:
5456
# - DEPLOY

samples/scaffold-sample/skaffold.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ customActions:
3232
args:
3333
- '-c'
3434
- |-
35-
integrationcli integrations apply --env=dev --reg=$CLOUD_DEPLOY_LOCATION --proj=$APP_INTEGRATION_PROJECT_ID --pipeline=$CLOUD_DEPLOY_DELIVERY_PIPELINE --release=$CLOUD_DEPLOY_RELEASE --output-gcs-path=$CLOUD_DEPLOY_OUTPUT_GCS_PATH --run-tests=true --metadata-token
35+
integrationcli integrations apply --env=$CLOUD_DEPLOY_TARGET --reg=$APP_INTEGRATION_REGION --proj=$APP_INTEGRATION_PROJECT_ID --wait=true --run-tests=true --metadata-token

0 commit comments

Comments
 (0)