18

We are using automated Docker hub builds to create our application images automatically whenever new commit is pushed to github.

That works well when we manually change the tag on docker hub. Now what we want is to create the image tag automatically as git commit sha so that we can pull that image in our kubernetes deployment for rolling updates

we want some thing like this, when commit foo is pushed in our application repository, docker hub will build the image automatically and we will have new image on dockerhub as myimage:foo

I did not find the documentation to achieve this on docker hub. How can one achieve this ?we have only two options on dockerhub, i.e tag, branch

Thanks.

manojlds
  • 290,304
  • 63
  • 469
  • 417
slashRahul
  • 555
  • 5
  • 18
  • 1
    You may be interested in the advanced automated builds on Docker Hub; see https://docs.docker.com/docker-cloud/builds/advanced/ – thaJeztah Dec 28 '16 at 23:48
  • @thaJeztah Tried but did not work, any use case you have ? – slashRahul Dec 29 '16 at 13:01
  • Could you add more details what you tried, and what didn't work on Docker Cloud? May be worth including that information, then I can try to either get information on how to resolve it, or report a bug – thaJeztah Dec 29 '16 at 17:53
  • I tried using this in Dockerfile `ENV SOURCE_COMMIT ` Not sure if this is the way to use it – slashRahul Dec 30 '16 at 04:54
  • I am also facing the same issue. Any help here. Kubernetes cluster is not updating if the image tag is same and docker hub automated build system generates only latest tag or tag with branch name. Docker hub should have an option to tag am image with git commit id. – Jitendra Oct 31 '17 at 04:33

1 Answers1

8

Make a new executable file in hooks/ called post_push with these contents to push another image with the latest git short hash as its tag:

#!/bin/bash

SHORTHASH="$(git rev-parse --short HEAD)"
docker tag $IMAGE_NAME $DOCKER_REPO:$SHORTHASH
docker push $DOCKER_REPO:$SHORTHASH
HeroCC
  • 988
  • 15
  • 33
  • 2
    Why not use the built-in $SOURCE_COMMIT instead of your specialized $SHORTHASH code? – rexypoo Jan 25 '20 at 19:42
  • @rexypoo It's been a while since I used $SOURCE_COMMIT, but IIRC it is one char shorter than the rev-parse git puts out. Most other tools use the longer version, and so I rather use that one. `$SHORTHASH == c67080b8` and `$SOURCE_COMMIT == c67080b`. – HeroCC May 05 '20 at 04:06