Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github actions docker caching

I think this will be useful for many others.

I am using https://github.com/phips28/gh-action-bump-version to automatically bump NPM versions in Github Actions.

Is there a way to cache the docker image of this action so it doesn't have to build each time? It takes a long time to run and it runs upfront before the rest of the steps. I am sure this is common for similar types of Github Actions that pull docker images.

The docker image looks pretty slim so I am not sure there will be any benefit of trying to optimise the image itself. More to do with how to configure Github Actions.

Any suggestions?

like image 260
Saul Frank Avatar asked Oct 02 '20 16:10

Saul Frank


1 Answers

TLDR

Somewhat! You can change the GitHub workflow file to pull an image from a repository instead of building each run. While this doesn't cache the image, it is significantly faster. This can be achieved by editing your flow to look like the following:

      - name: 'Automated Version Bump'
        id: version-bump
        uses: 'docker://phips28/gh-action-bump-version:master'
        with:
          tag-prefix: 'v'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Please note the docker:// prefix to the uses: statement, coupled with the change from @master to :master in order to convert the name to a valid image name.

I have opened up a PR on that repository with a suggested fix :^)

Original Response

A very good question, and one that little information can be found on in the official documents (Though GitHub acknowledge the delay in their docs).

GitHub Staff response

I had a search for you and managed to find an article from September 2019 on the GitHub community forum about this exact topic. It inevitably linked to this article from July 2019.

There is a wonderful explanation about how building each time will still utilise the docker build cache, reducing times, yet allow for flexibility in terms of using the latest version of the base image etc, etc.

There is a proposed solution if you aren't bothered about the flexibility of updates, and just want the shortest build time possible, though I am not certain if that syntax is still valid currently:

But let’s say that I don’t want my Action to even evaluate the Dockerfile every time the Action runs, because I want the absolute fastest runtime possible. I want a predefined Docker container to be spun up and get right to work. You can have that too! If you create a Docker image and upload it to Docker Hub or another public registry, you can instruct your Action to use that Docker image specifically using the docker:// form in the uses key. See the GitHub Actions documentation 72 for specifics. This requires a bit more up-front work and maintenance, but if you don’t need the flexibility that the above Dockerfile evaluation system provides, then it may be worth the tradeoff.

Unfortunately the link to the Github actions documentation is broken, but this does suggest that the author of the action could allow this behaviour if they modified their action

Alternate Ideas

If you require the ability to control the cache on the executor host (to truly cache an image), then you may think of considering hosting your own GitHub runner, as you would have total control over the images there. Though I imagine this is potentially a detterent given that GitHub actions is largely a free service (with limits, and this is perhaps one of them!)

You might want to consider adding a task that utilises the file cache action, and attempting to export the gh-action-bump-version to a file contents through docker commit or docker save, and then reinflate it on your next run. However, this introduces complexity and may not save you time in the long run.. Edit: This is a horrible idea now that we know actions can support pulling images from registries.

I hope this helps you, and anyone else searching for more information đź‘Ť

like image 100
TheQueenIsDead Avatar answered Sep 22 '22 21:09

TheQueenIsDead