Repository navigation
133 lines (122 loc) · 4.51 KB
/
Copy pathdocker-node.yml
File metadata and controls
133 lines (122 loc) · 4.51 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# ============================================================
# 节点端 Docker 镜像自动构建并发布到 GHCR
# ============================================================
# 触发条件:
# 1. 推送到 main 分支且 node/ 目录有变更
# 2. 推送 v* 格式的 tag(如 v1.0.0)
# 3. 手动触发
#
# 发布产物:
# - ghcr.io/<owner>/serverstatus-node:latest (main 分支)
# - ghcr.io/<owner>/serverstatus-node:<tag> (版本 tag,如 v1.0.0)
# - ghcr.io/<owner>/serverstatus-node:1.0 (主次版本号)
# - ghcr.io/<owner>/serverstatus-node:sha-<short> (commit SHA 前12位)
# ============================================================
name: Build & Publish Node Docker Image
on:
push:
branches:
- main
- master
paths:
- "node/**"
- ".github/workflows/docker-node.yml"
tags:
- "v*"
workflow_dispatch:
inputs:
reason:
description: "构建原因"
required: false
default: "manual"
env:
REGISTRY: ghcr.io
# 镜像名:小写仓库所有者 + 仓库名 + -node
# GitHub Actions 中 ${{ github.repository }} 格式为 owner/repo
IMAGE_NAME: ${{ github.repository }}-node
# 同一分支/tag 的新构建取消旧的
concurrency:
group: docker-node-${{ github.ref }}
cancel-in-progress: true
# GHCR 推送需要写入 packages 权限
permissions:
contents: read
packages: write
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
# ----------------------------------------------------
# 1. 检出代码
# ----------------------------------------------------
- name: Checkout
uses: actions/checkout@v4
# ----------------------------------------------------
# 2. 提取镜像元数据(tags + labels)
# ----------------------------------------------------
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# 生成的 tag 策略:
# type=raw: main 分支 → latest
# type=semver: v1.0.0 → 1.0.0, 1.0, 1
# type=sha: 每次构建 → sha-abc123def456
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha,prefix=sha-,format=short
labels: |
org.opencontainers.image.title=MC服务器状态监测-节点端
org.opencontainers.image.description=Minecraft Server Status Monitor - Node
org.opencontainers.image.licenses=MIT
# ----------------------------------------------------
# 3. 登录 GHCR
# ----------------------------------------------------
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# ----------------------------------------------------
# 4. 设置 Docker Buildx(支持多平台 + 缓存)
# ----------------------------------------------------
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# ----------------------------------------------------
# 5. 构建并推送镜像
# ----------------------------------------------------
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: node/Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# GitHub Actions 缓存加速后续构建
cache-from: type=gha
cache-to: type=gha,mode=max
# 构建参数透传
build-args: |
BUILD_DATE=${{ steps.meta.outputs.date }}
# ----------------------------------------------------
# 6. 输出镜像摘要
# ----------------------------------------------------
- name: Image digest
run: |
echo "✅ 镜像构建并推送成功"
echo ""
echo "镜像地址:"
echo " ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"
echo ""
echo "Tags:"
echo "${{ steps.meta.outputs.tags }}" | sed 's/^/ /'
echo ""
echo "Digest:"
echo " ${{ steps.meta.outputs.digest }}"