GNS3 / gns3-registry

GNS3 devices registry
GNU General Public License v3.0
323 stars 387 forks source link

Docker build: Support building and uploading to multiple registries #775

Closed b-ehlers closed 1 year ago

b-ehlers commented 1 year ago

Finally the PR to support building and uploading Docker images to multiple registries.

The user interface got only a small change, the environment variable DOCKER_REPOSITORY may now contain a list of repositories. The name-only docker images are built and uploaded to all repositories in this list.

First the fine print: This PR implements method #​3 from issue #772, this does just the basic stuff. I suggest to use it for some time and when we notice some limitations, it can be extended. The uploaded docker image to multiple repositories is only identical, when building in the same workflow run. When the docker image is built at different times, the packages that got installed during the docker build may have changed and then the docker image differs. That only happens, when a docker image is current in one repository and not in the other one. Normally the docker image is built, when the Dockerfile gets updated, then both repositories are not current and both got simultaneously updated. The hard way to force equal images is to do a manual build with --all as the image list. This rebuilds all images. Fixed by commit https://github.com/GNS3/gns3-registry/pull/775/commits/c47148cfd810f69565403a7a565ef577f6acdcbc.

The workflow in this PR still uses only the DockerHub registry.

If you want to enable both the DockerHub and the GitHub Container Registry for building and uploading:

The change would look like this:

--- .github/workflows/build-docker-images.yml.orig  2023-06-12 15:29:54.806133680 +0200
+++ .github/workflows/build-docker-images.yml   2023-06-12 15:38:11.243275102 +0200
@@ -46,7 +46,7 @@
       - name: Login to GitHub Container Registry
         # https://github.com/marketplace/actions/docker-login
         # set the condition depending on whether you want to login to ghcr.io.
-        if: false
+        if: true
         uses: docker/login-action@v2
         with:
           registry: ghcr.io
@@ -60,13 +60,13 @@
         env:
           # DOCKER_REPOSITORY - Repository for name-only images
           # DockerHub:
-          DOCKER_REPOSITORY: ${{ secrets.DOCKERHUB_REPOSITORY }}
+          #DOCKER_REPOSITORY: ${{ secrets.DOCKERHUB_REPOSITORY }}
           # GitHub Container Registry:
           #DOCKER_REPOSITORY: ghcr.io/${{ github.repository_owner }}
           # Both DockerHub and GitHub Container Registry:
-          #DOCKER_REPOSITORY: >-
-          #  ${{ secrets.DOCKERHUB_REPOSITORY }}
-          #  ghcr.io/${{ github.repository_owner }}
+          DOCKER_REPOSITORY: >-
+            ${{ secrets.DOCKERHUB_REPOSITORY }}
+            ghcr.io/${{ github.repository_owner }}
           #
           # Variables whose name are starting with "DOCKER_LOGIN"
           # contain the user/password for a docker registry.
b-ehlers commented 1 year ago

Update: Changed build strategy for multiple repositories, the uploaded docker images are now identical.

Previously: Target is tested for changes on first repository. When changed, it got updated. Then second repository is tested and when changed, it got updated. Both repositories were processed independently. In some cases that can lead to slightly different images in the repositories.

Now: Target is tested for changes on first repository, then on second repository. If the target image in any repository has changed, the image in all repositories get updated. So all repositories get identical images.

I'm quite pleased with this solution, that should solve #772.