Cloudbuild: How to deal with multiple environments?

41 Views Asked by At

I have read the documentation fairly extensively and yet I cannot find a reference to how one is supposed to implement multiple environments using a cloudbuild file.

This Medium article is out of date. One cannot pass variables from within the triggers anymore.

I could potentially set multiple yaml files (e.g cloudbuild.staging.yaml) but it is faily inelegant and inefficient.

I tried having a single file and setting up env variables dynamically during buildtime like so:

steps:
  - name: "gcr.io/cloud-builders/gcloud"
    entrypoint: "bash"
    args:
      - "-c"
      - |
        if [ "$BRANCH_NAME" == "main" ]; then
          export _ENVIRONMENT=$BRANCH_NAME
        elif [ "$BRANCH_NAME" == "staging" ]; then
          export _ENVIRONMENT=$BRANCH_NAME
        else
          export _ENVIRONMENT=$_PR_NUMBER
        fi

  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "build",
        "--platform=linux/amd64",
        "-t",
        "europe-west4-docker.pkg.dev/my-project-id/my-registry/my-app-${_ENVIRONMENT}",
        ".",
      ]

substitutions:
  _ENVIRONMENT: "staging"

But the environement name never gets replaced. It remains set to staging.

What is the proper way to deal with environments in cloudbuild?

Update: Ended up using multiple cloudbuild files Also noticed the field to inject variables in the trigger was now displaying.

0

There are 0 best solutions below