Repository navigation
126 lines (114 loc) · 4.23 KB
/
Copy pathci.yml
File metadata and controls
126 lines (114 loc) · 4.23 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
name: CI & Docker Publish
on:
push:
branches: [ main ]
tags: [ 'v*' ] # 打 tag 时触发发布
pull_request:
branches: [ main ]
workflow_dispatch:
inputs:
publish_image:
description: '是否推送 Docker 镜像到 Docker Hub'
required: true
default: 'false'
type: choice
options:
- 'false'
- 'true'
platforms:
description: 'Docker 构建平台。日常验证建议 linux/amd64,正式发布用 linux/amd64,linux/arm64'
required: true
default: 'linux/amd64'
type: string
# 高频提交场景下,只保留同一分支/PR 的最新一次 CI,避免排队浪费几小时。
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
# ============================================================
# Job 1: Node.js 依赖安装验证
# ============================================================
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
# ============================================================
# Job 2: Docker 构建 & 发布
# ============================================================
docker:
runs-on: ubuntu-latest
needs: validate
# Docker 多架构构建非常耗时:
# - tag(v*):正式发布,构建并推送多架构镜像
# - workflow_dispatch:手动按需构建/推送
# - PR 且触及容器/原生依赖关键文件:只做 amd64 构建验证,不推送
# main 分支普通 push 不再自动构建 Docker,避免一天几十次全量编译。
if: |
startsWith(github.ref, 'refs/tags/v') ||
github.event_name == 'workflow_dispatch' ||
github.event_name == 'pull_request'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up QEMU (多架构支持)
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# 仅在需要推送镜像时登录 Docker Hub
- name: Login to Docker Hub
if: startsWith(github.ref, 'refs/tags/v') || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish_image == 'true')
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# 自动生成镜像标签
# tag v1.0.0 → latest + 1.0.0 + 1.0 + 1
- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: lioensky/vcptoolbox
tags: |
# 语义化版本标签
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
# main 分支推送时打 latest
type=raw,value=latest,enable={{is_default_branch}}
- name: Resolve Docker build settings
id: docker_settings
shell: bash
run: |
if [[ "${GITHUB_REF}" == refs/tags/v* ]]; then
echo "platforms=linux/amd64,linux/arm64" >> "$GITHUB_OUTPUT"
echo "push=true" >> "$GITHUB_OUTPUT"
elif [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then
echo "platforms=${{ github.event.inputs.platforms }}" >> "$GITHUB_OUTPUT"
echo "push=${{ github.event.inputs.publish_image }}" >> "$GITHUB_OUTPUT"
else
echo "platforms=linux/amd64" >> "$GITHUB_OUTPUT"
echo "push=false" >> "$GITHUB_OUTPUT"
fi
# 构建并推送:
# - tag:正式多架构发布
# - 手动 workflow_dispatch:按输入决定平台与是否推送
# - PR:仅 amd64 构建验证,不推送
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
platforms: ${{ steps.docker_settings.outputs.platforms }}
push: ${{ steps.docker_settings.outputs.push == 'true' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max