concourse icon indicating copy to clipboard operation
concourse copied to clipboard

Variable for Git commit reference

Open jpds opened this issue 5 years ago • 6 comments

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:

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?

jpds avatar Oct 18 '18 13:10 jpds

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

marco-m avatar Oct 19 '18 20:10 marco-m

@jpds did the answer from @marco-m help you. If so, please go ahead and close the issue or continue the talk.

Thnx

larssb avatar Nov 28 '18 09:11 larssb

HI

Can we use the variable value of the git tag cmd

i mean just like this (git tag $variable)

Arshadkolkar avatar Aug 16 '19 15:08 Arshadkolkar

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/. Also, it looks like we could use tags as well, which is under project-x.git/.git/refs/tags

ZozoZoeLi avatar Nov 29 '19 06:11 ZozoZoeLi

@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.

paulmiller3000 avatar Sep 02 '22 13:09 paulmiller3000

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 :-)

marco-m avatar Sep 02 '22 14:09 marco-m