83 lines
3.1 KiB
YAML
83 lines
3.1 KiB
YAML
name: Publish Docker image to GHCR
|
||
|
||
on:
|
||
push:
|
||
branches: [master]
|
||
paths:
|
||
- 'Dockerfile'
|
||
- 'server.js'
|
||
- 'package.json'
|
||
- 'app/**'
|
||
- '.github/workflows/docker-publish.yml'
|
||
workflow_dispatch:
|
||
|
||
permissions:
|
||
contents: read
|
||
packages: write
|
||
|
||
jobs:
|
||
build-push:
|
||
runs-on: ubuntu-latest
|
||
steps:
|
||
- name: Checkout
|
||
uses: actions/checkout@v4
|
||
|
||
- name: Log in to GitHub Container Registry
|
||
uses: docker/login-action@v3
|
||
with:
|
||
registry: ghcr.io
|
||
username: ${{ github.actor }}
|
||
password: ${{ secrets.GITHUB_TOKEN }}
|
||
|
||
- name: Build and push
|
||
uses: docker/build-push-action@v6
|
||
with:
|
||
context: .
|
||
push: true
|
||
tags: |
|
||
ghcr.io/${{ github.repository_owner }}/moyu-qile:latest
|
||
ghcr.io/${{ github.repository_owner }}/moyu-qile:1.0.0
|
||
ghcr.io/${{ github.repository_owner }}/moyu-qile:${{ github.run_number }}
|
||
ghcr.io/${{ github.repository_owner }}/moyu-qile:20261008
|
||
|
||
- name: Make package public (anonymous pull)
|
||
continue-on-error: true
|
||
env:
|
||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||
run: |
|
||
gh api -X PATCH "/user/packages/container/moyu-qile" -f visibility=public \
|
||
&& echo "✅ 已设为 public" \
|
||
|| echo "⚠️ 未能通过 API 设为 public(若匿名拉取失败,需在 GitHub → Packages → moyu-qile 手动改为 Public)"
|
||
|
||
- name: Verify anonymous pull
|
||
continue-on-error: true
|
||
run: |
|
||
docker logout ghcr.io
|
||
docker manifest inspect ghcr.io/${{ github.repository_owner }}/moyu-qile:latest >/dev/null \
|
||
&& echo "✅ 匿名可拉取(public 生效)" \
|
||
|| echo "⚠️ 匿名仍不可拉取(包还是 private)"
|
||
|
||
# —— Docker Hub 双保险:secrets 未配置时输出跳过并正常退出,不影响 ghcr 流水线
|
||
# 注意:secrets 不能出现在 if 条件里(会触发 workflow file issue),判断放 shell 内做
|
||
- name: Mirror image to Docker Hub
|
||
env:
|
||
DH_USER: ${{ secrets.DOCKERHUB_USERNAME }}
|
||
DH_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
|
||
run: |
|
||
if [ -z "$DH_USER" ] || [ -z "$DH_TOKEN" ]; then
|
||
echo "⏭️ 未配置 DOCKERHUB_USERNAME / DOCKERHUB_TOKEN,跳过 Docker Hub 同步"
|
||
exit 0
|
||
fi
|
||
echo "$DH_TOKEN" | docker login docker.io -u "$DH_USER" --password-stdin
|
||
docker buildx imagetools create \
|
||
-t "$DH_USER/moyu-qile:latest" \
|
||
-t "$DH_USER/moyu-qile:1.0.0" \
|
||
-t "$DH_USER/moyu-qile:${{ github.run_number }}" \
|
||
-t "$DH_USER/moyu-qile:20261008" \
|
||
"ghcr.io/${{ github.repository_owner }}/moyu-qile:latest"
|
||
echo "✅ 已镜像到 Docker Hub(同 digest,不重新构建)"
|
||
docker logout docker.io
|
||
docker manifest inspect "$DH_USER/moyu-qile:latest" >/dev/null \
|
||
&& echo "✅ Docker Hub 匿名可拉取" \
|
||
|| echo "⚠️ Docker Hub 匿名拉取失败(仓库可能还是 private,需在 hub 网站设为 Public)"
|