Skip to content

Commit 18dcd1e

Browse files
committed
feat: use cloud deploy env vars #369
1 parent d51d27a commit 18dcd1e

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

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/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 {

0 commit comments

Comments
 (0)