准备

请在github的设置页面上创建一个token,并确保有以下的权限:

  • repo
  • read:packages
  • write:packages

请保存好该token,因为github将隐藏该值

在github的仓库的secrets设置页面(例:https://github.com/{your_username}/{your_repository_name}/settings/secrets/actions)里创建一个名为GHCR_PAT的secrets

secrets设置页面

创建GitHub Action文件

.github/workflow中创建一个build-and-publish.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
name: Build and Publish

on:
push:
branches: [ master ]

jobs:
build-and-push-docker-image:
name: Build Docker image and push to repositories
runs-on: ubuntu-latest

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

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

- name: Login to Github Packages
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GHCR_PAT }}

- name: Build image and push to GitHub Container Registry
uses: docker/build-push-action@v2
id: docker_build
with:
context: .
tags: |
# 将usernmae和repository改成你自己的github账号和仓库名
ghcr.io/{usernmae}/{repository}:${{ github.sha }}
# 当且只有运行在master分支时才需要推送到github容器仓库
push: ${{ github.ref == 'refs/heads/master' }}

- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}

默认推送的镜像是私有,想要公开,可以在
https://github.com/users/{your_username}/packages/container/{your_repository_name}/settings下面的Danger Zone中的Change package visibility设置成public还是private

参考资料

  1. How to build and push Docker image with GitHub actions?
  2. Hoteler