--
:
--
:
--
hugo-teek is loading...
1350 字
7 分钟
-- 次
cnb私有库发布到eop反代
最后更新于:
cnb私有库发布到eop反代

目录
[toc]
场景1:cnb公开库发布到eop反代(测试成功)
.cnb.yml代码:
1master:
2 push:
3 "volume-cache":
4 name: 云原生构建
5 runner:
6 cpus: 8
7 imports:
8 - https://cnb.cool/onedayxyy/secret/-/blob/main/envs.yml
9 docker:
10 image: docker.cnb.cool/yuwen-gueen/docker-images-chrom/hugo-teek-blog:latest
11 volumes:
12 - node_modules:copy-on-write
13 env:
14 # 产物想要存入的分支名
15 DIST_BRANCH: dist
16 stages:
17 - name: set env
18 script: echo -n $(date +%s)
19 exports:
20 info: CUSTOM_ENV_START_TIME
21 - name: check
22 script: |
23 git fetch origin --prune --tags
24 #- name: install
25 # script: |
26 # apt update
27 # apt install -y rsync
28 # npm i -g pnpm
29 # pnpm i
30 - name: check branch
31 script: |
32 if ! git ls-remote --exit-code --heads origin ${DIST_BRANCH}; then
33 echo "Remote branch '${DIST_BRANCH}' not found, creating it..."
34 git checkout --orphan ${DIST_BRANCH}
35 git rm -rf .
36 echo "# ${DIST_BRANCH} initialized" > README.md
37 git add README.md
38 git commit -m "chore: init ${DIST_BRANCH} branch"
39 git push origin ${DIST_BRANCH}
40 git checkout -
41 fi
42 - name: 构建
43 script: |
44 make build-docker
45 git fetch origin ${DIST_BRANCH}:${DIST_BRANCH}
46 rm -rf .deploy-tmp
47 git worktree add .deploy-tmp ${DIST_BRANCH}
48 find .deploy-tmp -mindepth 1 \
49 -not -name ".git" \
50 -not -name ".gitkeep" \
51 -exec rm -rf {} +
52 cp -rf ./hugo-teek-site/public/* ./.deploy-tmp/
53 - name: 部署
54 script: |
55 cd ./.deploy-tmp/
56 git add -A
57 if ! git diff --cached --quiet; then
58 git commit -m "deploy: $(date '+%Y-%m-%d %H:%M:%S')" --allow-empty || echo "Nothing to commit"
59 git push origin ${DIST_BRANCH}
60 else
61 echo "No changes to deploy"
62 fi
63 cd ..
64 if [ -d ./.deploy-tmp/.git ]; then
65 git worktree remove .deploy-tmp --force
66 fi
67 - name: ⏱️ 计算耗时
68 script: |
69 # 计算耗时
70 end_time=$(date +%s)
71 duration=$((end_time - $CUSTOM_ENV_START_TIME))
72 minutes=$((duration / 60))
73 seconds=$((duration % 60))
74 time_str="${minutes}分${seconds}秒"
75 echo -n ${time_str}
76 exports:
77 info: CUSTOM_ENV_BUILD_TIME
78
79
80 - name: 钉钉通知
81 image: docker.cnb.cool/yuwen-gueen/docker-images-chrom/tencentcom-dingtalk-bot-msg:latest_amd64
82 settings:
83 content: "「Hugo-Teek Blog」发布完成,耗时: ${CUSTOM_ENV_BUILD_TIME}"
84 c_type: "text" # 支持: text, markdown, link, actionCard, multiActionCard, feedCard
85 secret: $SECRET
86 webhook: $WEBHOOK
87 isAtAll: false
88 debug: false # 新增: 是否启用调试模式
89include:
90- .cnb/vscode.yml
场景2:cnb私有库发布到eop反代(测试成功)
(1)基础信息:
1仓库:https://cnb.cool/onedayxyy/hugo-teek-private
2hugo-teek-private 私有库 (存放hugo-tee源码及核心Md文档)
3
4仓库:https://cnb.cool/onedayxyy/hugo-teek-public-dist
5hugo-teek-public-dist 公开库(存放dist构建产物)
(2).cnb.yml代码:
1master:
2 push:
3 "volume-cache":
4 name: 云原生构建
5 runner:
6 cpus: 8
7 imports:
8 - https://cnb.cool/onedayxyy/secret/-/blob/main/envs.yml
9 docker:
10 image: docker.cnb.cool/yuwen-gueen/docker-images-chrom/hugo-teek-blog:latest
11 volumes:
12 - node_modules:copy-on-write
13 env:
14 # 产物将要被推送到的目标仓库 (公开库)
15 TARGET_REPO: "https://cnb.cool/onedayxyy/hugo-teek-public-dist.git"
16 # 产物在目标仓库中的分支名
17 DIST_BRANCH: "dist"
18 stages:
19 - name: set env
20 script: echo -n $(date +%s)
21 exports:
22 info: CUSTOM_ENV_START_TIME
23
24 - name: 配置 Git
25 script: |
26 # 确保 Git 有一个用户身份,这在 CI 环境中很重要
27 git config --global user.name "CNB Bot"
28 git config --global user.email "bot@cnb.cool"
29
30 # 检查是否已存在名为 'public' 的远程仓库,如果存在则更新 URL
31 if git remote | grep -q "public"; then
32 git remote set-url public "$TARGET_REPO"
33 else
34 # 否则,添加 'public' 作为指向公开库的远程仓库
35 git remote add public "$TARGET_REPO"
36 fi
37
38 - name: check
39 script: |
40 # 拉取私有库 (origin) 的最新代码
41 git fetch origin --prune --tags
42 # 也拉取一下公开库 (public) 的最新代码,特别是 dist 分支
43 git fetch public --prune --tags
44
45 - name: check and create branch on public repo
46 script: |
47 # 检查公开库的 'dist' 分支是否存在
48 if ! git ls-remote --exit-code --heads public "${DIST_BRANCH}"; then
49 echo "Remote branch '${DIST_BRANCH}' not found in public repo, creating it..."
50 # 创建一个孤立的本地分支,没有任何历史
51 git checkout --orphan "${DIST_BRANCH}"
52 # 删除所有本地文件,因为这是一个全新的、只包含产物的分支
53 git rm -rf .
54 echo "# ${DIST_BRANCH} branch for public distribution" > README.md
55 git add README.md
56 git commit -m "chore: init ${DIST_BRANCH} branch"
57 # 将这个新分支推送到公开库 (public)
58 git push public "${DIST_BRANCH}"
59 # 切换回原来的工作分支 (master)
60 git checkout master
61 fi
62
63 - name: 构建
64 script: |
65 # 执行你的构建命令
66 make build-docker
67
68 # 确保本地有 'dist' 分支的最新版本
69 git checkout "${DIST_BRANCH}"
70 git pull public "${DIST_BRANCH}"
71 git checkout master
72
73 # 使用 git worktree 将 'dist' 分支的内容 checkout 到 .deploy-tmp 目录
74 rm -rf .deploy-tmp
75 git worktree add .deploy-tmp "${DIST_BRANCH}"
76
77 # 清空 .deploy-tmp 目录下的所有内容,准备放入新的构建产物
78 find .deploy-tmp -mindepth 1 \
79 -not -name ".git" \
80 -exec rm -rf {} +
81
82 # 将构建好的产物复制到 .deploy-tmp
83 cp -rf ./hugo-teek-site/public/* ./.deploy-tmp/
84
85 - name: 部署到公开库
86 script: |
87 cd ./.deploy-tmp/
88
89 # 检查是否有文件变化
90 git add -A
91 if ! git diff --cached --quiet; then
92 git commit -m "deploy: $(date '+%Y-%m-%d %H:%M:%S')"
93 # 将更改推送到公开库 (public) 的 'dist' 分支
94 git push public "${DIST_BRANCH}"
95 else
96 echo "No changes to deploy"
97 fi
98
99 cd ..
100 # 清理 worktree
101 if [ -d ./.deploy-tmp ]; then
102 git worktree remove .deploy-tmp --force
103 fi
104
105 - name: ⏱️ 计算耗时
106 script: |
107 end_time=$(date +%s)
108 duration=$((end_time - $CUSTOM_ENV_START_TIME))
109 minutes=$((duration / 60))
110 seconds=$((duration % 60))
111 time_str="${minutes}分${seconds}秒"
112 echo -n ${time_str}
113 exports:
114 info: CUSTOM_ENV_BUILD_TIME
115
116 - name: 钉钉通知
117 image: docker.cnb.cool/yuwen-gueen/docker-images-chrom/tencentcom-dingtalk-bot-msg:latest_amd64
118 settings:
119 content: "「hugo-teek-private Blog」发布完成,耗时: ${CUSTOM_ENV_BUILD_TIME}"
120 c_type: "text"
121 secret: $SECRET
122 webhook: $WEBHOOK
123 isAtAll: false
124 debug: false
125
126include:
127- .cnb/vscode.yml
(3)eop反代云函数
下载地址:https://img.onedayxyy.cn/images/20251124-hugo-teek-private-functions.zip

(4)验证
https://hugo-teek.onedayxyy.cn/



存在问题的场景
问题1:(这种方式也换好,没啥问题)
cnb 私有库反代,又发现一个问题,别人拿着我们的 dist 开源库可以直接部署到自己 cnb,那么相当于直接把我们的网站做了一个克隆,这个很不友好哇。 有啥办法能解决嘛?
问题2:(建议把dist放到云服务器,然后eop反代云服务器)
私密 就是某个域名想只有自己给给特定的人分享,不想给其他人看。 那么dist放在开源库就不核实,感觉只能放在云服务器了。
结束。
📡
👤
作者:
余温Gueen
🌐
版权:
本站文章除特别声明外,均采用
CC BY-NC-SA 4.0
协议,转载请注明来自
余温Gueen Blog!
推荐使用微信支付

推荐使用支付宝
