concourse
concourse copied to clipboard
Variable for Git commit reference
What challenge are you facing?
I need to build a new Docker image with every Git commit as I'm deploying things on Kubernetes. For deployments to work correctly in Kubernetes, one needs to give the Deployment object a new image tag to do rolling upgrades, etc. I cannot use the :latest
tag for this, because it is not considered a change to the API object so nothing happens.
For some reason, it appears to be difficult in Concourse to get the git reference hash for the latest commit and pass that to the Docker image stage.
There are numerous workarounds and Github issues about this, such as:
- https://github.com/concourse/git-resource/issues/56
- https://github.com/concourse/git-resource/issues/104
- https://github.com/concourse/git-resource/issues/170
- https://stackoverflow.com/questions/48506510/concourse-git-resource-accessing-a-git-tag-to-use-on-a-docker-image-put
As a newcomer to Concourse, I find this quite strange because, as you can see from this screenshot:
...the system knows about the Git commit ref from the git resource. I've even seen that it can pick up the Git tags which would similarly be useful for Docker images.
Why can I not simply take that reference from the metadata and then pass it onto the Docker task for it to be the tag for the Docker build? Or even have it written to an environment variable such as BUILD_GIT_REF
a la BUILD_ID
which I can then pass in?
The trick is in the documentation of the git resource:
Additional files populated
.git/ref: Version reference detected and checked out. It will usually contain the commit SHA-1 ref, but also the detected tag name when using tag_filter.
and the docker image resource:
out: Push an image, or build and push a Dockerfile.
Parameters tag_file: Optional. The value should be a path to a file containing the name of the tag.
This pipeline will build and push a docker image, tagging it with the commit hash of the source repo:
resources:
- name: project-x.git
type: git
source:
uri: https://github.com/marco-m/concourse-pipelines
branch: master
- name: project-x-docker-image
type: docker-image
source:
repository: ((your-dockerhub-id))/project-x
username: ((dockerhub-username))
password: ((dockerhub-password))
jobs:
- name: build-docker-image
plan:
- get: project-x.git
trigger: true
- put: project-x-docker-image
params:
# The directory containing the Dockerfile
build: project-x.git/build-docker-image
tag_file: project-x.git/.git/ref
@jpds did the answer from @marco-m help you. If so, please go ahead and close the issue or continue the talk.
Thnx
HI
Can we use the variable value of the git tag cmd
i mean just like this (git tag $variable)
project-x.git/.git/ref
Thanks a lot for the help! Whereas in my case the path looks like project-x.git/.git/refs/heads/
@marco-m I realize this is very old, but I just want to thank you for including a snippet of code that actually shows how to use one of the "additional files populated," such as .git/.git/ref
. There is absolutely no documentation whatsoever about HOW to get that file and use it in a subsequent job step. Sometimes it feels like Concourse documentation is deliberately obtuse. Your example set me on the right path.
Hello @paulmiller3000, happy it helped! It took me a while to arrive to that snippet, so I wanted to share it :-) Concourse documentation is sometimes terse, it pays reading, re-reading and making experiments. At the end, we have to remember that it is open source and free :-)