Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publishing the build Docker image #18

Open
AlexanderWells-diamond opened this issue Oct 27, 2023 · 0 comments
Open

Publishing the build Docker image #18

AlexanderWells-diamond opened this issue Oct 27, 2023 · 0 comments

Comments

@AlexanderWells-diamond
Copy link

Is it possible to request that the CI be extended to publish the container images to a repository somewhere? GitHub itself has a Container Registry (GHCR) that integrates nicely with CI.

There exists some Github Actions that make this a fairly straightforward process. The main one would be build-push-action.

And, if it's helpful, here's a snippet of some template code to publish Python library container images to GHCR:

 - name: Log in to GitHub Docker Registry
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v3

      - name: Build and export to Docker local cache
        uses: docker/build-push-action@v5
        with:
        # Note build-args, context, file, and target must all match between this
        # step and the later build-push-action, otherwise the second build-push-action
        # will attempt to build the image again
          build-args: |
            PIP_OPTIONS=-r lockfiles/requirements.txt dist/*.whl
          context: artifacts/
          file: ./Dockerfile
          target: runtime
          load: true
          tags: ${{ env.TEST_TAG }}
          # If you have a long docker build (2+ minutes), uncomment the 
          # following to turn on caching. For short build times this 
          # makes it a little slower
          #cache-from: type=gha
          #cache-to: type=gha,mode=max

      - name: Test cli works in cached runtime image
        run: docker run docker.io/library/${{ env.TEST_TAG }} --version

      - name: Create tags for publishing image
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.IMAGE_REPOSITORY }}
          tags: |
            type=ref,event=tag
            type=raw,value=latest, enable=${{ github.ref_type == 'tag' }}
          # type=edge,branch=main
          # Add line above to generate image for every commit to given branch,
          # and uncomment the end of if clause in next step

      - name: Push cached image to container registry
        if: github.ref_type == 'tag' # || github.ref_name == 'main'
        uses: docker/build-push-action@v5
        # This does not build the image again, it will find the image in the 
        # Docker cache and publish it
        with:
        # Note build-args, context, file, and target must all match between this
        # step and the previous build-push-action, otherwise this step will 
        # attempt to build the image again
          build-args: |
            PIP_OPTIONS=-r lockfiles/requirements.txt dist/*.whl
          context: artifacts/
          file: ./Dockerfile
          target: runtime
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

Full file available here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant