@dtapp/ci-templates 0.0.1-nightly.20261126 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -3
- package/dist/minify/scripts/ci/fetch-scripts.sh +188 -59
- package/package.json +1 -1
- package/scripts/ci/fetch-scripts.sh +218 -63
package/README.md
CHANGED
|
@@ -4,6 +4,8 @@ CNB(云原生构建)流水线模板与配套脚本仓库。业务仓库通
|
|
|
4
4
|
|
|
5
5
|
[](https://cnb.cool/dtapp/ci-templates) [](https://cnb.cool/dtapp/ci-templates) [](https://cnb.cool/dtapp/ci-templates) [](https://cnb.cool/dtapp/ci-templates)
|
|
6
6
|
|
|
7
|
+
[](https://www.npmjs.com/package/@dtapp/ci-templates) [](https://www.npmjs.com/package/@dtapp/ci-templates) [](https://www.npmjs.com/package/@dtapp/ci-templates) [](https://github.com/dtapps/ci-templates/blob/main/LICENSE) [](https://www.npmjs.com/package/@dtapp/ci-templates) [](https://www.jsdelivr.com/package/npm/@dtapp/ci-templates) [](https://socket.dev/npm/package/@dtapp/ci-templates)
|
|
8
|
+
|
|
7
9
|
## 模板一览
|
|
8
10
|
|
|
9
11
|
| 文件 | 触发事件 | 作用 |
|
|
@@ -57,18 +59,32 @@ sh scripts/ci/render-ci-notice.sh --self-test
|
|
|
57
59
|
|
|
58
60
|
## 脚本同步(`scripts/ci/fetch-scripts.sh`)
|
|
59
61
|
|
|
60
|
-
`scripts/ci/fetch-scripts.sh`
|
|
62
|
+
`scripts/ci/fetch-scripts.sh` 是给业务仓库用的自举脚本:文件顶部固定各源地址和一份脚本清单,运行时按清单把脚本下载到**与模板仓库完全一致的相对路径**。业务仓库只要保留这一个文件,就不必手工复制或逐个跟版本。
|
|
63
|
+
|
|
64
|
+
首次接入时在业务仓库根目录执行(落盘路径与模板仓库一致,仍是 `scripts/ci/fetch-scripts.sh`;推荐走 CNB):
|
|
65
|
+
|
|
66
|
+
```sh
|
|
67
|
+
mkdir -p scripts/ci
|
|
68
|
+
curl -fsSL https://cnb.cool/dtapp/ci-templates/-/git/raw/main/scripts/ci/fetch-scripts.sh -o scripts/ci/fetch-scripts.sh
|
|
69
|
+
chmod +x scripts/ci/fetch-scripts.sh
|
|
70
|
+
sh scripts/ci/fetch-scripts.sh # 再按清单拉齐其余脚本
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
之后日常同步只需:
|
|
61
74
|
|
|
62
75
|
```sh
|
|
63
76
|
sh scripts/ci/fetch-scripts.sh # 同步:缺失的下载,已存在的比 sha256,不同则更新
|
|
64
77
|
sh scripts/ci/fetch-scripts.sh --force # 不做比对,已存在也直接覆盖
|
|
65
78
|
sh scripts/ci/fetch-scripts.sh --check # 只查存在性(不下载、不比对),缺失时退出码 1
|
|
66
79
|
sh scripts/ci/fetch-scripts.sh --list # 查看清单与候选源顺序
|
|
80
|
+
sh scripts/ci/fetch-scripts.sh --npm-version 0.0.1-nightly.20261126 # 指定 npm 包版本或 dist-tag
|
|
67
81
|
```
|
|
68
82
|
|
|
69
|
-
|
|
83
|
+
源分三类:四个仓库源、npm CDN、npm registry 与镜像,固定顺序为 `CNB → GitHub → Gitee → GitLab → jsdelivr → unpkg → npm → npmmirror → tencent`(包已发布到 npm:`@dtapp/ci-templates`)。脚本探测当前 CI 平台并用同平台仓库作为起点源,起点源排第一;**其余源一律按上述固定顺序尝试**,所以四个仓库源始终在 npm 源之前(GitHub 环境下 `cnb / gitee / gitlab` 也会排在 `npmmirror` 前面,不会被挪到末尾);一个都匹配不上(本地开发等)时默认 CNB。某个源取不到文件(超时、404)会自动换下一个源,回退是每个文件独立的,全部失败才报错;加 `--no-fallback` 可关掉回退。也可用 `--source` 指定起点源,`CI_TEMPLATES_REF` 固定仓库分支或 tag(默认 `main`),`CI_TEMPLATES_NPM_VERSION` 固定 npm 版本或 dist-tag(默认 `latest`)。
|
|
84
|
+
|
|
85
|
+
各平台下载地址格式不同:GitLab 是 `/-/raw/<ref>/<path>`,Gitee 是 `/raw/<ref>/<path>`(302 到内容域,靠 `curl -L` 跟随)。
|
|
70
86
|
|
|
71
|
-
|
|
87
|
+
npm 源分两种取法:jsDelivr(`cdn.jsdelivr.net/npm/<包>@<版本>/<path>`)和 unpkg(`unpkg.com/<包>@<版本>/<path>`,`app.unpkg.com` 只是文件浏览界面)能按路径直接取单文件;官方 registry 和镜像(`registry.npmjs.org` / `registry.npmmirror.com` / `mirrors.cloud.tencent.com/npm`)只有整包接口,脚本会先把 `<包>-<版本>.tgz` 下载到临时目录(每次运行每个镜像最多一次)再用 `tar -xzOf` 取单个文件,因此这类源额外依赖 `tar`。`latest` / dist-tag 会先向对应 registry 解析成具体版本号,整包路径里用具体版本。
|
|
72
88
|
|
|
73
89
|
本地已有同名文件时不会直接信任:先下载远端内容算 sha256,与本地一致就跳过、不一致就覆盖,所以仓库里不会停着旧版本脚本。下载先写临时文件、比对通过才 `mv`,失败不会破坏已有文件;`.sh` 必须以 `#!` 开头才落盘,避免把错误页面写成脚本。当前清单:`scripts/ci/delete-release.sh`、`scripts/ci/describe-artifact.sh`、`scripts/ci/render-ci-notice.sh`。
|
|
74
90
|
|
|
@@ -2,16 +2,18 @@
|
|
|
2
2
|
# 模板脚本拉取器:把 ci-templates 里的脚本按「与模板仓库完全一致的相对路径」
|
|
3
3
|
# 落到当前仓库(默认 scripts/ci/),供 CI stage 直接 sh scripts/ci/xxx.sh 调用。
|
|
4
4
|
#
|
|
5
|
-
# 背景:ci-templates 在 CNB / GitHub / GitLab / Gitee
|
|
6
|
-
#
|
|
7
|
-
#
|
|
5
|
+
# 背景:ci-templates 在 CNB / GitHub / GitLab / Gitee 各放一份,内容一致,
|
|
6
|
+
# 只有平台差异(下载地址格式);同时发布到 npm(@dtapp/ci-templates),
|
|
7
|
+
# 仓库源全部取不到时还能靠 npm CDN 或国内镜像兜底。
|
|
8
|
+
# 本文件下方的清单决定要拉哪些脚本;业务仓库只需保留本文件,不必手工复制、同步。
|
|
8
9
|
#
|
|
9
10
|
# 用法(在任意仓库根目录执行):
|
|
10
11
|
# sh scripts/ci/fetch-scripts.sh # 同步:缺失的下载,已存在的比 sha256,不同则更新
|
|
11
12
|
# sh scripts/ci/fetch-scripts.sh --force # 不做比对,已存在也直接覆盖
|
|
12
13
|
# sh scripts/ci/fetch-scripts.sh --check # 只查存在性(不下载、不比对),缺失时退出码 1
|
|
13
14
|
# sh scripts/ci/fetch-scripts.sh --list # 打印清单与候选源顺序
|
|
14
|
-
# sh scripts/ci/fetch-scripts.sh --source gitlab #
|
|
15
|
+
# sh scripts/ci/fetch-scripts.sh --source gitlab # 指定起点源(见下方源清单)
|
|
16
|
+
# sh scripts/ci/fetch-scripts.sh --npm-version 0.0.1-nightly.20261126 # 指定 npm 包版本或 dist-tag
|
|
15
17
|
# sh scripts/ci/fetch-scripts.sh --no-fallback # 只用起点源,失败不换源
|
|
16
18
|
# sh scripts/ci/fetch-scripts.sh delete-release.sh # 只处理指定脚本(可多个,也可写全路径)
|
|
17
19
|
#
|
|
@@ -19,17 +21,30 @@
|
|
|
19
21
|
# 1) --source <平台> / 环境变量 CI_TEMPLATES_SOURCE 显式指定「起点源」
|
|
20
22
|
# 2) 否则自动探测当前 CI 平台(CNB / GitHub / Gitee / GitLab)作为起点
|
|
21
23
|
# 3) 一个都探测不到(如本地开发):按 CNB → GitHub → Gitee → GitLab 取第一个,即默认 CNB
|
|
22
|
-
# 4)
|
|
23
|
-
#
|
|
24
|
+
# 4) 起点源取不到文件时,其余源按固定顺序 SOURCE_ORDER 依次尝试(四个仓库源始终
|
|
25
|
+
# 排在 npm 源之前),直到拿到或全部失败;回退是「每个文件独立」的,
|
|
26
|
+
# 所以某个源缺某个脚本也能自动从别的源补齐
|
|
24
27
|
# --no-fallback(或 CI_TEMPLATES_NO_FALLBACK=1)可关掉回退,只用起点源
|
|
25
28
|
#
|
|
26
|
-
#
|
|
29
|
+
# 源清单(内容一致,按 SOURCE_ORDER 顺序尝试):
|
|
30
|
+
# ① 仓库源:跟 CI_TEMPLATES_REF(默认 main)走,代码最新
|
|
27
31
|
# CNB :https://cnb.cool/dtapp/ci-templates /-/git/raw/<ref>/<path>
|
|
28
32
|
# GitHub :https://github.com/dtapps/ci-templates raw.githubusercontent.com/dtapps/ci-templates/<ref>/<path>
|
|
29
33
|
# Gitee :https://gitee.com/dtapps/ci-templates /raw/<ref>/<path>(302 到内容域,靠 -L 跟随)
|
|
30
34
|
# GitLab :https://gitlab.com/dtapps/ci-templates /-/raw/<ref>/<path>
|
|
31
|
-
#
|
|
32
|
-
#
|
|
35
|
+
#
|
|
36
|
+
# ② npm 源:跟 npm 包版本走(CI_TEMPLATES_NPM_VERSION,默认 latest 标签)
|
|
37
|
+
# jsdelivr:https://cdn.jsdelivr.net/npm/@dtapp/ci-templates@<ver>/<path>
|
|
38
|
+
# unpkg :https://unpkg.com/@dtapp/ci-templates@<ver>/<path>
|
|
39
|
+
# (https://www.npmjs.com/package/@dtapp/ci-templates 与 app.unpkg.com 只是
|
|
40
|
+
# 包页面/文件浏览界面,真正能按路径取文件的是这两个 CDN)
|
|
41
|
+
# npm :https://registry.npmjs.org/@dtapp/ci-templates/-/ci-templates-<ver>.tgz
|
|
42
|
+
# (官方 registry 没有单文件接口,只能下整包然后用 tar 取单个文件)
|
|
43
|
+
#
|
|
44
|
+
# ③ npm 镜像源:②全部失败才用,国内网络更稳
|
|
45
|
+
# npmmirror:https://registry.npmmirror.com/@dtapp/ci-templates/-/ci-templates-<ver>.tgz
|
|
46
|
+
# tencent :https://mirrors.cloud.tencent.com/npm/@dtapp/ci-templates/-/ci-templates-<ver>.tgz
|
|
47
|
+
# (镜像同样只提供整包 tgz,每次运行每个镜像最多下载一次并复用)
|
|
33
48
|
#
|
|
34
49
|
# 平台探测(只看各平台强特征变量,避免业务仓库里配的 GITEE_TOKEN 之类被误判成 CI):
|
|
35
50
|
# CNB CNB=true,或 CNB_BUILD_ID / CNB_PIPELINE_ID / CNB_BUILD_WORKSPACE 任一非空
|
|
@@ -38,13 +53,20 @@
|
|
|
38
53
|
# GitLab GITLAB_CI=true
|
|
39
54
|
#
|
|
40
55
|
# 环境变量:
|
|
41
|
-
# CI_TEMPLATES_SOURCE cnb | github | gitee | gitlab,等价于 --source
|
|
56
|
+
# CI_TEMPLATES_SOURCE cnb | github | gitee | gitlab | jsdelivr | unpkg | npm | npmmirror | tencent,等价于 --source
|
|
42
57
|
# CI_TEMPLATES_REF 模板仓库分支或 tag,默认 main
|
|
58
|
+
# CI_TEMPLATES_NPM_PACKAGE npm 包名,默认 @dtapp/ci-templates
|
|
59
|
+
# CI_TEMPLATES_NPM_VERSION npm 版本号或 dist-tag(latest / nightly ...),默认 latest
|
|
43
60
|
# CI_TEMPLATES_NO_FALLBACK 非空 = 不回退,只用起点源
|
|
44
61
|
# CI_TEMPLATES_CNB_URL CNB 下载基址(默认 https://cnb.cool/dtapp/ci-templates/-/git/raw)
|
|
45
62
|
# CI_TEMPLATES_GITHUB_URL GitHub 下载基址(默认 https://raw.githubusercontent.com/dtapps/ci-templates)
|
|
46
63
|
# CI_TEMPLATES_GITEE_URL Gitee 下载基址(默认 https://gitee.com/dtapps/ci-templates/raw)
|
|
47
64
|
# CI_TEMPLATES_GITLAB_URL GitLab 下载基址(默认 https://gitlab.com/dtapps/ci-templates/-/raw)
|
|
65
|
+
# CI_TEMPLATES_JSDELIVR_URL jsDelivr 基址(默认 https://cdn.jsdelivr.net/npm)
|
|
66
|
+
# CI_TEMPLATES_UNPKG_URL unpkg 基址(默认 https://unpkg.com)
|
|
67
|
+
# CI_TEMPLATES_NPM_URL 官方 npm registry 基址(默认 https://registry.npmjs.org)
|
|
68
|
+
# CI_TEMPLATES_NPMMIRROR_URL npmmirror 基址(默认 https://registry.npmmirror.com)
|
|
69
|
+
# CI_TEMPLATES_TENCENT_NPM_URL 腾讯云 npm 镜像基址(默认 https://mirrors.cloud.tencent.com/npm)
|
|
48
70
|
# CI_SCRIPTS_ROOT 落盘根目录,默认本脚本所在的仓库根
|
|
49
71
|
# CI_SCRIPTS_TIMEOUT 单文件下载超时秒数,默认 30
|
|
50
72
|
#
|
|
@@ -57,7 +79,8 @@
|
|
|
57
79
|
#
|
|
58
80
|
# 退出码:0 成功;1 有脚本缺失或下载失败;2 用法错误。
|
|
59
81
|
#
|
|
60
|
-
# 依赖:curl(缺失时退到 wget
|
|
82
|
+
# 依赖:curl(缺失时退到 wget);npm 镜像源额外需要 tar(只解包单个文件)。
|
|
83
|
+
# sha256 依次尝试 sha256sum / shasum / openssl dgst,
|
|
61
84
|
# 三者都没有时退化为「直接覆盖」——结果仍是最新,只是不再跳过。
|
|
62
85
|
|
|
63
86
|
set -u
|
|
@@ -65,7 +88,13 @@ TEMPLATE_CNB_URL='https://cnb.cool/dtapp/ci-templates/-/git/raw'
|
|
|
65
88
|
TEMPLATE_GITHUB_URL='https://raw.githubusercontent.com/dtapps/ci-templates'
|
|
66
89
|
TEMPLATE_GITEE_URL='https://gitee.com/dtapps/ci-templates/raw'
|
|
67
90
|
TEMPLATE_GITLAB_URL='https://gitlab.com/dtapps/ci-templates/-/raw'
|
|
68
|
-
|
|
91
|
+
TEMPLATE_JSDELIVR_URL='https://cdn.jsdelivr.net/npm'
|
|
92
|
+
TEMPLATE_UNPKG_URL='https://unpkg.com'
|
|
93
|
+
TEMPLATE_NPM_URL='https://registry.npmjs.org'
|
|
94
|
+
TEMPLATE_NPMMIRROR_URL='https://registry.npmmirror.com'
|
|
95
|
+
TEMPLATE_TENCENT_NPM_URL='https://mirrors.cloud.tencent.com/npm'
|
|
96
|
+
SOURCE_ORDER='cnb github gitee gitlab jsdelivr unpkg npm npmmirror tencent'
|
|
97
|
+
TARBALL_SOURCES='npm npmmirror tencent'
|
|
69
98
|
manifest() {
|
|
70
99
|
cat <<'EOF'
|
|
71
100
|
scripts/ci/delete-release.sh
|
|
@@ -84,6 +113,8 @@ die() {
|
|
|
84
113
|
}
|
|
85
114
|
SOURCE=${CI_TEMPLATES_SOURCE:-}
|
|
86
115
|
REF=${CI_TEMPLATES_REF:-main}
|
|
116
|
+
NPM_PACKAGE=${CI_TEMPLATES_NPM_PACKAGE:-@dtapp/ci-templates}
|
|
117
|
+
NPM_VERSION=${CI_TEMPLATES_NPM_VERSION:-latest}
|
|
87
118
|
TIMEOUT=${CI_SCRIPTS_TIMEOUT:-30}
|
|
88
119
|
FORCE=0
|
|
89
120
|
CHECK_ONLY=0
|
|
@@ -91,12 +122,14 @@ LIST_ONLY=0
|
|
|
91
122
|
FALLBACK=1
|
|
92
123
|
WANTED=
|
|
93
124
|
fs_tmp=
|
|
125
|
+
NPM_TMP=${TMPDIR:-/tmp}/ci-templates-npm.$$
|
|
126
|
+
[ -e "$NPM_TMP" ] && rm -rf "$NPM_TMP"
|
|
94
127
|
[ -n "${CI_TEMPLATES_NO_FALLBACK:-}" ] && FALLBACK=0
|
|
95
128
|
while [ $# -gt 0 ]; do
|
|
96
129
|
case "$1" in
|
|
97
130
|
--source | --platform)
|
|
98
131
|
SOURCE=${2:-}
|
|
99
|
-
[ -n "$SOURCE" ] || die "$1 需要一个值:cnb / github / gitlab /
|
|
132
|
+
[ -n "$SOURCE" ] || die "$1 需要一个值:cnb / github / gitee / gitlab / jsdelivr / unpkg / npm / npmmirror / tencent"
|
|
100
133
|
shift
|
|
101
134
|
[ $# -gt 0 ] && shift
|
|
102
135
|
;;
|
|
@@ -106,6 +139,18 @@ while [ $# -gt 0 ]; do
|
|
|
106
139
|
shift
|
|
107
140
|
[ $# -gt 0 ] && shift
|
|
108
141
|
;;
|
|
142
|
+
--npm-version)
|
|
143
|
+
NPM_VERSION=${2:-}
|
|
144
|
+
[ -n "$NPM_VERSION" ] || die "--npm-version 需要一个值(版本号或 dist-tag,如 latest / 0.0.1-nightly.20261126)"
|
|
145
|
+
shift
|
|
146
|
+
[ $# -gt 0 ] && shift
|
|
147
|
+
;;
|
|
148
|
+
--npm-package)
|
|
149
|
+
NPM_PACKAGE=${2:-}
|
|
150
|
+
[ -n "$NPM_PACKAGE" ] || die "--npm-package 需要一个值(如 @dtapp/ci-templates)"
|
|
151
|
+
shift
|
|
152
|
+
[ $# -gt 0 ] && shift
|
|
153
|
+
;;
|
|
109
154
|
--force | -f)
|
|
110
155
|
FORCE=1
|
|
111
156
|
shift
|
|
@@ -166,9 +211,9 @@ if [ -z "$SOURCE" ]; then
|
|
|
166
211
|
SOURCE=$(detect_source)
|
|
167
212
|
fi
|
|
168
213
|
case "$SOURCE" in
|
|
169
|
-
cnb | github | gitee | gitlab) ;;
|
|
214
|
+
cnb | github | gitee | gitlab | jsdelivr | unpkg | npm | npmmirror | tencent) ;;
|
|
170
215
|
*)
|
|
171
|
-
die "
|
|
216
|
+
die "源只能是 cnb / github / gitee / gitlab / jsdelivr / unpkg / npm / npmmirror / tencent(当前:${SOURCE})"
|
|
172
217
|
;;
|
|
173
218
|
esac
|
|
174
219
|
base_url_for() {
|
|
@@ -177,30 +222,94 @@ base_url_for() {
|
|
|
177
222
|
github) printf '%s' "${CI_TEMPLATES_GITHUB_URL:-$TEMPLATE_GITHUB_URL}" ;;
|
|
178
223
|
gitee) printf '%s' "${CI_TEMPLATES_GITEE_URL:-$TEMPLATE_GITEE_URL}" ;;
|
|
179
224
|
gitlab) printf '%s' "${CI_TEMPLATES_GITLAB_URL:-$TEMPLATE_GITLAB_URL}" ;;
|
|
225
|
+
jsdelivr) printf '%s' "${CI_TEMPLATES_JSDELIVR_URL:-$TEMPLATE_JSDELIVR_URL}" ;;
|
|
226
|
+
unpkg) printf '%s' "${CI_TEMPLATES_UNPKG_URL:-$TEMPLATE_UNPKG_URL}" ;;
|
|
227
|
+
npm) printf '%s' "${CI_TEMPLATES_NPM_URL:-$TEMPLATE_NPM_URL}" ;;
|
|
228
|
+
npmmirror) printf '%s' "${CI_TEMPLATES_NPMMIRROR_URL:-$TEMPLATE_NPMMIRROR_URL}" ;;
|
|
229
|
+
tencent) printf '%s' "${CI_TEMPLATES_TENCENT_NPM_URL:-$TEMPLATE_TENCENT_NPM_URL}" ;;
|
|
180
230
|
esac
|
|
181
231
|
}
|
|
232
|
+
is_tarball_source() {
|
|
233
|
+
case " $TARBALL_SOURCES " in
|
|
234
|
+
*" $1 "*) return 0 ;;
|
|
235
|
+
esac
|
|
236
|
+
return 1
|
|
237
|
+
}
|
|
238
|
+
download() {
|
|
239
|
+
fs_dl_url=$1
|
|
240
|
+
fs_dl_out=$2
|
|
241
|
+
[ -d "${fs_dl_out%/*}" ] || mkdir -p "${fs_dl_out%/*}" || return 1
|
|
242
|
+
if command -v curl >/dev/null 2>&1; then
|
|
243
|
+
curl -fsSL --connect-timeout 10 --max-time "$TIMEOUT" "$fs_dl_url" -o "$fs_dl_out" 2>/dev/null \
|
|
244
|
+
|| {
|
|
245
|
+
rm -f "$fs_dl_out"
|
|
246
|
+
return 1
|
|
247
|
+
}
|
|
248
|
+
elif command -v wget >/dev/null 2>&1; then
|
|
249
|
+
wget -q -T "$TIMEOUT" -O "$fs_dl_out" "$fs_dl_url" \
|
|
250
|
+
|| {
|
|
251
|
+
rm -f "$fs_dl_out"
|
|
252
|
+
return 1
|
|
253
|
+
}
|
|
254
|
+
else
|
|
255
|
+
warn '需要 curl 或 wget 才能下载模板脚本'
|
|
256
|
+
return 1
|
|
257
|
+
fi
|
|
258
|
+
if [ ! -s "$fs_dl_out" ]; then
|
|
259
|
+
rm -f "$fs_dl_out"
|
|
260
|
+
return 1
|
|
261
|
+
fi
|
|
262
|
+
return 0
|
|
263
|
+
}
|
|
182
264
|
source_url() {
|
|
183
|
-
|
|
184
|
-
|
|
265
|
+
fs_url_base=$(base_url_for "$1")
|
|
266
|
+
fs_url_base=${fs_url_base%/}
|
|
267
|
+
case "$1" in
|
|
268
|
+
jsdelivr | unpkg)
|
|
269
|
+
printf '%s/%s@%s/%s' "$fs_url_base" "$NPM_PACKAGE" "$NPM_VERSION" "$2"
|
|
270
|
+
;;
|
|
271
|
+
npm | npmmirror | tencent)
|
|
272
|
+
fs_url_ver=$(npm_version_for "$1" || true)
|
|
273
|
+
[ -n "$fs_url_ver" ] || return 1
|
|
274
|
+
printf '%s/%s/-/%s-%s.tgz' "$fs_url_base" "$NPM_PACKAGE" "${NPM_PACKAGE##*/}" "$fs_url_ver"
|
|
275
|
+
;;
|
|
276
|
+
*)
|
|
277
|
+
printf '%s/%s/%s' "$fs_url_base" "$REF" "$2"
|
|
278
|
+
;;
|
|
279
|
+
esac
|
|
280
|
+
}
|
|
281
|
+
npm_version_for() {
|
|
282
|
+
case "$NPM_VERSION" in
|
|
283
|
+
[0-9]*) printf '%s\n' "$NPM_VERSION" && return 0 ;;
|
|
284
|
+
esac
|
|
285
|
+
fs_ver_reg=$(base_url_for "$1")
|
|
286
|
+
fs_ver_reg=${fs_ver_reg%/}
|
|
287
|
+
[ -d "$NPM_TMP" ] || mkdir -p "$NPM_TMP" || return 1
|
|
288
|
+
fs_ver_cache="$NPM_TMP/version.$1"
|
|
289
|
+
if [ -s "$fs_ver_cache" ]; then
|
|
290
|
+
cat "$fs_ver_cache"
|
|
291
|
+
return 0
|
|
292
|
+
fi
|
|
293
|
+
fs_ver_json="$NPM_TMP/meta.$1.$$"
|
|
294
|
+
if ! download "${fs_ver_reg}/${NPM_PACKAGE}/${NPM_VERSION}" "$fs_ver_json"; then
|
|
295
|
+
warn "源 $1 解析版本失败(${NPM_PACKAGE}@${NPM_VERSION})"
|
|
296
|
+
return 1
|
|
297
|
+
fi
|
|
298
|
+
fs_ver=$(grep -o '"version"[[:space:]]*:[[:space:]]*"[^"]*"' "$fs_ver_json" 2>/dev/null \
|
|
299
|
+
| head -n 1 | sed 's/.*"\([^"]*\)"$/\1/')
|
|
300
|
+
rm -f "$fs_ver_json"
|
|
301
|
+
[ -n "$fs_ver" ] || return 1
|
|
302
|
+
printf '%s\n' "$fs_ver" >"$fs_ver_cache"
|
|
303
|
+
printf '%s\n' "$fs_ver"
|
|
185
304
|
}
|
|
186
305
|
source_candidates() {
|
|
187
306
|
if [ "$FALLBACK" != '1' ]; then
|
|
188
307
|
printf '%s\n' "$SOURCE"
|
|
189
308
|
return 0
|
|
190
309
|
fi
|
|
191
|
-
|
|
192
|
-
fs_tail=
|
|
193
|
-
fs_hit=0
|
|
310
|
+
printf '%s\n' "$SOURCE"
|
|
194
311
|
for fs_s in $SOURCE_ORDER; do
|
|
195
|
-
|
|
196
|
-
fs_tail="${fs_tail}${fs_tail:+ }$fs_s"
|
|
197
|
-
elif [ "$fs_s" = "$SOURCE" ]; then
|
|
198
|
-
fs_hit=1
|
|
199
|
-
else
|
|
200
|
-
fs_head="${fs_head}${fs_head:+ }$fs_s"
|
|
201
|
-
fi
|
|
202
|
-
done
|
|
203
|
-
for fs_s in $SOURCE $fs_tail $fs_head; do
|
|
312
|
+
[ "$fs_s" != "$SOURCE" ] || continue
|
|
204
313
|
printf '%s\n' "$fs_s"
|
|
205
314
|
done
|
|
206
315
|
}
|
|
@@ -236,7 +345,7 @@ selected() {
|
|
|
236
345
|
done
|
|
237
346
|
}
|
|
238
347
|
if [ "$LIST_ONLY" = '1' ]; then
|
|
239
|
-
info "起点源:${SOURCE}(ref=${REF})"
|
|
348
|
+
info "起点源:${SOURCE}(ref=${REF};npm 包=${NPM_PACKAGE}@${NPM_VERSION})"
|
|
240
349
|
info "源顺序:$(source_candidates | tr '\n' ' ')"
|
|
241
350
|
info "示例:$(source_url "$SOURCE" "$(manifest | head -n 1)")"
|
|
242
351
|
info "落盘目录:${ROOT_DIR}"
|
|
@@ -249,7 +358,10 @@ if [ -n "$WANTED" ]; then
|
|
|
249
358
|
|| die "清单里没有 ${fs_w}(用 --list 查看可用脚本)"
|
|
250
359
|
done
|
|
251
360
|
fi
|
|
252
|
-
cleanup() {
|
|
361
|
+
cleanup() {
|
|
362
|
+
[ -n "${fs_tmp:-}" ] && rm -f "$fs_tmp"
|
|
363
|
+
[ -n "${NPM_TMP:-}" ] && rm -rf "$NPM_TMP"
|
|
364
|
+
}
|
|
253
365
|
trap cleanup EXIT HUP INT TERM
|
|
254
366
|
sha256_of() {
|
|
255
367
|
if command -v sha256sum >/dev/null 2>&1; then
|
|
@@ -274,33 +386,34 @@ content_state() {
|
|
|
274
386
|
return 1
|
|
275
387
|
}
|
|
276
388
|
fetch() {
|
|
277
|
-
|
|
278
|
-
|
|
389
|
+
fs_source=$1
|
|
390
|
+
fs_rel=$2
|
|
391
|
+
fs_dest=$3
|
|
279
392
|
fs_dir=${fs_dest%/*}
|
|
280
393
|
fs_tmp="$fs_dest.tmp.$$"
|
|
281
394
|
[ -d "$fs_dir" ] || mkdir -p "$fs_dir" || return 1
|
|
282
|
-
if
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
395
|
+
if is_tarball_source "$fs_source"; then
|
|
396
|
+
fs_tgz=$(tarball_for "$fs_source") || return 1
|
|
397
|
+
if ! command -v tar >/dev/null 2>&1; then
|
|
398
|
+
warn '需要 tar 才能从 npm 整包里取脚本'
|
|
399
|
+
return 1
|
|
400
|
+
fi
|
|
401
|
+
if ! tar -xzOf "$fs_tgz" "package/${fs_rel}" >"$fs_tmp" 2>/dev/null; then
|
|
402
|
+
rm -f "$fs_tmp"
|
|
403
|
+
fs_tmp=
|
|
404
|
+
return 1
|
|
405
|
+
fi
|
|
406
|
+
if [ ! -s "$fs_tmp" ]; then
|
|
407
|
+
rm -f "$fs_tmp"
|
|
408
|
+
fs_tmp=
|
|
409
|
+
return 1
|
|
410
|
+
fi
|
|
296
411
|
else
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
fs_tmp=
|
|
303
|
-
return 1
|
|
412
|
+
fs_url=$(source_url "$fs_source" "$fs_rel") || return 1
|
|
413
|
+
if ! download "$fs_url" "$fs_tmp"; then
|
|
414
|
+
fs_tmp=
|
|
415
|
+
return 1
|
|
416
|
+
fi
|
|
304
417
|
fi
|
|
305
418
|
case "$fs_dest" in
|
|
306
419
|
*.sh)
|
|
@@ -313,6 +426,23 @@ fetch() {
|
|
|
313
426
|
esac
|
|
314
427
|
return 0
|
|
315
428
|
}
|
|
429
|
+
tarball_for() {
|
|
430
|
+
fs_tb_ver=$(npm_version_for "$1") || return 1
|
|
431
|
+
[ -n "$fs_tb_ver" ] || return 1
|
|
432
|
+
[ -d "$NPM_TMP" ] || mkdir -p "$NPM_TMP" || return 1
|
|
433
|
+
fs_tb_tgz="$NPM_TMP/$1.tgz"
|
|
434
|
+
if [ ! -s "$fs_tb_tgz" ]; then
|
|
435
|
+
fs_tb_url=$(base_url_for "$1")
|
|
436
|
+
fs_tb_url=$(printf '%s/%s/-/%s-%s.tgz' \
|
|
437
|
+
"${fs_tb_url%/}" "$NPM_PACKAGE" "${NPM_PACKAGE##*/}" "$fs_tb_ver")
|
|
438
|
+
if ! download "$fs_tb_url" "$fs_tb_tgz"; then
|
|
439
|
+
warn "源 $1 整包下载失败:${fs_tb_url}"
|
|
440
|
+
rm -f "$fs_tb_tgz"
|
|
441
|
+
return 1
|
|
442
|
+
fi
|
|
443
|
+
fi
|
|
444
|
+
printf '%s\n' "$fs_tb_tgz"
|
|
445
|
+
}
|
|
316
446
|
fs_created=0
|
|
317
447
|
fs_updated=0
|
|
318
448
|
fs_same=0
|
|
@@ -320,7 +450,7 @@ fs_missing=0
|
|
|
320
450
|
fs_failed=0
|
|
321
451
|
CANDIDATES=$(source_candidates)
|
|
322
452
|
CANDIDATE_COUNT=$(printf '%s\n' "$CANDIDATES" | grep -c .)
|
|
323
|
-
info "起点源:${SOURCE}(ref=${REF}) → ${ROOT_DIR}"
|
|
453
|
+
info "起点源:${SOURCE}(ref=${REF};npm 包=${NPM_PACKAGE}@${NPM_VERSION}) → ${ROOT_DIR}"
|
|
324
454
|
if [ "$FALLBACK" = '1' ]; then
|
|
325
455
|
info "失败回退顺序:$(printf '%s' "$CANDIDATES" | tr '\n' ' ')"
|
|
326
456
|
fi
|
|
@@ -343,8 +473,7 @@ while IFS= read -r fs_rel; do
|
|
|
343
473
|
fs_ok=0
|
|
344
474
|
fs_used=
|
|
345
475
|
for fs_src in $CANDIDATES; do
|
|
346
|
-
|
|
347
|
-
if fetch "$fs_url" "$fs_dest"; then
|
|
476
|
+
if fetch "$fs_src" "$fs_rel" "$fs_dest"; then
|
|
348
477
|
fs_ok=1
|
|
349
478
|
fs_used=$fs_src
|
|
350
479
|
break
|
|
@@ -408,8 +537,8 @@ if [ "$CHECK_ONLY" = '1' ]; then
|
|
|
408
537
|
exit 0
|
|
409
538
|
fi
|
|
410
539
|
if [ "$fs_failed" -ne 0 ]; then
|
|
411
|
-
warn "有 ${fs_failed} 个脚本在所有源都取不到(起点:${SOURCE},ref:${REF})"
|
|
412
|
-
warn "可指定源重试:sh $0 --source <cnb|github|gitee|gitlab> --force"
|
|
540
|
+
warn "有 ${fs_failed} 个脚本在所有源都取不到(起点:${SOURCE},ref:${REF},npm:${NPM_PACKAGE}@${NPM_VERSION})"
|
|
541
|
+
warn "可指定源重试:sh $0 --source <cnb|github|gitee|gitlab|jsdelivr|unpkg|npm|npmmirror|tencent> --force"
|
|
413
542
|
exit 1
|
|
414
543
|
fi
|
|
415
544
|
info "完成:新增 ${fs_created} 个,更新 ${fs_updated} 个,一致 ${fs_same} 个"
|
package/package.json
CHANGED
|
@@ -2,16 +2,18 @@
|
|
|
2
2
|
# 模板脚本拉取器:把 ci-templates 里的脚本按「与模板仓库完全一致的相对路径」
|
|
3
3
|
# 落到当前仓库(默认 scripts/ci/),供 CI stage 直接 sh scripts/ci/xxx.sh 调用。
|
|
4
4
|
#
|
|
5
|
-
# 背景:ci-templates 在 CNB / GitHub / GitLab / Gitee
|
|
6
|
-
#
|
|
7
|
-
#
|
|
5
|
+
# 背景:ci-templates 在 CNB / GitHub / GitLab / Gitee 各放一份,内容一致,
|
|
6
|
+
# 只有平台差异(下载地址格式);同时发布到 npm(@dtapp/ci-templates),
|
|
7
|
+
# 仓库源全部取不到时还能靠 npm CDN 或国内镜像兜底。
|
|
8
|
+
# 本文件下方的清单决定要拉哪些脚本;业务仓库只需保留本文件,不必手工复制、同步。
|
|
8
9
|
#
|
|
9
10
|
# 用法(在任意仓库根目录执行):
|
|
10
11
|
# sh scripts/ci/fetch-scripts.sh # 同步:缺失的下载,已存在的比 sha256,不同则更新
|
|
11
12
|
# sh scripts/ci/fetch-scripts.sh --force # 不做比对,已存在也直接覆盖
|
|
12
13
|
# sh scripts/ci/fetch-scripts.sh --check # 只查存在性(不下载、不比对),缺失时退出码 1
|
|
13
14
|
# sh scripts/ci/fetch-scripts.sh --list # 打印清单与候选源顺序
|
|
14
|
-
# sh scripts/ci/fetch-scripts.sh --source gitlab #
|
|
15
|
+
# sh scripts/ci/fetch-scripts.sh --source gitlab # 指定起点源(见下方源清单)
|
|
16
|
+
# sh scripts/ci/fetch-scripts.sh --npm-version 0.0.1-nightly.20261126 # 指定 npm 包版本或 dist-tag
|
|
15
17
|
# sh scripts/ci/fetch-scripts.sh --no-fallback # 只用起点源,失败不换源
|
|
16
18
|
# sh scripts/ci/fetch-scripts.sh delete-release.sh # 只处理指定脚本(可多个,也可写全路径)
|
|
17
19
|
#
|
|
@@ -19,17 +21,30 @@
|
|
|
19
21
|
# 1) --source <平台> / 环境变量 CI_TEMPLATES_SOURCE 显式指定「起点源」
|
|
20
22
|
# 2) 否则自动探测当前 CI 平台(CNB / GitHub / Gitee / GitLab)作为起点
|
|
21
23
|
# 3) 一个都探测不到(如本地开发):按 CNB → GitHub → Gitee → GitLab 取第一个,即默认 CNB
|
|
22
|
-
# 4)
|
|
23
|
-
#
|
|
24
|
+
# 4) 起点源取不到文件时,其余源按固定顺序 SOURCE_ORDER 依次尝试(四个仓库源始终
|
|
25
|
+
# 排在 npm 源之前),直到拿到或全部失败;回退是「每个文件独立」的,
|
|
26
|
+
# 所以某个源缺某个脚本也能自动从别的源补齐
|
|
24
27
|
# --no-fallback(或 CI_TEMPLATES_NO_FALLBACK=1)可关掉回退,只用起点源
|
|
25
28
|
#
|
|
26
|
-
#
|
|
29
|
+
# 源清单(内容一致,按 SOURCE_ORDER 顺序尝试):
|
|
30
|
+
# ① 仓库源:跟 CI_TEMPLATES_REF(默认 main)走,代码最新
|
|
27
31
|
# CNB :https://cnb.cool/dtapp/ci-templates /-/git/raw/<ref>/<path>
|
|
28
32
|
# GitHub :https://github.com/dtapps/ci-templates raw.githubusercontent.com/dtapps/ci-templates/<ref>/<path>
|
|
29
33
|
# Gitee :https://gitee.com/dtapps/ci-templates /raw/<ref>/<path>(302 到内容域,靠 -L 跟随)
|
|
30
34
|
# GitLab :https://gitlab.com/dtapps/ci-templates /-/raw/<ref>/<path>
|
|
31
|
-
#
|
|
32
|
-
#
|
|
35
|
+
#
|
|
36
|
+
# ② npm 源:跟 npm 包版本走(CI_TEMPLATES_NPM_VERSION,默认 latest 标签)
|
|
37
|
+
# jsdelivr:https://cdn.jsdelivr.net/npm/@dtapp/ci-templates@<ver>/<path>
|
|
38
|
+
# unpkg :https://unpkg.com/@dtapp/ci-templates@<ver>/<path>
|
|
39
|
+
# (https://www.npmjs.com/package/@dtapp/ci-templates 与 app.unpkg.com 只是
|
|
40
|
+
# 包页面/文件浏览界面,真正能按路径取文件的是这两个 CDN)
|
|
41
|
+
# npm :https://registry.npmjs.org/@dtapp/ci-templates/-/ci-templates-<ver>.tgz
|
|
42
|
+
# (官方 registry 没有单文件接口,只能下整包然后用 tar 取单个文件)
|
|
43
|
+
#
|
|
44
|
+
# ③ npm 镜像源:②全部失败才用,国内网络更稳
|
|
45
|
+
# npmmirror:https://registry.npmmirror.com/@dtapp/ci-templates/-/ci-templates-<ver>.tgz
|
|
46
|
+
# tencent :https://mirrors.cloud.tencent.com/npm/@dtapp/ci-templates/-/ci-templates-<ver>.tgz
|
|
47
|
+
# (镜像同样只提供整包 tgz,每次运行每个镜像最多下载一次并复用)
|
|
33
48
|
#
|
|
34
49
|
# 平台探测(只看各平台强特征变量,避免业务仓库里配的 GITEE_TOKEN 之类被误判成 CI):
|
|
35
50
|
# CNB CNB=true,或 CNB_BUILD_ID / CNB_PIPELINE_ID / CNB_BUILD_WORKSPACE 任一非空
|
|
@@ -38,13 +53,20 @@
|
|
|
38
53
|
# GitLab GITLAB_CI=true
|
|
39
54
|
#
|
|
40
55
|
# 环境变量:
|
|
41
|
-
# CI_TEMPLATES_SOURCE cnb | github | gitee | gitlab,等价于 --source
|
|
56
|
+
# CI_TEMPLATES_SOURCE cnb | github | gitee | gitlab | jsdelivr | unpkg | npm | npmmirror | tencent,等价于 --source
|
|
42
57
|
# CI_TEMPLATES_REF 模板仓库分支或 tag,默认 main
|
|
58
|
+
# CI_TEMPLATES_NPM_PACKAGE npm 包名,默认 @dtapp/ci-templates
|
|
59
|
+
# CI_TEMPLATES_NPM_VERSION npm 版本号或 dist-tag(latest / nightly ...),默认 latest
|
|
43
60
|
# CI_TEMPLATES_NO_FALLBACK 非空 = 不回退,只用起点源
|
|
44
61
|
# CI_TEMPLATES_CNB_URL CNB 下载基址(默认 https://cnb.cool/dtapp/ci-templates/-/git/raw)
|
|
45
62
|
# CI_TEMPLATES_GITHUB_URL GitHub 下载基址(默认 https://raw.githubusercontent.com/dtapps/ci-templates)
|
|
46
63
|
# CI_TEMPLATES_GITEE_URL Gitee 下载基址(默认 https://gitee.com/dtapps/ci-templates/raw)
|
|
47
64
|
# CI_TEMPLATES_GITLAB_URL GitLab 下载基址(默认 https://gitlab.com/dtapps/ci-templates/-/raw)
|
|
65
|
+
# CI_TEMPLATES_JSDELIVR_URL jsDelivr 基址(默认 https://cdn.jsdelivr.net/npm)
|
|
66
|
+
# CI_TEMPLATES_UNPKG_URL unpkg 基址(默认 https://unpkg.com)
|
|
67
|
+
# CI_TEMPLATES_NPM_URL 官方 npm registry 基址(默认 https://registry.npmjs.org)
|
|
68
|
+
# CI_TEMPLATES_NPMMIRROR_URL npmmirror 基址(默认 https://registry.npmmirror.com)
|
|
69
|
+
# CI_TEMPLATES_TENCENT_NPM_URL 腾讯云 npm 镜像基址(默认 https://mirrors.cloud.tencent.com/npm)
|
|
48
70
|
# CI_SCRIPTS_ROOT 落盘根目录,默认本脚本所在的仓库根
|
|
49
71
|
# CI_SCRIPTS_TIMEOUT 单文件下载超时秒数,默认 30
|
|
50
72
|
#
|
|
@@ -57,19 +79,29 @@
|
|
|
57
79
|
#
|
|
58
80
|
# 退出码:0 成功;1 有脚本缺失或下载失败;2 用法错误。
|
|
59
81
|
#
|
|
60
|
-
# 依赖:curl(缺失时退到 wget
|
|
82
|
+
# 依赖:curl(缺失时退到 wget);npm 镜像源额外需要 tar(只解包单个文件)。
|
|
83
|
+
# sha256 依次尝试 sha256sum / shasum / openssl dgst,
|
|
61
84
|
# 三者都没有时退化为「直接覆盖」——结果仍是最新,只是不再跳过。
|
|
62
85
|
|
|
63
86
|
set -u
|
|
64
87
|
|
|
65
|
-
# ──
|
|
88
|
+
# ── 各源的下载基址(内容一致,只差平台与取文件方式)─────────────────────
|
|
66
89
|
TEMPLATE_CNB_URL='https://cnb.cool/dtapp/ci-templates/-/git/raw'
|
|
67
90
|
TEMPLATE_GITHUB_URL='https://raw.githubusercontent.com/dtapps/ci-templates'
|
|
68
91
|
TEMPLATE_GITEE_URL='https://gitee.com/dtapps/ci-templates/raw'
|
|
69
92
|
TEMPLATE_GITLAB_URL='https://gitlab.com/dtapps/ci-templates/-/raw'
|
|
93
|
+
TEMPLATE_JSDELIVR_URL='https://cdn.jsdelivr.net/npm'
|
|
94
|
+
TEMPLATE_UNPKG_URL='https://unpkg.com'
|
|
95
|
+
TEMPLATE_NPM_URL='https://registry.npmjs.org'
|
|
96
|
+
TEMPLATE_NPMMIRROR_URL='https://registry.npmmirror.com'
|
|
97
|
+
TEMPLATE_TENCENT_NPM_URL='https://mirrors.cloud.tencent.com/npm'
|
|
70
98
|
|
|
71
99
|
# 回退顺序:探测不到 CI 时取第一个;某个源失败时按同一顺序继续往后切
|
|
72
|
-
|
|
100
|
+
# 仓库源(跟 main,最新)→ npm CDN(跟包版本)→ npm 镜像(兜底,国内可用)
|
|
101
|
+
SOURCE_ORDER='cnb github gitee gitlab jsdelivr unpkg npm npmmirror tencent'
|
|
102
|
+
|
|
103
|
+
# 只有这几个源是「整包 tgz」,其余都能按路径直接取单文件
|
|
104
|
+
TARBALL_SOURCES='npm npmmirror tencent'
|
|
73
105
|
|
|
74
106
|
# ── 需要同步的脚本清单(路径相对模板仓库根,落盘路径与之完全一致)─────────
|
|
75
107
|
# 新增脚本只要在这里加一行,业务仓库无需改动其它地方。
|
|
@@ -94,6 +126,8 @@ die() {
|
|
|
94
126
|
|
|
95
127
|
SOURCE=${CI_TEMPLATES_SOURCE:-}
|
|
96
128
|
REF=${CI_TEMPLATES_REF:-main}
|
|
129
|
+
NPM_PACKAGE=${CI_TEMPLATES_NPM_PACKAGE:-@dtapp/ci-templates}
|
|
130
|
+
NPM_VERSION=${CI_TEMPLATES_NPM_VERSION:-latest}
|
|
97
131
|
TIMEOUT=${CI_SCRIPTS_TIMEOUT:-30}
|
|
98
132
|
FORCE=0
|
|
99
133
|
CHECK_ONLY=0
|
|
@@ -101,6 +135,10 @@ LIST_ONLY=0
|
|
|
101
135
|
FALLBACK=1
|
|
102
136
|
WANTED=
|
|
103
137
|
fs_tmp=
|
|
138
|
+
# npm 整包与版本解析的临时目录(只在用到 registry 源时才真正创建)
|
|
139
|
+
NPM_TMP=${TMPDIR:-/tmp}/ci-templates-npm.$$
|
|
140
|
+
# pid 被复用时可能撞上上次异常退出残留的目录,先清掉,避免复用旧版本的整包/版本号
|
|
141
|
+
[ -e "$NPM_TMP" ] && rm -rf "$NPM_TMP"
|
|
104
142
|
|
|
105
143
|
# 环境变量已开启「不回退」时,后面再遇到 --fallback 也能被命令行改回来
|
|
106
144
|
[ -n "${CI_TEMPLATES_NO_FALLBACK:-}" ] && FALLBACK=0
|
|
@@ -109,7 +147,7 @@ while [ $# -gt 0 ]; do
|
|
|
109
147
|
case "$1" in
|
|
110
148
|
--source | --platform)
|
|
111
149
|
SOURCE=${2:-}
|
|
112
|
-
[ -n "$SOURCE" ] || die "$1 需要一个值:cnb / github / gitlab /
|
|
150
|
+
[ -n "$SOURCE" ] || die "$1 需要一个值:cnb / github / gitee / gitlab / jsdelivr / unpkg / npm / npmmirror / tencent"
|
|
113
151
|
shift
|
|
114
152
|
[ $# -gt 0 ] && shift
|
|
115
153
|
;;
|
|
@@ -119,6 +157,18 @@ while [ $# -gt 0 ]; do
|
|
|
119
157
|
shift
|
|
120
158
|
[ $# -gt 0 ] && shift
|
|
121
159
|
;;
|
|
160
|
+
--npm-version)
|
|
161
|
+
NPM_VERSION=${2:-}
|
|
162
|
+
[ -n "$NPM_VERSION" ] || die "--npm-version 需要一个值(版本号或 dist-tag,如 latest / 0.0.1-nightly.20261126)"
|
|
163
|
+
shift
|
|
164
|
+
[ $# -gt 0 ] && shift
|
|
165
|
+
;;
|
|
166
|
+
--npm-package)
|
|
167
|
+
NPM_PACKAGE=${2:-}
|
|
168
|
+
[ -n "$NPM_PACKAGE" ] || die "--npm-package 需要一个值(如 @dtapp/ci-templates)"
|
|
169
|
+
shift
|
|
170
|
+
[ $# -gt 0 ] && shift
|
|
171
|
+
;;
|
|
122
172
|
--force | -f)
|
|
123
173
|
FORCE=1
|
|
124
174
|
shift
|
|
@@ -184,9 +234,9 @@ if [ -z "$SOURCE" ]; then
|
|
|
184
234
|
fi
|
|
185
235
|
|
|
186
236
|
case "$SOURCE" in
|
|
187
|
-
cnb | github | gitee | gitlab) ;;
|
|
237
|
+
cnb | github | gitee | gitlab | jsdelivr | unpkg | npm | npmmirror | tencent) ;;
|
|
188
238
|
*)
|
|
189
|
-
die "
|
|
239
|
+
die "源只能是 cnb / github / gitee / gitlab / jsdelivr / unpkg / npm / npmmirror / tencent(当前:${SOURCE})"
|
|
190
240
|
;;
|
|
191
241
|
esac
|
|
192
242
|
|
|
@@ -197,34 +247,114 @@ base_url_for() {
|
|
|
197
247
|
github) printf '%s' "${CI_TEMPLATES_GITHUB_URL:-$TEMPLATE_GITHUB_URL}" ;;
|
|
198
248
|
gitee) printf '%s' "${CI_TEMPLATES_GITEE_URL:-$TEMPLATE_GITEE_URL}" ;;
|
|
199
249
|
gitlab) printf '%s' "${CI_TEMPLATES_GITLAB_URL:-$TEMPLATE_GITLAB_URL}" ;;
|
|
250
|
+
jsdelivr) printf '%s' "${CI_TEMPLATES_JSDELIVR_URL:-$TEMPLATE_JSDELIVR_URL}" ;;
|
|
251
|
+
unpkg) printf '%s' "${CI_TEMPLATES_UNPKG_URL:-$TEMPLATE_UNPKG_URL}" ;;
|
|
252
|
+
npm) printf '%s' "${CI_TEMPLATES_NPM_URL:-$TEMPLATE_NPM_URL}" ;;
|
|
253
|
+
npmmirror) printf '%s' "${CI_TEMPLATES_NPMMIRROR_URL:-$TEMPLATE_NPMMIRROR_URL}" ;;
|
|
254
|
+
tencent) printf '%s' "${CI_TEMPLATES_TENCENT_NPM_URL:-$TEMPLATE_TENCENT_NPM_URL}" ;;
|
|
255
|
+
esac
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
# 这个源是不是「只能下整包 tgz」的 registry 源
|
|
259
|
+
is_tarball_source() {
|
|
260
|
+
case " $TARBALL_SOURCES " in
|
|
261
|
+
*" $1 "*) return 0 ;;
|
|
200
262
|
esac
|
|
263
|
+
return 1
|
|
201
264
|
}
|
|
202
265
|
|
|
203
|
-
#
|
|
266
|
+
# 纯下载:文件非空即成功,不做脚本格式校验(元数据 / tgz 也走这里)
|
|
267
|
+
download() {
|
|
268
|
+
fs_dl_url=$1
|
|
269
|
+
fs_dl_out=$2
|
|
270
|
+
[ -d "${fs_dl_out%/*}" ] || mkdir -p "${fs_dl_out%/*}" || return 1
|
|
271
|
+
|
|
272
|
+
if command -v curl >/dev/null 2>&1; then
|
|
273
|
+
curl -fsSL --connect-timeout 10 --max-time "$TIMEOUT" "$fs_dl_url" -o "$fs_dl_out" 2>/dev/null \
|
|
274
|
+
|| {
|
|
275
|
+
rm -f "$fs_dl_out"
|
|
276
|
+
return 1
|
|
277
|
+
}
|
|
278
|
+
elif command -v wget >/dev/null 2>&1; then
|
|
279
|
+
wget -q -T "$TIMEOUT" -O "$fs_dl_out" "$fs_dl_url" \
|
|
280
|
+
|| {
|
|
281
|
+
rm -f "$fs_dl_out"
|
|
282
|
+
return 1
|
|
283
|
+
}
|
|
284
|
+
else
|
|
285
|
+
warn '需要 curl 或 wget 才能下载模板脚本'
|
|
286
|
+
return 1
|
|
287
|
+
fi
|
|
288
|
+
|
|
289
|
+
if [ ! -s "$fs_dl_out" ]; then
|
|
290
|
+
rm -f "$fs_dl_out"
|
|
291
|
+
return 1
|
|
292
|
+
fi
|
|
293
|
+
return 0
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
# 拼下载地址:
|
|
297
|
+
# 仓库源 <base>/<ref>/<path>
|
|
298
|
+
# npm CDN <base>/<包名>@<版本或 tag>/<path>
|
|
299
|
+
# 镜像源 <base>/<包名>/-/<包名末段>-<具体版本>.tgz(版本稍后解析)
|
|
204
300
|
source_url() {
|
|
205
|
-
|
|
206
|
-
|
|
301
|
+
fs_url_base=$(base_url_for "$1")
|
|
302
|
+
fs_url_base=${fs_url_base%/}
|
|
303
|
+
case "$1" in
|
|
304
|
+
jsdelivr | unpkg)
|
|
305
|
+
printf '%s/%s@%s/%s' "$fs_url_base" "$NPM_PACKAGE" "$NPM_VERSION" "$2"
|
|
306
|
+
;;
|
|
307
|
+
npm | npmmirror | tencent)
|
|
308
|
+
fs_url_ver=$(npm_version_for "$1" || true)
|
|
309
|
+
[ -n "$fs_url_ver" ] || return 1
|
|
310
|
+
printf '%s/%s/-/%s-%s.tgz' "$fs_url_base" "$NPM_PACKAGE" "${NPM_PACKAGE##*/}" "$fs_url_ver"
|
|
311
|
+
;;
|
|
312
|
+
*)
|
|
313
|
+
printf '%s/%s/%s' "$fs_url_base" "$REF" "$2"
|
|
314
|
+
;;
|
|
315
|
+
esac
|
|
207
316
|
}
|
|
208
317
|
|
|
209
|
-
#
|
|
318
|
+
# 把 latest / dist-tag 解析成具体版本号(registry 的整包路径不接受 tag)
|
|
319
|
+
npm_version_for() {
|
|
320
|
+
case "$NPM_VERSION" in
|
|
321
|
+
[0-9]*) printf '%s\n' "$NPM_VERSION" && return 0 ;;
|
|
322
|
+
esac
|
|
323
|
+
|
|
324
|
+
fs_ver_reg=$(base_url_for "$1")
|
|
325
|
+
fs_ver_reg=${fs_ver_reg%/}
|
|
326
|
+
|
|
327
|
+
[ -d "$NPM_TMP" ] || mkdir -p "$NPM_TMP" || return 1
|
|
328
|
+
|
|
329
|
+
fs_ver_cache="$NPM_TMP/version.$1"
|
|
330
|
+
if [ -s "$fs_ver_cache" ]; then
|
|
331
|
+
cat "$fs_ver_cache"
|
|
332
|
+
return 0
|
|
333
|
+
fi
|
|
334
|
+
|
|
335
|
+
fs_ver_json="$NPM_TMP/meta.$1.$$"
|
|
336
|
+
if ! download "${fs_ver_reg}/${NPM_PACKAGE}/${NPM_VERSION}" "$fs_ver_json"; then
|
|
337
|
+
warn "源 $1 解析版本失败(${NPM_PACKAGE}@${NPM_VERSION})"
|
|
338
|
+
return 1
|
|
339
|
+
fi
|
|
340
|
+
fs_ver=$(grep -o '"version"[[:space:]]*:[[:space:]]*"[^"]*"' "$fs_ver_json" 2>/dev/null \
|
|
341
|
+
| head -n 1 | sed 's/.*"\([^"]*\)"$/\1/')
|
|
342
|
+
rm -f "$fs_ver_json"
|
|
343
|
+
[ -n "$fs_ver" ] || return 1
|
|
344
|
+
printf '%s\n' "$fs_ver" >"$fs_ver_cache"
|
|
345
|
+
printf '%s\n' "$fs_ver"
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
# 候选源:起点源打头,其余按 SOURCE_ORDER 固定顺序(仓库源始终在 npm 源之前);
|
|
349
|
+
# 关掉回退时只留起点源
|
|
210
350
|
source_candidates() {
|
|
211
351
|
if [ "$FALLBACK" != '1' ]; then
|
|
212
352
|
printf '%s\n' "$SOURCE"
|
|
213
353
|
return 0
|
|
214
354
|
fi
|
|
215
|
-
|
|
216
|
-
fs_tail=
|
|
217
|
-
fs_hit=0
|
|
355
|
+
printf '%s\n' "$SOURCE"
|
|
218
356
|
for fs_s in $SOURCE_ORDER; do
|
|
219
|
-
|
|
220
|
-
fs_tail="${fs_tail}${fs_tail:+ }$fs_s"
|
|
221
|
-
elif [ "$fs_s" = "$SOURCE" ]; then
|
|
222
|
-
fs_hit=1
|
|
223
|
-
else
|
|
224
|
-
fs_head="${fs_head}${fs_head:+ }$fs_s"
|
|
225
|
-
fi
|
|
226
|
-
done
|
|
227
|
-
for fs_s in $SOURCE $fs_tail $fs_head; do
|
|
357
|
+
[ "$fs_s" != "$SOURCE" ] || continue
|
|
228
358
|
printf '%s\n' "$fs_s"
|
|
229
359
|
done
|
|
230
360
|
}
|
|
@@ -266,7 +396,7 @@ selected() {
|
|
|
266
396
|
}
|
|
267
397
|
|
|
268
398
|
if [ "$LIST_ONLY" = '1' ]; then
|
|
269
|
-
info "起点源:${SOURCE}(ref=${REF})"
|
|
399
|
+
info "起点源:${SOURCE}(ref=${REF};npm 包=${NPM_PACKAGE}@${NPM_VERSION})"
|
|
270
400
|
info "源顺序:$(source_candidates | tr '\n' ' ')"
|
|
271
401
|
info "示例:$(source_url "$SOURCE" "$(manifest | head -n 1)")"
|
|
272
402
|
info "落盘目录:${ROOT_DIR}"
|
|
@@ -281,7 +411,10 @@ if [ -n "$WANTED" ]; then
|
|
|
281
411
|
done
|
|
282
412
|
fi
|
|
283
413
|
|
|
284
|
-
cleanup() {
|
|
414
|
+
cleanup() {
|
|
415
|
+
[ -n "${fs_tmp:-}" ] && rm -f "$fs_tmp"
|
|
416
|
+
[ -n "${NPM_TMP:-}" ] && rm -rf "$NPM_TMP"
|
|
417
|
+
}
|
|
285
418
|
trap cleanup EXIT HUP INT TERM
|
|
286
419
|
|
|
287
420
|
# 计算文件 sha256;没有可用工具时返回 1
|
|
@@ -311,37 +444,39 @@ content_state() {
|
|
|
311
444
|
}
|
|
312
445
|
|
|
313
446
|
# 只下载到临时文件(fs_tmp),不落盘;由调用方决定 mv 还是丢弃
|
|
447
|
+
# fetch <源> <脚本相对路径> <落盘路径>
|
|
314
448
|
fetch() {
|
|
315
|
-
|
|
316
|
-
|
|
449
|
+
fs_source=$1
|
|
450
|
+
fs_rel=$2
|
|
451
|
+
fs_dest=$3
|
|
317
452
|
fs_dir=${fs_dest%/*}
|
|
318
453
|
fs_tmp="$fs_dest.tmp.$$"
|
|
319
454
|
|
|
320
455
|
[ -d "$fs_dir" ] || mkdir -p "$fs_dir" || return 1
|
|
321
456
|
|
|
322
|
-
if
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
457
|
+
if is_tarball_source "$fs_source"; then
|
|
458
|
+
# registry 镜像没有单文件接口:整包下一次,用 tar 直接解出单个文件
|
|
459
|
+
fs_tgz=$(tarball_for "$fs_source") || return 1
|
|
460
|
+
if ! command -v tar >/dev/null 2>&1; then
|
|
461
|
+
warn '需要 tar 才能从 npm 整包里取脚本'
|
|
462
|
+
return 1
|
|
463
|
+
fi
|
|
464
|
+
if ! tar -xzOf "$fs_tgz" "package/${fs_rel}" >"$fs_tmp" 2>/dev/null; then
|
|
465
|
+
rm -f "$fs_tmp"
|
|
466
|
+
fs_tmp=
|
|
467
|
+
return 1
|
|
468
|
+
fi
|
|
469
|
+
if [ ! -s "$fs_tmp" ]; then
|
|
470
|
+
rm -f "$fs_tmp"
|
|
471
|
+
fs_tmp=
|
|
472
|
+
return 1
|
|
473
|
+
fi
|
|
336
474
|
else
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
rm -f "$fs_tmp"
|
|
343
|
-
fs_tmp=
|
|
344
|
-
return 1
|
|
475
|
+
fs_url=$(source_url "$fs_source" "$fs_rel") || return 1
|
|
476
|
+
if ! download "$fs_url" "$fs_tmp"; then
|
|
477
|
+
fs_tmp=
|
|
478
|
+
return 1
|
|
479
|
+
fi
|
|
345
480
|
fi
|
|
346
481
|
|
|
347
482
|
# 防止把 404 页面 / 网关错误页当成脚本落盘
|
|
@@ -358,6 +493,27 @@ fetch() {
|
|
|
358
493
|
return 0
|
|
359
494
|
}
|
|
360
495
|
|
|
496
|
+
# registry 源的整包:第一次调用下载,之后复用同一个 tgz
|
|
497
|
+
tarball_for() {
|
|
498
|
+
fs_tb_ver=$(npm_version_for "$1") || return 1
|
|
499
|
+
[ -n "$fs_tb_ver" ] || return 1
|
|
500
|
+
|
|
501
|
+
[ -d "$NPM_TMP" ] || mkdir -p "$NPM_TMP" || return 1
|
|
502
|
+
|
|
503
|
+
fs_tb_tgz="$NPM_TMP/$1.tgz"
|
|
504
|
+
if [ ! -s "$fs_tb_tgz" ]; then
|
|
505
|
+
fs_tb_url=$(base_url_for "$1")
|
|
506
|
+
fs_tb_url=$(printf '%s/%s/-/%s-%s.tgz' \
|
|
507
|
+
"${fs_tb_url%/}" "$NPM_PACKAGE" "${NPM_PACKAGE##*/}" "$fs_tb_ver")
|
|
508
|
+
if ! download "$fs_tb_url" "$fs_tb_tgz"; then
|
|
509
|
+
warn "源 $1 整包下载失败:${fs_tb_url}"
|
|
510
|
+
rm -f "$fs_tb_tgz"
|
|
511
|
+
return 1
|
|
512
|
+
fi
|
|
513
|
+
fi
|
|
514
|
+
printf '%s\n' "$fs_tb_tgz"
|
|
515
|
+
}
|
|
516
|
+
|
|
361
517
|
fs_created=0
|
|
362
518
|
fs_updated=0
|
|
363
519
|
fs_same=0
|
|
@@ -367,7 +523,7 @@ fs_failed=0
|
|
|
367
523
|
CANDIDATES=$(source_candidates)
|
|
368
524
|
CANDIDATE_COUNT=$(printf '%s\n' "$CANDIDATES" | grep -c .)
|
|
369
525
|
|
|
370
|
-
info "起点源:${SOURCE}(ref=${REF}) → ${ROOT_DIR}"
|
|
526
|
+
info "起点源:${SOURCE}(ref=${REF};npm 包=${NPM_PACKAGE}@${NPM_VERSION}) → ${ROOT_DIR}"
|
|
371
527
|
if [ "$FALLBACK" = '1' ]; then
|
|
372
528
|
info "失败回退顺序:$(printf '%s' "$CANDIDATES" | tr '\n' ' ')"
|
|
373
529
|
fi
|
|
@@ -395,8 +551,7 @@ while IFS= read -r fs_rel; do
|
|
|
395
551
|
fs_ok=0
|
|
396
552
|
fs_used=
|
|
397
553
|
for fs_src in $CANDIDATES; do
|
|
398
|
-
|
|
399
|
-
if fetch "$fs_url" "$fs_dest"; then
|
|
554
|
+
if fetch "$fs_src" "$fs_rel" "$fs_dest"; then
|
|
400
555
|
fs_ok=1
|
|
401
556
|
fs_used=$fs_src
|
|
402
557
|
break
|
|
@@ -466,8 +621,8 @@ if [ "$CHECK_ONLY" = '1' ]; then
|
|
|
466
621
|
fi
|
|
467
622
|
|
|
468
623
|
if [ "$fs_failed" -ne 0 ]; then
|
|
469
|
-
warn "有 ${fs_failed} 个脚本在所有源都取不到(起点:${SOURCE},ref:${REF})"
|
|
470
|
-
warn "可指定源重试:sh $0 --source <cnb|github|gitee|gitlab> --force"
|
|
624
|
+
warn "有 ${fs_failed} 个脚本在所有源都取不到(起点:${SOURCE},ref:${REF},npm:${NPM_PACKAGE}@${NPM_VERSION})"
|
|
625
|
+
warn "可指定源重试:sh $0 --source <cnb|github|gitee|gitlab|jsdelivr|unpkg|npm|npmmirror|tencent> --force"
|
|
471
626
|
exit 1
|
|
472
627
|
fi
|
|
473
628
|
|