332 lines
12 KiB
YAML
332 lines
12 KiB
YAML
name: 构建并发布 Docker 镜像
|
||
|
||
on:
|
||
push:
|
||
branches:
|
||
- main
|
||
- dev
|
||
workflow_dispatch:
|
||
workflow_call:
|
||
inputs:
|
||
app_version:
|
||
description: 正式发布版本号(不含 v)
|
||
required: true
|
||
type: string
|
||
|
||
permissions:
|
||
contents: read
|
||
packages: write
|
||
|
||
concurrency:
|
||
group: docker-publish-${{ github.ref }}
|
||
cancel-in-progress: true
|
||
|
||
jobs:
|
||
verify:
|
||
name: Docker 发布测试门禁
|
||
runs-on: ubuntu-24.04
|
||
outputs:
|
||
app_version: ${{ steps.version.outputs.app_version }}
|
||
build_time: ${{ steps.version.outputs.build_time }}
|
||
steps:
|
||
- name: 准备构建版本
|
||
id: version
|
||
shell: bash
|
||
run: |
|
||
set -Eeuo pipefail
|
||
if [ -n "${{ inputs.app_version }}" ]; then
|
||
app_version="${{ inputs.app_version }}"
|
||
else
|
||
app_version="${GITHUB_REF_NAME}-${GITHUB_SHA::12}"
|
||
fi
|
||
if [ -n "${{ inputs.app_version }}" ] && [[ ! "$app_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
|
||
echo "正式版本必须符合 1.2.3 格式:$app_version" >&2
|
||
exit 1
|
||
fi
|
||
echo "app_version=$app_version" >> "$GITHUB_OUTPUT"
|
||
echo "build_time=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_OUTPUT"
|
||
- name: 检出代码
|
||
uses: actions/checkout@v6
|
||
|
||
- name: 配置 Go
|
||
uses: actions/setup-go@v7
|
||
with:
|
||
go-version-file: go.mod
|
||
cache: true
|
||
|
||
- name: 配置 Node.js
|
||
uses: actions/setup-node@v6
|
||
with:
|
||
node-version: 24
|
||
cache: npm
|
||
cache-dependency-path: frontend/package-lock.json
|
||
|
||
- name: 验证前端
|
||
shell: bash
|
||
run: |
|
||
set -Eeuo pipefail
|
||
npm ci --prefix frontend
|
||
npm run typecheck --prefix frontend
|
||
npm test --prefix frontend
|
||
npm run build --prefix frontend
|
||
|
||
- name: 验证 Go 服务
|
||
run: |
|
||
go vet ./...
|
||
go test ./...
|
||
|
||
build:
|
||
name: 构建镜像 (${{ matrix.arch }})
|
||
needs: verify
|
||
runs-on: ${{ matrix.runner }}
|
||
strategy:
|
||
fail-fast: false
|
||
matrix:
|
||
include:
|
||
- arch: amd64
|
||
runner: ubuntu-24.04
|
||
platform: linux/amd64
|
||
playwright_platform: debian13-x64
|
||
- arch: arm64
|
||
runner: ubuntu-24.04-arm
|
||
platform: linux/arm64
|
||
playwright_platform: debian13-arm64
|
||
steps:
|
||
- name: 检出代码
|
||
uses: actions/checkout@v6
|
||
|
||
- name: 校验 runner 原生架构
|
||
shell: bash
|
||
env:
|
||
EXPECTED_ARCH: ${{ matrix.arch }}
|
||
run: |
|
||
set -Eeuo pipefail
|
||
case "$(uname -m)" in
|
||
x86_64) actual_arch=amd64 ;;
|
||
aarch64|arm64) actual_arch=arm64 ;;
|
||
*) echo "不支持的 runner 架构:$(uname -m)" >&2; exit 1 ;;
|
||
esac
|
||
if [ "$actual_arch" != "$EXPECTED_ARCH" ]; then
|
||
echo "runner 架构 $actual_arch 与目标架构 $EXPECTED_ARCH 不一致,拒绝使用模拟器构建。" >&2
|
||
exit 1
|
||
fi
|
||
|
||
- name: 配置 Go
|
||
uses: actions/setup-go@v7
|
||
with:
|
||
go-version-file: go.mod
|
||
cache: true
|
||
|
||
- name: 配置 Node.js
|
||
uses: actions/setup-node@v6
|
||
with:
|
||
node-version: 24
|
||
|
||
- name: 恢复 Docker Chromium runtime 缓存
|
||
id: cache-docker-playwright
|
||
uses: actions/cache/restore@v5
|
||
with:
|
||
path: .docker/playwright-runtime/${{ matrix.arch }}
|
||
key: ydisks-playwright-runtime-v6-docker-${{ matrix.arch }}-${{ matrix.playwright_platform }}-${{ hashFiles('go.mod', 'go.sum', 'cmd/browser-install/**') }}
|
||
|
||
- name: 准备 Docker Chromium runtime
|
||
if: steps.cache-docker-playwright.outputs.cache-hit != 'true'
|
||
shell: bash
|
||
env:
|
||
GOARCH: ${{ matrix.arch }}
|
||
PLAYWRIGHT_HOST_PLATFORM_OVERRIDE: ${{ matrix.playwright_platform }}
|
||
run: |
|
||
set -Eeuo pipefail
|
||
mkdir -p ".docker/playwright-runtime/$GOARCH/playwright-driver" ".docker/playwright-runtime/$GOARCH/playwright-browsers"
|
||
GOOS=linux GOARCH="$GOARCH" CGO_ENABLED=0 go build \
|
||
-trimpath -ldflags='-s -w' \
|
||
-o "$RUNNER_TEMP/browser-install" ./cmd/browser-install
|
||
"$RUNNER_TEMP/browser-install" \
|
||
-driver-dir "$PWD/.docker/playwright-runtime/$GOARCH/playwright-driver" \
|
||
-browser-dir "$PWD/.docker/playwright-runtime/$GOARCH/playwright-browsers"
|
||
|
||
- name: 校验 Docker Chromium runtime
|
||
shell: bash
|
||
env:
|
||
GOARCH: ${{ matrix.arch }}
|
||
run: |
|
||
set -Eeuo pipefail
|
||
runtime_dir=".docker/playwright-runtime/$GOARCH"
|
||
test -x "$runtime_dir/playwright-driver/node"
|
||
test -f "$runtime_dir/playwright-driver/package/cli.js"
|
||
chromium_path="$(find "$runtime_dir/playwright-browsers" -type f \
|
||
\( -name chrome -o -name headless_shell -o -name chrome-headless-shell \) -perm -111 -print -quit)"
|
||
test -n "$chromium_path"
|
||
chromium_revision="$(RUNTIME_SPEC="$PWD/$runtime_dir/playwright-driver/package/browsers.json" node -e 'const b=require(process.env.RUNTIME_SPEC).browsers; console.log(b.find(x=>x.name==="chromium").revision)')"
|
||
headless_revision="$(RUNTIME_SPEC="$PWD/$runtime_dir/playwright-driver/package/browsers.json" node -e 'const b=require(process.env.RUNTIME_SPEC).browsers; console.log(b.find(x=>x.name==="chromium-headless-shell").revision)')"
|
||
test -d "$runtime_dir/playwright-browsers/chromium-$chromium_revision"
|
||
test -d "$runtime_dir/playwright-browsers/chromium_headless_shell-$headless_revision"
|
||
|
||
- name: 保存 Docker Chromium runtime 缓存
|
||
if: steps.cache-docker-playwright.outputs.cache-hit != 'true'
|
||
uses: actions/cache/save@v5
|
||
with:
|
||
path: .docker/playwright-runtime/${{ matrix.arch }}
|
||
key: ${{ steps.cache-docker-playwright.outputs.cache-primary-key }}
|
||
|
||
- name: 配置 Docker Buildx
|
||
uses: docker/setup-buildx-action@v4
|
||
|
||
- name: 登录 GitHub Container Registry
|
||
uses: docker/login-action@v4
|
||
with:
|
||
registry: ghcr.io
|
||
username: ${{ github.actor }}
|
||
password: ${{ secrets.GITHUB_TOKEN }}
|
||
|
||
- name: 生成小写镜像名称
|
||
id: image
|
||
shell: bash
|
||
run: echo "name=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT"
|
||
|
||
- name: 构建并推送原生架构摘要
|
||
id: build
|
||
uses: docker/build-push-action@v7
|
||
with:
|
||
context: .
|
||
file: Dockerfile.debian13
|
||
platforms: ${{ matrix.platform }}
|
||
build-args: |
|
||
APP_VERSION=${{ needs.verify.outputs.app_version }}
|
||
VCS_REF=${{ github.sha }}
|
||
BUILD_TIME=${{ needs.verify.outputs.build_time }}
|
||
outputs: type=image,name=${{ steps.image.outputs.name }},push-by-digest=true,name-canonical=true,push=true
|
||
cache-from: type=gha,scope=ydisks-docker-${{ matrix.arch }}
|
||
cache-to: type=gha,mode=max,scope=ydisks-docker-${{ matrix.arch }}
|
||
provenance: mode=max
|
||
sbom: true
|
||
|
||
- name: 验证待发布镜像中的 Chromium 与服务
|
||
shell: bash
|
||
env:
|
||
IMAGE_NAME: ${{ steps.image.outputs.name }}
|
||
IMAGE_DIGEST: ${{ steps.build.outputs.digest }}
|
||
run: |
|
||
set -Eeuo pipefail
|
||
image_ref="$IMAGE_NAME@$IMAGE_DIGEST"
|
||
docker pull "$image_ref"
|
||
|
||
docker run --rm --entrypoint sh "$image_ref" -ec '
|
||
chromium_path="$(find /ms-playwright/chromium-* -type f \
|
||
-name chrome -perm -111 -print -quit)"
|
||
test -n "$chromium_path"
|
||
timeout 45 "$chromium_path" \
|
||
--headless --no-sandbox --disable-gpu --disable-dev-shm-usage \
|
||
--dump-dom about:blank | grep -q "<html"
|
||
'
|
||
|
||
container_id="$(docker run -d -p 127.0.0.1::59188 "$image_ref")"
|
||
cleanup() {
|
||
docker logs "$container_id" || true
|
||
docker rm -f "$container_id" >/dev/null 2>&1 || true
|
||
}
|
||
trap cleanup EXIT
|
||
host_port="$(docker port "$container_id" 59188/tcp | sed -n 's/.*://p' | head -n 1)"
|
||
test -n "$host_port"
|
||
for attempt in $(seq 1 60); do
|
||
if health="$(curl --fail --silent --show-error "http://127.0.0.1:$host_port/health" 2>/dev/null)"; then
|
||
printf '%s' "$health" | grep -q '"status":"ok"'
|
||
printf '%s' "$health" | grep -q '"database":"ok"'
|
||
exit 0
|
||
fi
|
||
if ! docker inspect -f '{{.State.Running}}' "$container_id" | grep -q true; then
|
||
echo '待发布镜像中的服务提前退出。' >&2
|
||
exit 1
|
||
fi
|
||
sleep 1
|
||
done
|
||
echo '待发布镜像未能在 60 秒内通过健康检查。' >&2
|
||
exit 1
|
||
|
||
- name: 保存镜像摘要
|
||
shell: bash
|
||
env:
|
||
IMAGE_DIGEST: ${{ steps.build.outputs.digest }}
|
||
run: |
|
||
set -Eeuo pipefail
|
||
mkdir -p dist/digests
|
||
touch "dist/digests/${IMAGE_DIGEST#sha256:}"
|
||
|
||
- name: 上传镜像摘要
|
||
uses: actions/upload-artifact@v7
|
||
with:
|
||
name: ydisks-docker-digest-${{ matrix.arch }}
|
||
path: dist/digests/*
|
||
if-no-files-found: error
|
||
retention-days: 30
|
||
|
||
publish:
|
||
name: 合并多架构镜像
|
||
if: inputs.app_version == ''
|
||
needs: build
|
||
runs-on: ubuntu-24.04
|
||
steps:
|
||
- name: 配置 Docker Buildx
|
||
uses: docker/setup-buildx-action@v4
|
||
|
||
- name: 下载镜像摘要
|
||
uses: actions/download-artifact@v8
|
||
with:
|
||
pattern: ydisks-docker-digest-*
|
||
path: dist/digests
|
||
merge-multiple: true
|
||
|
||
- name: 登录 GitHub Container Registry
|
||
uses: docker/login-action@v4
|
||
with:
|
||
registry: ghcr.io
|
||
username: ${{ github.actor }}
|
||
password: ${{ secrets.GITHUB_TOKEN }}
|
||
|
||
- name: 生成小写镜像名称
|
||
id: image
|
||
shell: bash
|
||
run: echo "name=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT"
|
||
|
||
- name: 生成镜像标签
|
||
id: meta
|
||
uses: docker/metadata-action@v6
|
||
with:
|
||
images: ${{ steps.image.outputs.name }}
|
||
tags: |
|
||
type=sha,format=long
|
||
type=raw,value=main,enable=${{ github.ref == 'refs/heads/main' }}
|
||
type=raw,value=dev,enable=${{ github.ref == 'refs/heads/dev' }}
|
||
|
||
- name: 创建并推送多架构 manifest
|
||
shell: bash
|
||
env:
|
||
IMAGE_NAME: ${{ steps.image.outputs.name }}
|
||
IMAGE_TAGS: ${{ steps.meta.outputs.tags }}
|
||
run: |
|
||
set -Eeuo pipefail
|
||
mapfile -t digest_files < <(find dist/digests -maxdepth 1 -type f -print | sort)
|
||
if [ "${#digest_files[@]}" -ne 2 ]; then
|
||
echo "预期 2 个镜像摘要,实际 ${#digest_files[@]} 个。" >&2
|
||
exit 1
|
||
fi
|
||
digest_args=()
|
||
for digest_file in "${digest_files[@]}"; do
|
||
digest_args+=("$IMAGE_NAME@sha256:$(basename "$digest_file")")
|
||
done
|
||
tag_args=()
|
||
while IFS= read -r tag; do
|
||
[ -n "$tag" ] && tag_args+=(--tag "$tag")
|
||
done <<< "$IMAGE_TAGS"
|
||
docker buildx imagetools create "${tag_args[@]}" "${digest_args[@]}"
|
||
|
||
- name: 检查多架构 manifest
|
||
shell: bash
|
||
env:
|
||
IMAGE_TAGS: ${{ steps.meta.outputs.tags }}
|
||
run: |
|
||
set -Eeuo pipefail
|
||
first_tag="$(printf '%s\n' "$IMAGE_TAGS" | sed -n '/./{p;q;}')"
|
||
test -n "$first_tag"
|
||
docker buildx imagetools inspect "$first_tag"
|