Cloud build dynamic tags to help filter

Viewed 159

I'm triggering a build on a web hook from a pull-request, and then it reads in the payload of the request. The payload has things like PR name, author, commit hash, etc.

I'd like to inject some of these into the tag attribute but their format doesn't comply:

invalid build: invalid build tag "Ari Example": must match format "^[\\w][\\w.-]{0,127}$"

Is there any way I could modify the tag dynamically, in the example above for a users name?

Ari Example -> ari-example

My cloudbuild.yaml looks like so:

substitutions:
# These a dynamically populated by the PR and substitutions
# below is just for illustrative purposes

  _PR_AUTHOR_ID: '827364872-23472634-2352'
  _PR_AUTHOR: 'Ari Example'
  _PR_TITLE: 'fix: update some file'

steps:
  - id: 'Pull Request Payload'
    name: 'gcr.io/cloud-builders/git'
    entrypoint: '/bin/bash'
    args:
    - '-c'
    - |
      echo Author: $_PR_AUTHOR (id: $_PR_AUTHOR_ID)
      echo Title:  $_PR_TITLE

tags: ['$_PR_AUTHOR']
1 Answers

Pure bash. Below you replace any spaces with dashes and convert the string to lowercase:

_PR_AUTHOR='Ari Example'

echo ${_PR_AUTHOR// /-} | awk '{print tolower($0)}'

Output: ari-example

If you want to convert special characters and add more validation you might need to use regex.

Related