7

I have link a github repo with my docker hub account. When there is a push to github master branch, a new image will be built in docker hub. But the image only has LATEST tag. How can I make the version increased automatically?

Ideally, I'd like it follow the sversion 1.0.0. And increase for every push 1.0.1, 1.0.2 1.0.3 etc.

Is there a way to make it follow this pattern?

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523

2 Answers2

4

You could associate a GitHub Action workflow to your repository, like docker/metadata-action

GitHub Action to extract metadata (tags, labels) for Docker. This action is particularly useful if used with Docker Build Push action.

You can see it used here. Warning: the tag name (as generated by the GitHub Action) will contain the branch name as well.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
4

I was having the same problem, solved with this GitHub Action Code:

  1. Create a secret called MAJOR to save your mayor version
  2. Create a secret called MINOR to save your minor version
  3. You will need a token to update you repo secrets, so... create a secret called REPO_ACCESS_TOKEN to grant your action dose his work.
name: Docker Image CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:    
    - uses: actions/checkout@v2
    -
      name: Build the Docker image
      run: docker build . --file src/MasterReport.UI/Dockerfile --tag eriksongm/master-report:${{ secrets.MAJOR }}.${{ secrets.MINOR }}
    -
      name: Login to DockerHub
      uses: docker/login-action@v1 
      with:
        username: ${{ secrets.DOCKERHUB_USERNAME }}
        password: ${{ secrets.DOCKERHUB_TOKEN }}
    -
      name: Push to DockerHub
      run: docker push eriksongm/master-report:${{ secrets.MAJOR }}.${{ secrets.MINOR }}
    -
      name: Update Minor version
      uses: hmanzur/actions-set-secret@v2.0.0
      with:
        name: 'MINOR'
        value: $((${{ secrets.MINOR }}+1))
        repository: EriksonGM/MasterReport
        token: ${{ secrets.REPO_ACCESS_TOKEN }}

This was my final code, as you can see, I have a last step just to update the minor version, only if all the other jobs run ok.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • worked for me. thank you! – carlosza Jan 07 '22 at 03:11
  • This does not work. I get error trying to build docker image using the incremented tag. Error: buildx failed with: error: invalid tag "ghcr.io/my-company/my-repo:***.***": invalid reference format – Mikolaj Jan 13 '22 at 22:37
  • @Mikolaj, can your share your code? – Erikson Gomes Jan 30 '22 at 04:45
  • @EriksonGomes I added a new question: https://stackoverflow.com/questions/70925425/how-to-implement-semantic-versioning-in-github-actions-workflow – Mikolaj Jan 31 '22 at 12:21
  • ``` - name: Autoincrement a new patch version run: | echo "NEW_PATCH_VERSION=$((${{ secrets.PATCH_VERSION }}+1))" >> $GITHUB_ENV``` Try setting the new patch version in a bash command like this. – Neil May 16 '22 at 04:20