قالب وردپرس درنا توس
Home / Tips and Tricks / How to set up automatic buildings for Docker images on GitHub – CloudSavvy IT

How to set up automatic buildings for Docker images on GitHub – CloudSavvy IT



GitHub logo.

GitHub has a feature called GitHub Actions that runs automated builds, tests, and other scripts when you make changes to an archive. A practical use of this is to automatically build and operate Docker containers to a container register.

GitHub’s new container register

GitHub’s new container registry, called the GitHub Container Registry, is a little different from most registers like Docker Hub. It acts as an extension of GitHub packages, a package storage system that associates packages with their source repositories. Packages can be built and pushed from the repository, often automatically using a GitHub Actions pipeline.

GitHub Container Registry.

The GitHub Container Registry simply adds Docker-specific compatibility to GitHub packages, making it work as a container registry for driving docker pull and other CLI commands.

You do not need to publish to GitHub’s container registry – you can still publish to Docker Hub from an action with a specific configuration. However, the preventative measures work with GHCR out of the box, so it is much easier to set up.

How to set up automatic buildings for GitHub packages

To start, you need an archive. Even if you only publish packages, you still need a repo, as the format for GHCR is:

ghcr.io/username/repository/image:version

Set a repo and then click “Actions” to create a new action. Click “Publish Docker Container” under “More Continuous Integration Workflows”.

Set your repo.

This generates a startup template that needs some modification to work. First, the IMAGE_NAME variable must be changed to your picture name.

Change IMAGE_NAME variable to your image name.

Then, on line 39, you will find where it logs into GHCR.

run: echo "${{ secrets.CR_PAT }}" | docker login https://ghcr.io -u ${{ github.actor }} --password-stdin

Currently, the only authentication scheme supported is Personal Access Tokens (PAT), which is not good for security as they provide cross-account access. GitHub knows this and is working on a better solution for the future, but if you want to use GHCR from a GitHub Actions workflow in the meantime, you need to store a PAT in Secrets for your repository, because obviously just pasting this would be awful .

First, go to Settings> Developer Settings> Personal Access Token and create a new token. This token needs write:packages and delete:packages settings. Note that for some reason the selection of writing packages automatically marks “Full control of custody”, which you must deselect.

Create a new token with type: package and delete: package settings.

Then go over to the repository settings and create a new secret called CR_PAT, to match the action.

Create a new secret called CR_PAT.

Return to the action and click “Start Commit” to push it to the repository.

Click

Once engaged, it will trigger a workflow to run and build the package. You can monitor the status of all ongoing workflows under the “Actions” tab. It failed here because the standard actions expect tests to run, which this image did not have.

Monitor the status of all ongoing workflows below

Once it has succeeded, you should see the container in the register, under “Packages” on the repository’s main page or under the packages in your profile.

A successful package building.


Source link