hugo-teek is loading...

CNB云端构建静态博客

最后更新于:

实战-cnb云端构建并传输静态文件到服务器-20250401(测试成功)

目录

[[toc]]

[toc]

版权

二丫讲梵指导,自己测试。

来源于: 《二丫-JenkinsGuide》https://cnb.cool/opsre/JenkinsGuide

前言

全网最美博客-teek(知识库&博客二合一),你已经拥有了。最丝滑的markdown编辑器-typora你也在用。

此时,如果你只需要在本地仓库维护数据,一键提交后,就能触发流水线,自动构建,生成dist内容,然后自动传到你服务器nginx站点目录。

这个工作流是不是很丝滑,那么以下讲的cnb.cool将能完全满足你的需求。😜

前提

2025年4月3日测试成功。

前提条件:

- 具备git环境;

- 具有自己的云服务器;

- 具有[teek网站](https://onedayxyy.cn/teek-online-install)环境;

1、创建组织

  • 来到https://cnb.cool/网站,使用微信登录,按相关要求创建组织:

警告

这里好像是要拥有一个自己的域名来着……,然后验证域名归属,再给自己的组织起一个名称。

image-20250401191611715

image-20250401191640109

2、创建仓库

  • (1)以上组织创建好后,我们开始创建一个仓库:(本次创建的是一个私有库)

https://cnb.cool/onedayxyy/vitepress-theme-teek-one-private.git

image-20250401192019953

  • 初始化本地仓库,并将自己本地库推送到cnb.cool远程仓库

本次按 方式 3:空仓初始化 来初始化自己的本地仓库:

image-20250401192410469

1git init .
2git remote add origin https://cnb.cool/onedayxyy/vitepress-theme-teek-one-private.git
3git config --local user.name cnb.aYHw4hqhwFA
4git config --local user.email "Z0KUuiPxqUfIPDstra7UrF+cnb.aYHw4hqhwFA@noreply.cnb.cool"
5git config credential.helper store
  • (2)创建ssh令牌密钥:

自己先生成一个token:

image-20250401200602211

image-20250401193421045

警告

自己一定要记录号刚才生成的密钥:(后续需要用到的)

![image-20250401214732067](https://img.onedayxyy.cn/images/image-20250401214732067.png)

  • (3)推送本地现有仓库:
 1#因为自己本地已经有了teek仓库,这里我需要先删除仓库里的.git,再重新初始化仓库。
 2cd /d/vitepress-theme-teek-one-private
 3rm -rf .git 
 4git init
 5git add -A
 6git commit -m"first commit"
 7git remote add origin https://cnb.cool/onedayxyy/vitepress-theme-teek-one-private.git
 8#git remote add origin git@gitee.com:onlyonexl/vitepress-theme-teek-one-private.git
 9#git push -u origin "master" --force
10git config credential.helper store
11# 作用:启用 Git 凭据存储,避免每次推送时重复输入账号密码。
12# 关键点:
13# 首次推送需输入用户名/密码,后续会自动保存在 ~/.git-credentials。
14# 仅推荐在 安全环境 下使用(若为公共电脑,建议用 cache 临时存储)。
15
16git push -u origin "master"

特别注意:

执行`git config credential.helper store`后,会自动弹出一个git终端,此时输入前面创建好的密钥就好。

image-20250401201544336

3、配置文件

以上代码推送到仓库后,在本地仓库里,编辑如下2个文件:

  • (1)创建.cnb.yml

具体代码:

  • (2)创建env.yaml

具体代码:

在自己云服务器上生成ssh密钥,将私钥拷贝到`env.yaml`的`SSH_KEY`里。

编辑内容:

![image-20250401201911376](https://img.onedayxyy.cn/images/image-20250401201911376.png)

4、运行验证

  • 以上编辑完成后,提交代码,第一次提交后,就可以看到已经触达流水线了:
1git add -A
2git commit -m"更新cnb-cool配置,第一次构建测试"
3git push

image-20250401202316796

观察构建过程:

image-20250401202333013

image-20250401215211746

  • 完美:

image-20250401202640607

image-20250401213044288

可以看到,自己网站也更新成功了,丝滑。😜

扩展

扩展1:条件触发自动构建

提示

次步骤是在上面步骤基础上操作的哦!

到此为止,上面流程已经很完美了,但是存在一个问题:

如何只推送仓库,且不构建?或者 如何推送时 加上某个参数后 才会进行构建?(频繁构建很不友好……)

哈哈,我已经准备好手把手文档了:

(1)编辑.cnb.yml

具体代码:

说明:

CNB_COMMIT_MESSAGE_TITLE 这个变量是你提交的message

https://docs.cnb.cool/zh/build-in-env.html#CNB_COMMIT_MESSAGE_TITLE

可以结合这个if实现你的需求:

https://docs.cnb.cool/zh/grammar/stage.html#if

(2)配置完成后,测试效果即可

 1##以下代码在vscode里执行,具体路径替换为你项目路径
 2
 3#只推送代码
 4cat >>/etc/profile <<EOF
 5alias gg="
 6cd /d/vitepress-theme-teek-one-private/
 7git status
 8git pull
 9git add -A
10git commit -m'commit data'
11git push
12git status
13"
14EOF
15
16
17#推送代码同时触发自动构建
18cat >>/etc/profile <<EOF
19alias gg2="
20cd /d/vitepress-theme-teek-one-private/
21git status
22git pull
23git add -A
24git commit -m'BUILD'
25git push
26git status
27"
28EOF
29
30
31source /etc/profile

执行gg效果:

执行gg2效果:

完美。

扩展2:自动构建后刷新阿里云cdn、自动同步代码到gitee仓库

2025年4月3日测试成功。

本次代码更改,新增自动构建后刷新阿里云cdn、自动同步代码到gitee仓库功能哦,这样,咋们的pipeline流水线更加丝滑了哦。😊

(1)编辑.cnb.yml

增加如下代码:

 1            prescript:
 2              - echo "prescript"
 3              - ls -l  /root/rsync/rsync-vitepress/dist/
 4            script:
 5              - echo "after script"
 6              - ls -l /root/rsync/rsync-vitepress/dist/
 7              - date
 8              - aliyun cdn RefreshObjectCaches --ObjectType File --ObjectPath "onedayxyy.cn/" #刷新阿里云cdn缓存
 9
10        #同步仓库到gitee                  
11        - name: sync to gitee
12          image: tencentcom/git-sync
13          imports: https://cnb.cool/onedayxyy/vitepress-theme-teek-one-private/-/blob/master/env.yaml
14          settings:
15            branch: master
16            auth_type: https
17            username: ${GIT_USERNAME}
18            password: ${GIT_ACCESS_TOKEN}
19            target_url: https://gitee.com/onlyonexl/vitepress-theme-teek-one-private.git
20            # git_email: 'github-actions[bot]@users.noreply.github.com'

::: deatils 详细代码:

 1master:
 2  push:
 3    - runner:
 4        cpus: 16
 5      services:
 6        - docker
 7        - git-clone-yyds
 8      docker:
 9        image: docker.cnb.cool/znb/images/node:18
10        volumes:
11          - /data/.cache:copy-on-write #声明式的构建缓存
12      stages:
13        - name: 🖨️ 打印环境
14          if: |
15            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]
16          script: |
17            node -v && npm -v && yarn -v && pnpm -v
18        - name: 📦️ 安装依赖 
19          if: |
20            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]          
21          script: |
22            pnpm install
23            
24        - name: ⚗️ 编译项目
25          if: |
26            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]        
27          script: |
28            pnpm docs:build   # VitePress 专用命令
29        - name: 🚚 发布制品
30          if: |
31            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]            
32          image: tencentcom/rsync
33          imports: https://cnb.cool/onedayxyy/vitepress-theme-teek-one-private/-/blob/master/env.yaml
34          settings:
35            user: ${SSH_USER}
36            key: ${SSH_KEY}
37            port: 22
38            hosts:
39              - ${ECS_IP}
40            source: docs/.vitepress/dist/
41            target: /root/rsync/rsync-vitepress/dist/
42            delete: true
43            prescript:
44              - echo "prescript"
45              - ls -l  /root/rsync/rsync-vitepress/dist/
46            script:
47              - echo "after script"
48              - ls -l /root/rsync/rsync-vitepress/dist/
49              - date
50              - aliyun cdn RefreshObjectCaches --ObjectType File --ObjectPath "onedayxyy.cn/" #刷新阿里云cdn缓存
51
52        #同步仓库到gitee                  
53        - name: sync to gitee
54          image: tencentcom/git-sync
55          imports: https://cnb.cool/onedayxyy/vitepress-theme-teek-one-private/-/blob/master/env.yaml
56          settings:
57            branch: master
58            auth_type: https
59            username: ${GIT_USERNAME}
60            password: ${GIT_ACCESS_TOKEN}
61            target_url: https://gitee.com/onlyonexl/vitepress-theme-teek-one-private.git
62            # git_email: 'github-actions[bot]@users.noreply.github.com'

:::

(2)编辑env.yaml

 1#gitee token
 2GIT_USERNAME: gitee账户登录名
 3GIT_ACCESS_TOKEN: gitee账户token
 4
 5
 6ECS_IP: 云服务器ip
 7SSH_USER: 云服务器登录用户   
 8SSH_KEY: |
 9    -----BEGIN RSA PRIVATE KEY-----
10    MIIEpAIBAAKCAQEAwWunMrptJsXbxx0M17w3qaHyJb9B/CN9PgeNE9KoyN26//nN
11	……
12    EqbXIgV6SEJtBLQlzkOS0BR2hGZl5Y99aA15vcub+AafBAzOeAL8ig==
13    -----END RSA PRIVATE KEY-----

(3)修改完成后,提交代码,触发自动构建,观察效果

可以纵向丝滑哦。😍

扩展3:使用秘钥仓库

image-20250721064052113

扩展4:全局导入环境变量

2025年7月23日更新

最新代码:

.cnb.yml文件

 1master:
 2  push:
 3    - runner:
 4        cpus: 16
 5      services:
 6        - docker
 7        - git-clone-yyds
 8      imports:
 9        - imports: https://cnb.cool/onedayxyy/secret/-/blob/main/envs.yml     
10      docker:
11        image: docker.cnb.cool/znb/images/node:18
12        volumes:
13          - /data/.cache:copy-on-write #声明式的构建缓存
14      stages:
15        - name: 🖨️ 打印环境
16          if: |
17            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]
18          script: |
19            node -v && npm -v && yarn -v && pnpm -v
20        - name: 📦️ 安装依赖 
21          if: |
22            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]          
23          script: |
24            pnpm install
25            
26        - name: ⚗️ 编译项目
27          if: |
28            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]        
29          script: |
30            pnpm docs:build   # VitePress 专用命令
31        - name: 🚚 发布制品
32          if: |
33            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]            
34          image: tencentcom/rsync
35          settings:
36            user: ${SSH_USER}
37            key: ${SSH_KEY}
38            port: 22
39            hosts:
40              - ${ECS_IP}
41            source: docs/.vitepress/dist/
42            target: /root/rsync/rsync-vitepress/dist/
43            delete: true
44            prescript:
45              - echo "prescript"
46              - ls -l  /root/rsync/rsync-vitepress/dist/
47            # script:
48            #   - echo "after script"
49            #   - ls -l /root/rsync/rsync-vitepress/dist/
50            #   - date
51            #   # - bash  /root/website_music.sh
52            #   - aliyun cdn RefreshObjectCaches --ObjectType File --ObjectPath "onedayxyy.cn/" #刷新阿里云cdn缓存
53
54        # - name: 🔔 发布通知
55        #   image: tencentcom/wecom-message
56        #   settings:
57        #     robot: ${WECOM_BOT}
58        #     msgType: markdown
59        #     content: |
60        #       > **🎉 JenkinsGuide 又一次发布啦!**
61        #       > **构建时间:** $CUSTOM_ENV_DATE_INFO
62        #       > **提交信息:** $CNB_COMMIT_MESSAGE_TITLE
63        #       > **仓库地址:** [$CNB_REPO_URL_HTTPS]($CNB_REPO_URL_HTTPS)
64
65        - name: 🧘‍♂️ 刷新缓存
66          image: docker.cnb.cool/znb/cdn-refresh
67          settings:
68            ak: "${TENCENT_OPSRE_AK}"
69            sk: "${TENCENT_OPSRE_SK}"
70            kind: "tencenteo"
71            rtype: "path"
72            domain: "onedayxyy.cn"
73            urls:
74              - "https://onedayxyy.cn/"
75
76        #同步仓库到gitee                  
77        - name: sync to gitee
78          image: tencentcom/git-sync
79          settings:
80            branch: master
81            auth_type: https
82            username: ${GIT_USERNAME}
83            password: ${GIT_ACCESS_TOKEN}
84            target_url: https://gitee.com/onlyonexl/vitepress-theme-teek-one-private.git
85            # git_email: 'github-actions[bot]@users.noreply.github.com'

扩展5:使用企业微信机器人

见单独md。

2025年7月23日实现

原作者代码

2025.4.3-下载-JenkinsGuide

image-20250723053906471

自己最新代码

https://cnb.cool/onedayxyy/vitepress-theme-teek-one-private (私有)

image-20250723054016396

2025.7.23-最新代码

添加企业微信机器人推送构建消息代码:

.cnb.yml

 1master:
 2  push:
 3    - runner:
 4        cpus: 16
 5      services:
 6        - docker
 7        - git-clone-yyds
 8      imports:
 9        - https://cnb.cool/onedayxyy/secret/-/blob/main/envs.yml     
10      docker:
11        image: docker.cnb.cool/znb/images/node:18
12        volumes:
13          - /data/.cache:copy-on-write #声明式的构建缓存
14      stages:
15        - name: set env
16          script: echo -n $(date "+%Y-%m-%d %H:%M")
17          exports:
18            info: CUSTOM_ENV_DATE_INFO      
19        - name: 🖨️ 打印环境
20          if: |
21            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]
22          script: |
23            node -v && npm -v && yarn -v && pnpm -v
24        - name: 📦️ 安装依赖 
25          if: |
26            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]          
27          script: |
28            pnpm install
29            
30        - name: ⚗️ 编译项目
31          if: |
32            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]        
33          script: |
34            pnpm docs:build   # VitePress 专用命令
35        - name: 🚚 发布制品
36          if: |
37            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]            
38          image: tencentcom/rsync
39          settings:
40            user: ${SSH_USER}
41            key: ${SSH_KEY}
42            port: 22
43            hosts:
44              - ${ECS_IP}
45            source: docs/.vitepress/dist/
46            target: /root/rsync/rsync-vitepress/dist/
47            delete: true
48            prescript:
49              - echo "prescript"
50              - ls -l  /root/rsync/rsync-vitepress/dist/
51            # script:
52            #   - echo "after script"
53            #   - ls -l /root/rsync/rsync-vitepress/dist/
54            #   - date
55            #   # - bash  /root/website_music.sh
56            #   - aliyun cdn RefreshObjectCaches --ObjectType File --ObjectPath "onedayxyy.cn/" #刷新阿里云cdn缓存
57
58
59
60        - name: 🧘‍♂️ 刷新缓存
61          image: docker.cnb.cool/znb/cdn-refresh
62          settings:
63            ak: "${TENCENT_OPSRE_AK}"
64            sk: "${TENCENT_OPSRE_SK}"
65            kind: "tencenteo"
66            rtype: "path"
67            domain: "onedayxyy.cn"
68            urls:
69              - "https://onedayxyy.cn/"
70
71        #同步仓库到gitee                  
72        - name: sync to gitee
73          image: tencentcom/git-sync
74          settings:
75            branch: master
76            auth_type: https
77            username: ${GIT_USERNAME}
78            password: ${GIT_ACCESS_TOKEN}
79            target_url: https://gitee.com/onlyonexl/vitepress-theme-teek-one-private.git
80            # git_email: 'github-actions[bot]@users.noreply.github.com'
81
82        - name: 🔔 发布通知
83          image: tencentcom/wecom-message
84          settings:
85            robot: ${WECOM_BOT}
86            msgType: markdown
87            content: |
88              > **🎉 One Blog 又一次发布成功啦!**   
89              > **构建时间:** $CUSTOM_ENV_DATE_INFO          
90              > **构建id:** $CNB_BUILD_ID  
91              > **提交id:** $CNB_COMMIT_SHORT            
92              > **构建分支:** $CNB_BRANCH
93              > **提交信息:** $CNB_COMMIT_MESSAGE_TITLE
94              > **提交者:** $CNB_COMMITTER
95              > **仓库地址:** [$CNB_REPO_URL_HTTPS]($CNB_REPO_URL_HTTPS)
  • 效果

关于我

我的博客主旨:

  • 排版美观,语言精炼;
  • 文档即手册,步骤明细,拒绝埋坑,提供源码;
  • 本人实战文档都是亲测成功的,各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人帮您解决问题,让我们一起进步!

🍀 个人网站

image-20250109220325748

🍀 微信二维码

x2675263825 (舍得), qq:2675263825。

image-20230107215114763

🍀 微信公众号

《云原生架构师实战》

image-20230107215126971

🍀 csdn

https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421

image-20230107215149885

🍀 知乎

https://www.zhihu.com/people/foryouone

image-20230107215203185

最后

如果你还有疑惑,可以去我的网站查看更多内容或者联系我帮忙查看。

如果你有更好的方式,评论区留言告诉我。谢谢!

好了,本次就到这里了,感谢大家阅读,最后祝大家生活快乐,每天都过的有意义哦,我们下期见!

image-20250401215005230

推荐使用微信支付
微信支付二维码
推荐使用支付宝
支付宝二维码
最新文章

文档导航