Rishav Kumar
Rishav Kumar

Reputation: 45

How to use dynamically versioning in cloudbuild.yaml file?

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:

Step 1: Build the Docker image

Step 2: Tag the Docker image

Step 3: Push the Docker image to Google Container Registry (GCR)

Step 4: Deploy the container image to Cloud Run

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:

Step 1: Fetch version from package.json and store it in an environment variable

Step 2: Load the version from the environment file and export it as a substitution

Step 3: Build the Docker image

Step 4: Tag the Docker image

Step 5: Push the Docker image to Google Container Registry (GCR)

Step 6: Deploy the container image to Cloud Run

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

Answers (1)

Abdellatif Derbel
Abdellatif Derbel

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

Related Questions