Use GITHub actions to make the "latest" version an old version

681 views Asked by At

I'm using GitHub Actions and Docker WatchTower to update my images on the fly when I check-in my software (no, it is not crucial software. It is more important to have a lean CI/CD).

name: Docker Image CI
on:
 push:
  branches: [ main ]
jobs:
 build:
  runs-on: ubuntu-latest
   steps:
   - uses: actions/checkout@v3
   - name: Build the Docker image
     run: docker build . -t me/myrepo:${{github.run_number}}
     #:$(date +%s)
     #docker build --rm -t ne/myrepo .

   - name: Login to Docker Hub
     env:
      DOCKER_USER: ${{ secrets.DOCKER_USER }}   
      DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}  
     run: |
      echo $DOCKER_USER
      docker login -u $DOCKER_USER -p $DOCKER_PASSWORD

    - name: Push the new Tag to Docker Hub
      run: |
       docker push me/myrepo:${{github.run_number}}

This works very nicely.

But watch tower can only download the latest version of a version e.g. latest. Best solution would be I could keep the incrementing versions on github actions and watchtower would take the highest version. I guess it cannot do that.

  • or -

I tag the latest version (e.g. 49) also as the latest. How would you do that with git hub actions? This should be nothing else than giving multiple tags to a build, no?

1

There are 1 answers

0
feder On

Well, I actually answered it almost myself when phrasing the question.

Simply create 2 builds and 2 images. One incrementing (so that you always could roll-back to an older version) and update the latest version.

Prioritize the latest so that it is available faster.

- name: Build the Docker image
  run: docker build . -t me/myrepo:latest    

- name: Build the Docker image
  run: docker build .

-t me/myrepo:${{github.run_number}}

and then push it twice.

- name: Push the also the latest Tag to Docker Hub
  run: |
    docker push me/myrepo:latest

- name: Push the new Tag to Docker Hub
  run: |
    docker push me/myrepo:${{github.run_number}}