Publish a Docker image as Github package (Container Registry)

Publish a Docker image as Github package (Container Registry)

Let's say you want to distribute your #docker image via #github packages (Github Container Registry ). It is very straightforward... Just add a github action file in `\your_repo_dir\.github\workflows\docker-image-release.yml` with the following content.

name: Build and Publish Docker Image to GitHub Packages

on:
  push:
    branches:
      - release

jobs:
  docker:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

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

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io  # GitHub Container Registry
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: ghcr.io/${{ github.repository }}:${{ github.sha }}           

This GitHub Action workflow is triggered every time you push to the release branch of your repository.

The workflow checks out your code, sets up Docker Buildx, logs into the GitHub Container Registry using your GitHub account's username and token, and then builds and pushes a Docker image to the GitHub Container Registry. The Docker image is tagged with the SHA of the commit that triggered the workflow.

Please note that in order to use the GitHub Packages, you need to have the appropriate permissions and the GITHUB_TOKEN secret available in your repository (this should exist by default).

要查看或添加评论,请登录

社区洞察

其他会员也浏览了