concourse / registry-image-resource

a resource for images in a Docker registry
Apache License 2.0
89 stars 107 forks source link

Tag image with its code's commit SHA? #306

Closed markmark206 closed 2 years ago

markmark206 commented 2 years ago

It is super-easy to use docker-image resource to publish a docker image tagged with the commit SHA that triggered the build (I am including an example pipeline at the end).

I couldn't find any documentation or examples describing how to accomplish this with registry-image.

We would love to have a clear docker-image -> registry-image migration path, that would allow us to continue using this mechanism.

If this is currently possible with registry-image, it would be great to have clear documentation. Otherwise, it would be great to add this functionality (or keep keeping docker-image around).

Thank you!

PS Here is an example of this functionality implemented on top of docker-image resource:


resources:

- name: my-code
  type: git
  icon: github
  source:
     uri: "git@github.com:me/my-code.git"
     branch: main
     private_key: ((github-ssh-private-key))

- name: my-docker-image
  type: docker-image
  icon: docker
  source:
    repository: me/my-docker-image
    username: ((dockerhub_username))
    password: ((dockerhub_password))

jobs:
- name: build-my-docker-image
  plan:
  - get: my-code
    trigger: true
  - put: my-docker-image
    params:
      tag_file: my-code/.git/ref
      tag_as_latest: true
bennesp commented 2 years ago

Hello @markmark206

I encountered the same problem, and it seems that additional_tags is what you are searching, is it?

It is indeed in the documentation but it is not so obvious (README.md#out-push-and-tag-an-image):

With additional_tags given in params, the image will be pushed as each tag listed in the file (whitespace separated).

Your example would become something like the following, and it would produce an image with tag latest and the SHA of the commit, for example fc4a2b3484d0bd93bf22399e1b5f890d7994a737 :

resources:

- name: my-code
  type: git
  icon: github
  source:
     uri: "git@github.com:me/my-code.git"
     branch: main
     private_key: ((github-ssh-private-key))

- name: my-docker-image
  type: docker-image
  icon: docker
  source:
    repository: me/my-docker-image
    username: ((dockerhub_username))
    password: ((dockerhub_password))
    tag: "latest" # <-- CHANGE n.1

jobs:
- name: build-my-docker-image
  plan:
  - get: my-code
    trigger: true
  - put: my-docker-image
    params:
      additional_tags: my-code/.git/HEAD # <-- CHANGE n.2
xtremerui commented 2 years ago

Closing due to solution provided already.