Some checks are pending
Docker Image CI / build-and-push-image (push) Waiting to run
68 lines
2.0 KiB
YAML
68 lines
2.0 KiB
YAML
name: Docker Image CI
|
|
|
|
on:
|
|
push:
|
|
branches: [ "master", "main" ]
|
|
# 当发布新版本时触发
|
|
tags: [ 'v*.*.*' ]
|
|
pull_request:
|
|
branches: [ "master", "main" ]
|
|
|
|
env:
|
|
# GitHub Container Registry 的地址
|
|
REGISTRY: ghcr.io
|
|
# 镜像名称,默认为 GitHub 用户名/仓库名
|
|
IMAGE_NAME: ${{ github.repository }}
|
|
|
|
jobs:
|
|
build-and-push-image:
|
|
runs-on: ubuntu-latest
|
|
permissions:
|
|
contents: read
|
|
packages: write
|
|
# 如果需要签名生成的镜像,可以使用 id-token: write
|
|
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
|
|
# 设置 Docker Buildx 用于构建多平台镜像 (可选)
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
# 登录到 Docker 镜像仓库
|
|
# 如果只是在 PR 中测试构建,则跳过登录
|
|
- name: Log in to the Container registry
|
|
if: github.event_name != 'pull_request'
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
# 提取 Docker 镜像的元数据(标签、注释等)
|
|
- name: Extract metadata (tags, labels) for Docker
|
|
id: meta
|
|
uses: docker/metadata-action@v5
|
|
with:
|
|
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
|
tags: |
|
|
type=schedule
|
|
type=ref,event=branch
|
|
type=ref,event=pr
|
|
type=semver,pattern={{version}}
|
|
type=semver,pattern={{major}}.{{minor}}
|
|
type=semver,pattern={{major}}
|
|
type=sha
|
|
type=raw,value=latest,enable={{is_default_branch}}
|
|
|
|
# 构建并推送 Docker 镜像
|
|
- name: Build and push Docker image
|
|
uses: docker/build-push-action@v5
|
|
with:
|
|
context: .
|
|
push: ${{ github.event_name != 'pull_request' }}
|
|
tags: ${{ steps.meta.outputs.tags }}
|
|
labels: ${{ steps.meta.outputs.labels }}
|
|
cache-from: type=gha
|
|
cache-to: type=gha,mode=max |