GoogleCloudBuild/gcbapp-example

Question - Assistance to get build running with TAG_NAME built-in substitutions

Opened this issue · 9 comments

I got an issue, that I have not been able to find a solution for.
I have installed and setup cloud build to be able to build code fetched from my private github repositories. It is working and I have created nice flows that builds dockers and push them to GCR etc.
I have now added a new build where I would like to tag my docker images with the git tag. I found that I could use the TAG_NAME built-in substitution variable to do that. But my problem is that it is never set when running the build. It seems that when fetching/pull the code from github no tags are fetched/pull.

config yaml

substitutions:
 _IMAGE_NAME: "<image_name>"
 _REGION: 'europe-west1'
steps:
- name: 'gcr.io/cloud-builders/docker'
  id: 'Build Docker Image'
  args: ['build', '-t', "eu.gcr.io/$PROJECT_ID/${_IMAGE_NAME}:${TAG_NAME}", '--build-arg', "jarversion=${TAG_NAME}", '--build-arg', 'gitsha=${SHORT_SHA}', '.']
images: 
- 'eu.gcr.io/$PROJECT_ID/${_IMAGE_NAME}:${TAG_NAME}' 

Here is the tag on my git(hub) repo. (it has been pushed to remote too).

> git describe --tags $(git rev-list --tags --max-count=1)
0.0.1

So what am I missing?

This here is the build-in substitutions for latest build, found on the execution detail tab under the failed build.

Built-in substitutions
COMMIT_SHA d96122536f520eff2bd8f829625f3e873d49b3f9
SHORT_SHA d961225
BRANCH_NAME main
REPO_NAME <repo name>
REVISION_ID d96122536f520eff2bd8f829625f3e873d49b3f9

But the TAG_NAME is not set...
Hope to hear from you soon. Thanks in advance.

I have tried to a an additional step

- name: gcr.io/cloud-builders/git
  args: [fetch, --depth=100]

But that just resulted in an error remote: Invalid username or password. which is strange since it can see the cloudbuild.yaml file in the repository.

I have tried to run the following from command line, in my repository:

> gcloud builds submit --config=cloudbuild.yaml --substitutions=TAG_NAME="0.0.1"

And that works fine. So my guess is that, some how the TAG_NAME var/arg is not set. Google Cloud Builder does not pickup/get the information on the git tags on my github repository.

Hi @navierula, I can see you did the last commit to master on this here repository. Can you help please? Thanks in advance.

Hi @kaaquist,
Not 100% sure - haven't tried that yet - but I think that TAG_NAME is actually populated and can be used only when your Build flow is set to be triggered based on Tags and not on Branch names. If your build kicks off based on branch that TAG_NAME would be empty. As I said, I'm not 100% sure but I think this is the issue.

I'm new to GCP and I'm working on a similar build:

...
substitutions:
 
  # We'll generate the application version being using TAG_NAME or SHORT_SHA
  _GENERATED_APPLICATION_VERSION: ${TAG_NAME:-${SHORT_SHA}}

  # We'll generate an image being tagged either with TAG_NAME
  # if that's present or with SHORT_SHA if no tag is found.

  _IMAGE_NAME: 'gcr.io/${PROJECT_ID}/${_APPLICATION_SERVICE_NAME}:${_GENERATED_APPLICATION_VERSION}'
...

@SebastianGogoasa thanks. That could make sense. I guess I could try that. I created a workaround and that have worked fine for last year or so.

Hi! I ran into this thread looking for the same issue. It doesn't seem to have any update and using TAG_NAME even when fetching from a branch makes sense. I'm moving to SHORT_SHA too, but it would be great to get this working

Just did a quick test of this behavior, and it works as I would expect, TAG_NAME and BRANCH_NAME are both useable regardless of trigger type, but in the case of a branch trigger TAG_NAME is empty, and same for BRANCH_NAME when using a tag trigger.

Are you expecting different behavior, I'm not sure why you would expect TAG_NAME to have a value when fetching from a branch.

It'd be awesome if we could substitute the substitute haha. I'm just trying to debug a cloud build trigger file and need to plugin the TAG_NAME value to some arbitrary release a while back. We have triggers setup on tag already and so I can't just assign it to a Tag event type.

This is an old thread, but @Jlrine2, the behavior I would expect is that TAG_NAME and BRANCH_NAME are always available regardless of the trigger type. For example, I could push a tag to any branch, but I do not want my build to trigger unless I push to the main branch. However, I want to build my container image tag based on the latest TAG_NAME on that commit.

Attempting to do this manually in a build step, i.e. git describe --tags $(git rev-list --tags --max-count=1) results in the following error:

Step #0: fatal: not a git repository (or any parent up to mount point /)
Step #0: Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

Attempting to do this in it's own step fails as well with the error: Step #0: fatal: No names found, cannot describe anything., despite the fact that this literal command does work when in a repo directory.

- name: 'gcr.io/cloud-builders/git'
  script: |
    #!/usr/bin/env bash
    echo $(git describe --tags $(git rev-list --tags --max-count=1)) >> tag.txt

    echo "Tag name: $(<tag.txt)"

It is very unfortunate that you cannot find the tag name when pushed to a branch. Alternatively, it should be possible to trigger on tag, but only with specific branches.