Update github-image.yml

This commit is contained in:
J.K.SAGE 2024-01-31 14:04:31 +08:00 committed by GitHub
parent 10d1b46cfd
commit e85252cbe1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,60 +1,65 @@
# This workflow uses actions that are not certified by GitHub. # 工作流名称
# They are provided by a third-party and are governed by name: Build Docker Image
# separate terms of service, privacy policy, and support
# documentation.
# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.
# Create a Docker image and push it to ghcr.io whenever a new version tag is pushed
name: GHCR Image CI
# push tag 时触发执行
on: on:
push: push:
tags: tags:
- "v*.*.*" - v*
# 定义环境变量, 后面会使用
# 定义 APP_NAME 用于 docker build-args
# 定义 DOCKERHUB_REPO 标记 docker hub repo 名称
env: env:
REGISTRY: ghcr.io APP_NAME: pairdrop
IMAGE_NAME: ${{ github.repository }} DOCKERHUB_REPO: sage417/pairdrop
jobs: jobs:
build-and-push-image: main:
# 在 Ubuntu 上运行
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps: steps:
- name: Checkout repository # git checkout 代码
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 - name: Checkout
uses: actions/checkout@v4
- name: Setup qemu # 设置 QEMU, 后面 docker buildx 依赖此.
uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 # v3.0.0 - name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Setup Docker Buildx # 设置 Docker buildx, 方便构建 Multi platform 镜像
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to the Container registry # 登录 docker hub
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 - name: Login to DockerHub
uses: docker/login-action@v1
with: with:
registry: ${{ env.REGISTRY }} # GitHub Repo => Settings => Secrets 增加 docker hub 登录密钥信息
username: ${{ github.actor }} # DOCKERHUB_USERNAME 是 docker hub 账号名.
password: ${{ secrets.GITHUB_TOKEN }} # DOCKERHUB_TOKEN: docker hub => Account Setting => Security 创建.
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker - name: Extract metadata (tags, labels) for Docker
id: meta id: meta
uses: docker/metadata-action@31cebacef4805868f9ce9a0cb03ee36c32df2ac4 # v5.3.0 uses: docker/metadata-action@31cebacef4805868f9ce9a0cb03ee36c32df2ac4 # v5.3.0
with: with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} images: ${{ env.DOCKERHUB_REPO }}/${{ env.APP_NAME }}
# 构建 Docker 并推送到 Docker hub
- name: Build and push Docker image - name: Build and push
uses: docker/build-push-action@4a13e500e55cf31b7a5d59a38ab2040ab0f42f56 # v5.1.0 id: docker_build
uses: docker/build-push-action@v5
with: with:
context: . # 是否 docker push
platforms: linux/amd64,linux/arm64
push: true push: true
tags: ${{ steps.meta.outputs.tags }} # 生成多平台镜像, see https://github.com/docker-library/bashbrew/blob/v0.1.1/architecture/oci-platform.go
platforms: |
linux/arm/v6
linux/arm/v7
linux/arm64/v8
# docker build arg, 注入 APP_NAME/APP_VERSION
build-args: |
APP_NAME=${{ env.APP_NAME }}
APP_VERSION=${{ env.APP_VERSION }}
# 生成两个 docker tag: ${APP_VERSION} 和 latest
tags: |
${{ env.DOCKERHUB_REPO }}:latest
${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }} labels: ${{ steps.meta.outputs.labels }}