Automate GitHub Container Registry deployments with GitHub Actions
Creating a container image
Let us create a very simple container image.
What does this image do?
For every argument this image receives, it outputs the argument to the screen.
Just like an echo command.
We use Alpine because the image sizes are small and hence downloads are faster.
Of course your image can be highly complex.
Writing a GitHub Action
Let me first show you the entire GitHub Actions and then we can go through it step by step.
Looks simple right?
Triggering the action
Our current action has been designed to simplify triggering via workflow_dispatch using GitHub CLI or using the GitHub Web based interface.
Again the focus here is on complete simplicity. You can trigger on :-
- Push
- Pull Request
- Release
- On a schedule (Build image every week)
- etc.
Thus making it easy to configure.
Setting things up
Based on the repository, we can say
- Check out the repository
- setup-qemu action can be useful if you want to add emulation support with QEMU to be able to build against more platforms.
- setup-buildx action will create and boot a builder using by default the docker-container builder driver. This is not required but recommended using it to be able to build multi-platform images, export cache, etc.
Again both these steps are not mandatory. In our case, neither are required. But it is recommended that you use them in case you do indeed need them. Step 2 can be skipped if not needed.
Logging in
We can login to GitHub Container Registry using
Notice how we do not have to configure a single secret in the GitHub Repository and how everything is handled for us. The entire process is both seamless and effortless.
This convenience is one of the major advantages of using GitHub Actions with GitHub Container Registry.
Building
As you can see building is fairly easy. All we have to do is specify the tag of the image. This is similar to how it would have worked offline as well.
Deploy with release tagged
To deploy with the release tagged, instead of a fixed string latest in a repo triggered by on release, you can use
ghcr.io/${{ github.repository }}/echo:${{ github.event.release.tag_name }}
What is the tag format?
- The tag starts with ghcr.io which is the GitHub Container Registry website.
- It then contains the name of the repository. For example:- github/example.
- And then it will contain the name of our image.
This allows us to deploy multiple images to the same repository.
Further Configuration
For further configuration, you can check out the documentation page of the GitHub Action itself.
The page deals with adding support for Caching etc. which you might need based on your project requirements.
Usage
You can use this docker image easily by