Reputation: 45
This is my cloudbuild.yaml file. In this file i manually write the version of the new application, but i want to take this automatically by any approach, Is this possible anyhow?
steps:
substitutions: _VERSION: '1.0.0'
options: defaultLogsBucketBehavior: 'REGIONAL_USER_OWNED_BUCKET'
I tried many approach but nothing are working, one of the approach I share below
steps:
substitutions: _VERSION: '1.0.0' # Default value, will be overridden by the fetched version
options: defaultLogsBucketBehavior: 'REGIONAL_USER_OWNED_BUCKET'
I faced this type error of error everytime
Your build failed to run: generic::invalid_argument: invalid value for 'build.substitutions': key in the template "VERSION" is not a valid built-in substitution.
Upvotes: 0
Views: 217
Reputation: 604
I suppose that create tag to run this build.
You can use this code to get a tag and replace all points by hyphens.
substitutions:
# Double replace hack
_VERSION_NAME_1: ${TAG_NAME//./-}
_VERSION_NAME_2: ${_VERSION_NAME_1//_/-}
So if tag = 1.2.3 _VERSION_NAME_2 wil be 1-2-3
Your cloudbuild.yaml file can be like that:
steps:
- id: 'build-the-docker-image'
name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'demo-backend', '-f', 'backend/Dockerfile', 'backend']
- id: 'tag-the-docker-image'
name: 'gcr.io/cloud-builders/docker'
args: ['tag', 'demo-backend', 'location-docker.pkg.dev/demo/demo-backend-repository/demo-backend:${_VERSION_NAME_2}']
- id: 'push-the-docker-image'
name: 'gcr.io/cloud-builders/docker'
args: ['push', 'location-docker.pkg.dev/demo/demo-backend-repository/demo-backend:${_VERSION_NAME_2}']
- id: 'deploy-the-docker-image'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: bash
args:
- '-c'
- |
gcloud run deploy demo-backend \
--image=location-docker.pkg.dev/demo/demo-backend-repository/demo-backend:${_VERSION_NAME_2} \
--platform=managed \
--region=location \
--allow-unauthenticated
substitutions:
# Double replace hack
_VERSION_NAME_1: ${TAG_NAME//./-}
_VERSION_NAME_2: ${_VERSION_NAME_1//_/-}
options:
dynamic_substitutions: true
FYI Cloud Build provides the following default substitutions for builds invoked by triggers:
$TRIGGER_NAME: the name associated with your trigger
$COMMIT_SHA: the commit ID associated with your build
$REVISION_ID: the commit ID associated with your build
$SHORT_SHA : the first seven characters of COMMIT_SHA
$REPO_NAME: the name of your repository
$REPO_FULL_NAME: the full name of your repository, including either the user or organization
$BRANCH_NAME: the name of your branch
$TAG_NAME: the name of your tag
$REF_NAME: the name of your branch or tag
$TRIGGER_BUILD_CONFIG_PATH: the path to your build configuration file used during your build execution; otherwise, an empty string if your build is configured inline on the trigger or uses a Dockerfile or Buildpack.
$SERVICE_ACCOUNT_EMAIL: email of the service account you are using for the build. This is either a default service account or a user-specified service account.
$SERVICE_ACCOUNT: the resource name of the service account, in the format projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT_EMAIL
Upvotes: 0