Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tag docker hub automated builds as git sha

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.

like image 910
slashRahul Avatar asked Dec 28 '16 05:12

slashRahul


People also ask

How do I push tag to Docker Hub?

To push an image to Docker Hub, you must first name your local image using your Docker Hub username and the repository name that you created through Docker Hub on the web. You can add multiple images to a repository by adding a specific :<tag> to them (for example docs/base:testing ).

What is Docker SHA256?

A Docker image's ID is a digest, which contains an SHA256 hash of the image's JSON configuration object. Docker creates intermediate images during a local image build, for the purposes of maintaining a build cache. An image manifest is created and pushed to a Docker registry when an image is pushed.


1 Answers

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
like image 97
HeroCC Avatar answered Sep 21 '22 13:09

HeroCC