Search code examples
google-cloud-platformdeploymentgoogle-cloud-build

How to add substitution variables in cloud build when creating the trigger?


There is a divergence between Google Cloud Console and Gcloud when creating a Cloud Build Trigger. On the Google Cloud Console it is possible to add the substitution variables during the creation process without having to necessarily submit a build. But I couldn't find a way to do it with gcloud beta builds triggers create. I've found that you can add substitution variables with the command gcloud builds submit, which will do it during the build process. I would like to be able to use gcloud the same way I use the Console, which is, inform the substitutions during the creation process. Is it possible?


Solution

  • It seems it's not on the official documentation, but just noticed that if you run:

    gcloud beta builds triggers create cloud-source-repositories --help
    

    there is a section that describes adding substitutions to the trigger:

             --substitutions=[KEY=VALUE,...]
                Parameters to be substituted in the build specification.
    
                For example (using some nonsensical substitution keys; all keys
                must begin with an underscore):
    
                    $ gcloud builds triggers create ... --config config.yaml
                        --substitutions _FAVORITE_COLOR=blue,_NUM_CANDIES=10
    
                This will result in a build where every occurrence of
                ${_FAVORITE_COLOR} in certain fields is replaced by "blue", and
                similarly for ${_NUM_CANDIES} and "10".
    
                Only the following built-in variables can be specified with the
                --substitutions flag: REPO_NAME, BRANCH_NAME, TAG_NAME,
                REVISION_ID, COMMIT_SHA, SHORT_SHA.
    

    So the below command should work:

    gcloud beta builds triggers create ... --config config.yaml \
    --substitutions _FAVORITE_COLOR=blue,_NUM_CANDIES=10