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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
name: Release
on:
push:
tags:
- "v*"
workflow_dispatch:
inputs:
tag:
description: "Tag to release (e.g. v0.1.4)"
required: true
type: string
env:
CARGO_TERM_COLOR: always
# 架构 M-2:防止并发 release 导致缓存冲突 / crates.io 发布顺序不可控
concurrency:
group: release-${{ github.ref }}
cancel-in-progress: false
jobs:
release:
name: Create Release
runs-on: ubuntu-latest
# 安全 LOW-1 / 架构 L-2:permissions 收窄到 job 级别,最小权限原则
# id-token: write:crates.io trusted publishing(OIDC)换取短时发布凭证
permissions:
contents: write
id-token: write
steps:
- name: Checkout code
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ inputs.tag || github.ref }}
# 架构 H-2:支持 workflow_dispatch 手动触发,publish 失败时可重试
- name: Extract version from tag
id: version
env:
INPUT_TAG: ${{ inputs.tag }}
run: |
if [ -n "$INPUT_TAG" ]; then
TAG="$INPUT_TAG"
else
TAG="${GITHUB_REF#refs/tags/}"
fi
VERSION="${TAG#v}"
# 安全 H-1:版本号白名单校验,防止表达式注入(拒绝 shell 元字符)
if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[A-Za-z0-9.]+)?$ ]]; then
echo "ERROR: invalid version format: $VERSION"
exit 1
fi
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
echo "Extracted tag: $TAG"
echo "Extracted version: $VERSION"
# semver 预发布段(-rc.N 等)→ GitHub prerelease 标记;rc 不得抢占 Latest
- name: Derive prerelease flag
id: meta
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
if [[ "$VERSION" == *-* ]]; then
echo "PRERELEASE=true" >> "$GITHUB_OUTPUT"
else
echo "PRERELEASE=false" >> "$GITHUB_OUTPUT"
fi
# 架构 M-1 / 性能 M-1:版本验证前移,fail-fast 节约 CI 资源
- name: Verify Cargo.toml version matches tag
env:
TAG_VERSION: ${{ steps.version.outputs.version }}
run: |
CARGO_VERSION=$(awk -F'"' '/^version = "/{print $2; exit}' Cargo.toml)
if [ "$TAG_VERSION" != "$CARGO_VERSION" ]; then
echo "ERROR: tag version ($TAG_VERSION) != Cargo.toml version ($CARGO_VERSION)"
exit 1
fi
echo "Version match: $TAG_VERSION"
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
# 性能 H-1:用 arduino/setup-protoc@v3 替代 apt-get update(10-20s → 1-2s)
- name: Setup protoc
uses: arduino/setup-protoc@c65c819552d16ad3c9b72d9dfd5ba5237b9c906b # v3
- name: Restore Cargo cache
uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
# 安全 M-2:发布前 SAST 扫描(规则24:0 CRITICAL 才允许继续)
- name: Install cargo-audit
uses: taiki-e/install-action@cfdb7ed4ccc7c7dce37759eae89dc5674b469b39 # v2.90.3
with:
tool: cargo-audit
- name: Run cargo audit
run: cargo audit --deny warnings
# 架构 H-1 / M-3:publish 前做 dry-run 验证(包含完整构建,CI 已跑测试)
# 必须在 create archives 之前:cargo publish 拒绝 working directory 有未提交文件
- name: Verify package builds
run: cargo publish --dry-run
# 幂等:已发布版本直接跳过,避免重跑时"already exists"报错
- name: Check already published
id: published
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
# crates.io 偶发 5xx / 索引延迟会把「未发布」误判而触发重复 publish 失败:
# 对 5xx 退避重试,其余状态码(含 200/403/404)一次判定。
# curl 网络层失败时 `-w "%{http_code}"` 写出 "000" 且退出码非零——
# 不用 `|| echo "000"`:curl 非 0 退出时 -w 已输出 000,`|| echo` 会追加
# 第二个 000 产生多行 "000\n000",使 `[[ != "000" ]]` 恒 true 导致 fail-closed
# 被穿透(OCR 补扫发现)。改用 `${PIPESTATUS[0]}` 检测 curl 退出码来补写。
CODE=""
for _ in 1 2 3 4; do
curl -s -o /dev/null -w "%{http_code}" -H 'User-Agent: kirky-x-release-verify' \
"https://crates.io/api/v1/crates/calnexus/${VERSION}" > /tmp/.cr_code 2>/dev/null
CURL_RC=$?
CODE=$(cat /tmp/.cr_code)
if [ "$CURL_RC" -ne 0 ] && [ "$CODE" = "000" ]; then
CODE="000"
elif [ "$CURL_RC" -ne 0 ]; then
CODE="000"
fi
echo "calnexus ${VERSION}: HTTP ${CODE}"
[[ "$CODE" != 5* && "$CODE" != "000" ]] && break
sleep 15
done
if [[ "$CODE" == "000" ]]; then
echo "::error::无法确定 ${VERSION} 的发布状态(crates.io 网络不可达),拒绝继续"
exit 1
fi
echo "exists=$([[ "$CODE" == "200" ]] && echo true || echo false)" >> "$GITHUB_OUTPUT"
# crates.io trusted publishing(OIDC):官方 auth-action 以 Actions OIDC 换取短时发布凭证
- uses: rust-lang/crates-io-auth-action@c6f97d42243bad5fab37ca0427f495c86d5b1a18 # v1.0.5
id: auth
# 性能 H-2:-v 提供进度反馈,避免日志黑屏 45s 像 hang 死
# crates.io 是事实发布,必须在 GitHub Release 之前
- name: Publish to crates.io
if: steps.published.outputs.exists == 'false'
env:
CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
run: cargo publish -v
# Release 只带 GitHub 自动生成的源码压缩包,不附自制 archive/校验和产物。
# 幂等:softprops 对同 tag 已有 release 为 upsert 语义(重跑改写而非新建),
# 随后 uniqueness 断言钉住恰好 1 个。
- name: Create GitHub Release
uses: softprops/action-gh-release@8a0cd651e5b2365bd640854a3fa5dfc70e4875f3 # v3.0.4
with:
tag_name: ${{ steps.version.outputs.tag }}
name: ${{ steps.version.outputs.tag }}
generate_release_notes: true
prerelease: ${{ steps.meta.outputs.PRERELEASE }}
# TAG 取自 extract 步(与创建步同源):手动触发时 GITHUB_REF 是分支 ref,
# 按 GITHUB_REF 剥前缀会得到错误 tag 导致断言误报
- name: Verify release uniqueness (idempotency guard)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ steps.version.outputs.tag }}
run: |
COUNT=$(gh api "repos/${GITHUB_REPOSITORY}/releases?per_page=100" --paginate --jq "[.[] | select(.tag_name == \"${TAG}\")] | length")
echo "release objects for ${TAG}: ${COUNT}"
if [ "${COUNT}" != "1" ]; then
echo "::error::expected exactly 1 release for ${TAG}, found ${COUNT}; release state is not idempotent"
exit 1
fi