Skip to content
0

cnb配置机器人2种方式

背景

image-20250813201338990

image-20250813201442872

https://cnb.cool/w3c/vitepress-theme-teek

yaml
main:
  push:
    - runner:
        cpus: 64
      docker:
        image: node:22
      stages:
          # 将依赖存起来,下次构建就不需要安装依赖了,每次构建都重复安装依赖就很烦,也很慢.缓存一次供后续循环使用,直到package.json有改动才会在次生成缓存
        - name: 构建缓存镜像
          type: docker:cache
          options:
            dockerfile: cache.dockerfile
            # 将以下文件的依赖推送到制品保存起来,循环复用,只有首次和package.json有改动导致依赖有变化才会触发下一次推送制品
            by:
              - package.json
              - pnpm-lock.yaml
            versionBy:
              - pnpm-lock.yaml
          exports:
            name: DOCKER_CACHE_IMAGE_NAME
        - name: 使用缓存的node_modules,免安装依赖
          image: $DOCKER_CACHE_IMAGE_NAME
          commands:
            - cp -r "$NODE_PATH" ./node_modules
        - name: 构建当前项目
          # 构建时不需要安装依赖的命令了,因为依赖已经在制品里安装过了
          script: npm install -g pnpm && npm run docs:build
        # 以下这部分是zip打包,内容多可以用,内容少就没用,因为推送也很快
        # - name: 使用zip工具
        #   script: apt-get update && apt-get install -y zip
        # - name: 打包压缩dist内文件
        #   script: |
        #     cd /workspace/.vitepress/dist/
        #     zip -r dist.zip ./*
        - name: 部署到Pages
          imports: https://cnb.cool/w3c/envs/-/blob/main/pages.yml
          script: npx edgeone pages deploy ./.vitepress/dist/ --name vitepress-theme-teek --token $EDGEONE_PAGES_API_TOKEN

  web_trigger:
    - runner:
        cpus: 64
      docker:
        image: node:22
      stages:
          # 将依赖存起来,下次构建就不需要安装依赖了,每次构建都重复安装依赖就很烦,也很慢.缓存一次供后续循环使用,直到package.json有改动才会在次生成缓存
        - name: 构建缓存镜像
          type: docker:cache
          options:
            dockerfile: cache.dockerfile
            # 将以下文件的依赖推送到制品保存起来,循环复用,只有首次和package.json有改动导致依赖有变化才会触发下一次推送制品
            by:
              - package.json
              - pnpm-lock.yaml
            versionBy:
              - pnpm-lock.yaml
          exports:
            name: DOCKER_CACHE_IMAGE_NAME
        - name: 使用缓存的node_modules,免安装依赖
          image: $DOCKER_CACHE_IMAGE_NAME
          commands:
            - cp -r "$NODE_PATH" ./node_modules
        - name: 构建当前项目
          # 构建时不需要安装依赖的命令了,因为依赖已经在制品里安装过了
          script: npm install -g pnpm && npm run docs:build
        # 以下这部分是zip打包,内容多可以用,内容少就没用,因为推送也很快
        # - name: 使用zip工具
        #   script: apt-get update && apt-get install -y zip
        # - name: 打包压缩dist内文件
        #   script: |
        #     cd /workspace/.vitepress/dist/
        #     zip -r dist.zip ./*
        - name: 部署到Pages
          imports: https://cnb.cool/w3c/envs/-/blob/main/pages.yml
          script: npx edgeone pages deploy ./.vitepress/dist/ --name vitepress-theme-teek --token $EDGEONE_PAGES_API_TOKEN

自己之前.cnb.yml内容(修改前)

yaml
master:
  push:
    - runner:
        cpus: 64
      services:
        - docker
      imports:
        - https://cnb.cool/onedayxyy/secret/-/blob/main/envs.yml     
      docker:
        image: docker.cnb.cool/znb/images/node:18
      stages:
        - name: set env
          script: echo -n $(date "+%Y-%m-%d %H:%M")
          exports:
            info: CUSTOM_ENV_DATE_INFO      
        - name: 🖨️ 打印环境
          if: |
            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]
          script: |
            node -v && npm -v && yarn -v && pnpm -v
        # - name: 🗄️ 声明缓存
        #   cache:
        #     key: ${CNB_BUILD_ID}
        #     paths:
        #       - docs/.vitepress/dist  # 缓存构建产物
        #       - node_modules          # 可选,加速后续构建            
        - name: 📦️ 安装依赖 
          if: |
            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]          
          script: |
            pnpm install
            
        - name: ⚗️ 编译项目
          if: |
            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]        
          script: |
            pnpm docs:build   # VitePress 专用命令
            ls -la docs/.vitepress/dist  # 确认构建文件存在
            # cd docs/.vitepress/dist
            # git init
            # git remote add origin https://cnb.cool/onedayxyy/vitepress-theme-teek-one-public.git
            # git add -A
            # git commit -m "Deploying to CNB"
            # git push -u origin master:dist -f     


        # - name: 🚀 强制同步到公开仓库
        #   image: alpine/git
        #   cache:
        #     key: ${CNB_BUILD_ID}
        #     paths:
        #       - docs/.vitepress/dist          
        #   script: |
        #     # 配置 Git
        #     git config --global user.name "CI-Bot"
        #     git config --global user.email "ci@onedayxyy.cn"

        #     # 清理旧目录(防止残留)
        #     rm -rf /tmp/public-repo

        #     # 尝试克隆 dist 分支,失败则初始化新分支
        #     if ! git clone -b dist --single-branch \
        #       https://${GIT_USERNAME}:${GIT_ACCESS_TOKEN}@cnb.cool/onedayxyy/vitepress-theme-teek-one-public.git \
        #       /tmp/public-repo 2>/dev/null; then
              
        #       echo "➜ dist 分支不存在,初始化新仓库..."
        #       git clone https://${GIT_USERNAME}:${GIT_ACCESS_TOKEN}@cnb.cool/onedayxyy/vitepress-theme-teek-one-public.git /tmp/public-repo
        #       cd /tmp/public-repo
        #       git checkout --orphan dist
        #       git commit --allow-empty -m "Initial empty dist branch"  # 必须有一个提交
        #     fi

        #     # 进入目录并清空内容(保留 .git)
        #     cd /tmp/public-repo
        #     echo "➜ 当前目录内容:"
        #     ls -la
        #     find . -mindepth 1 -maxdepth 1 ! -name '.git' -exec rm -rf {} +

        #     # 验证源目录存在
        #     echo "➜ 源目录内容:"
        #     ls -la ${CNB_BUILD_ID}
        #     cp -r ${CNB_BUILD_ID} .

        #     # 提交并强制推送  
        #     git add -A
        #     git status  # 调试查看
        #     git commit -m "Update: Auto sync from ${CNB_COMMIT_SHORT}"
        #     git push origin dist --force
        #     echo "✅ 同步完成"            
        # - name: 🚀 同步到公开仓库
        #   if: |
        #     [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]
        #   image: tencentcom/git-sync
        #   settings:
        #     branch: dist  # 指定目标分支
        #     create_missing_branch: true  # 自动创建不存在的分支
        #     auth_type: https
        #     username: ${GIT_cnb_USERNAME}  # 从环境变量读取
        #     password: ${GIT_cnb_ACCESS_TOKEN}  # 需有目标仓库写入权限
        #     target_url: https://cnb.cool/onedayxyy/vitepress-theme-teek-one-public.git
        #     source_dir: ./docs/.vitepress/dist  # 要推送的目录
        #     force: true  # 强制覆盖目标分支
        #     git_config: |
        #       user.name "CI-Bot"
        #       user.email "ci@onedayxyy.cn"

        - name: 🚚 部署到Pages
          if: |
            [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]            
          image: node:22
          script: npx edgeone pages deploy ./docs/.vitepress/dist --name vitepress-theme-teek-one-private --token $EDGEONE_PAGES_API_TOKEN
          
        # - name: 🚚 发布制品 (把Teek静态文件从云服务器)
        #   if: |
        #     [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]            
        #   image: tencentcom/rsync
        #   settings:
        #     user: ${SSH_USER}
        #     key: ${SSH_KEY}
        #     port: 22
        #     hosts:
        #       - ${ECS_IP}
        #     source: docs/.vitepress/dist/
        #     target: /root/rsync/rsync-vitepress/dist/
        #     delete: true
        #     prescript:
        #       - echo "prescript"
        #       - ls -l  /root/rsync/rsync-vitepress/dist/
            # script:
            #   - echo "after script"
            #   - ls -l /root/rsync/rsync-vitepress/dist/
            #   - date
            #   # - bash  /root/website_music.sh
            #   - aliyun cdn RefreshObjectCaches --ObjectType File --ObjectPath "onedayxyy.cn/" #刷新阿里云cdn缓存



        # - name: 🧘‍♂️ 刷新缓存
        #   image: docker.cnb.cool/znb/cdn-refresh
        #   settings:
        #     ak: "${TENCENT_OPSRE_AK}"
        #     sk: "${TENCENT_OPSRE_SK}"
        #     kind: "tencenteo"
        #     rtype: "path"
        #     domain: "onedayxyy.cn"
        #     urls:
        #       - "https://onedayxyy.cn/"

        #同步仓库到gitee                  
        - name: sync to gitee
          image: tencentcom/git-sync
          settings:
            branch: master
            auth_type: https
            username: ${GIT_USERNAME}
            password: ${GIT_ACCESS_TOKEN}
            target_url: https://gitee.com/onlyonexl/vitepress-theme-teek-one-private.git
            # git_email: 'github-actions[bot]@users.noreply.github.com'

        - name: 🔔 发布通知  
          image: tencentcom/wecom-message
          settings:
            robot: ${WECOM_BOT}
            msgType: markdown
            content: |
              > **🎉 One Blog 又一次发布成功啦!**   
              > **构建时间:** $CUSTOM_ENV_DATE_INFO          
              > **构建id:** $CNB_BUILD_ID  
              > **提交id:** $CNB_COMMIT_SHORT            
              > **构建分支:** $CNB_BRANCH
              > **提交信息:** $CNB_COMMIT_MESSAGE_TITLE
              > **提交者:** $CNB_COMMITTER
              > **仓库地址:** [$CNB_REPO_URL_HTTPS]($CNB_REPO_URL_HTTPS)
              > **我的网站:** [onedayxyy.cn](https://onedayxyy.cn/)

        - name: build knowledge base
          image: cnbcool/knowledge-base
          settings:
            embedding_model: bge-m3
            include: "**/**.md"
            exclude: ""

群里钉钉内容(可以看到构建耗时)

.cnb.yml内容

yaml
main:
  push:
    "volume-cache":
      name: 云原生构建
      runner:
        cpus: 64
      imports:	
        - https://cnb.cool/5566/secret/-/blob/main/envs.yml
      docker:
        image: node:22
      stages:
        - name: set env
          script: echo -n $(date +%s)
          exports:
            info: CUSTOM_ENV_START_TIME
        - name: 🏠️ 设置淘宝源
          script: |
            npm config set registry https://registry.npmmirror.com
        - name: 🖨️ 打印环境
          script: |
            npm install -g pnpm && node -v && npm -v && yarn -v && pnpm -v
        - name: 📦️ 安装依赖
          script: |
            pnpm install
        - name: ⚗️ 编译项目
          script: |
            pnpm docs:build
        - name: 🚚 部署到Pages
          image: node:22
          script: >-
            npx edgeone pages deploy ./docs/.vitepress/dist --name
            ceshi --token $EDGEONE_PAGES_API_TOKEN
        - name: ⏱️ 计算耗时
          script: |
            # 计算耗时
            end_time=$(date +%s)
            duration=$((end_time - $CUSTOM_ENV_START_TIME))
            minutes=$((duration / 60))
            seconds=$((duration % 60))
            time_str="${minutes}分${seconds}秒"
            echo -n ${time_str}
          exports:
            info: CUSTOM_ENV_BUILD_TIME
        - name: 钉钉通知
          image: tencentcom/dingtalk-bot-msg:latest
          settings:
            content: "「KylinOS/UOS技术文档」发布完成,耗时: ${CUSTOM_ENV_BUILD_TIME}"
            c_type: "text" # 支持: text, markdown, link, actionCard, multiActionCard, feedCard
            secret: $SECRET
            webhook: $WEBHOOK
            isAtAll: false
            debug: false # 新增: 是否启用调试模式

自己最新内容(修改后)-v1-传统版(测试成功)

yaml
master:
  push:
    "volume-cache":
      name: 云原生构建
      runner:
        cpus: 64
      imports:	
        - https://cnb.cool/onedayxyy/secret/-/blob/main/envs.yml
      docker:
        image: node:22
      stages:
        - name: set env
          script: echo -n $(date +%s)
          exports:
            info: CUSTOM_ENV_START_TIME
        - name: 🏠️ 设置淘宝源
          script: |
            npm config set registry https://registry.npmmirror.com
        - name: 🖨️ 打印环境
          script: |
            npm install -g pnpm && node -v && npm -v && yarn -v && pnpm -v
        - name: 📦️ 安装依赖
          script: |
            pnpm install
        - name: ⚗️ 编译项目
          script: |
            pnpm docs:build
        - name: 🚚 部署到Pages
          image: node:22
          script: >-
            npx edgeone pages deploy ./docs/.vitepress/dist --name
            vitepress-theme-teek-one-private --token $EDGEONE_PAGES_API_TOKEN


        #同步仓库到gitee                  
        - name: sync to gitee
          image: tencentcom/git-sync
          settings:
            branch: master
            auth_type: https
            username: ${GIT_USERNAME}
            password: ${GIT_ACCESS_TOKEN}
            target_url: https://gitee.com/onlyonexl/vitepress-theme-teek-one-private.git
            # git_email: 'github-actions[bot]@users.noreply.github.com'

        # - name: build knowledge base
        #   image: cnbcool/knowledge-base
        #   settings:
        #     embedding_model: bge-m3
        #     include: "**/**.md"
        #     exclude: ""    

        - name: ⏱️ 计算耗时
          script: |
            # 计算耗时
            end_time=$(date +%s)
            duration=$((end_time - $CUSTOM_ENV_START_TIME))
            minutes=$((duration / 60))
            seconds=$((duration % 60))
            time_str="${minutes}分${seconds}秒"
            echo -n ${time_str}
          exports:
            info: CUSTOM_ENV_BUILD_TIME

            
        - name: 钉钉通知
          image: tencentcom/dingtalk-bot-msg:latest
          settings:
            content: "「One Blog」发布完成,耗时: ${CUSTOM_ENV_BUILD_TIME}"
            c_type: "text" # 支持: text, markdown, link, actionCard, multiActionCard, feedCard
            secret: $SECRET
            webhook: $WEBHOOK
            isAtAll: false
            debug: false # 新增: 是否启用调试模式

        # - name: 🔔 发布通知  
        #   image: tencentcom/wecom-message
        #   settings:
        #     robot: ${WECOM_BOT}
        #     msgType: markdown
        #     content: |
        #       > **🎉 One Blog 又一次发布成功啦!**   
        #       > **构建时间:** $CUSTOM_ENV_DATE_INFO 
        #       > **构建耗时:** ${CUSTOM_ENV_BUILD_TIME}        
        #       > **构建id:** $CNB_BUILD_ID  
        #       > **提交id:** $CNB_COMMIT_SHORT            
        #       > **构建分支:** $CNB_BRANCH
        #       > **提交信息:** $CNB_COMMIT_MESSAGE_TITLE
        #       > **提交者:** $CNB_COMMITTER
        #       > **仓库地址:** [$CNB_REPO_URL_HTTPS]($CNB_REPO_URL_HTTPS)
        #       > **我的网站:** [onedayxyy.cn](https://onedayxyy.cn/)

自己最新内容(修改后)-v2-制品版(测试成功)

yaml
master:
  push:
    - runner:
        cpus: 64
      imports:	
        - https://cnb.cool/onedayxyy/secret/-/blob/main/envs.yml        
      docker:
        image: node:22
      stages:
        - name: 😉 set env
          script: echo -n $(date +%s)
          exports:
            info: CUSTOM_ENV_START_TIME     
        # - name: 🖨️ 打印环境
        #   if: |
        #     [ "$CNB_COMMIT_MESSAGE_TITLE" = "BUILD" ]
        #   script: |
        #     node -v && npm -v && yarn -v && pnpm -v 

          # 将依赖存起来,下次构建就不需要安装依赖了,每次构建都重复安装依赖就很烦,也很慢.缓存一次供后续循环使用,直到package.json有改动才会在次生成缓存
        - name: 📢 构建缓存镜像
          type: docker:cache
          options:
            dockerfile: cache.dockerfile
            # 将以下文件的依赖推送到制品保存起来,循环复用,只有首次和package.json有改动导致依赖有变化才会触发下一次推送制品
            by:
              - package.json
              - pnpm-lock.yaml
            versionBy:
              - pnpm-lock.yaml
          exports:
            name: DOCKER_CACHE_IMAGE_NAME
        - name: 📦 使用缓存的node_modules,免安装依赖
          image: $DOCKER_CACHE_IMAGE_NAME
          commands:
            - cp -r "$NODE_PATH" ./node_modules
        - name: 🔨 构建当前项目
          # 构建时不需要安装依赖的命令了,因为依赖已经在制品里安装过了
          script: npm install -g pnpm && npm run docs:build
        # 以下这部分是zip打包,内容多可以用,内容少就没用,因为推送也很快
        # - name: 使用zip工具
        #   script: apt-get update && apt-get install -y zip
        # - name: 打包压缩dist内文件
        #   script: |
        #     cd /workspace/.vitepress/dist/
        #     zip -r dist.zip ./*
        - name: 🎉部署到Pages
          script: npx edgeone pages deploy ./docs/.vitepress/dist --name vitepress-theme-teek-one-private --token $EDGEONE_PAGES_API_TOKEN



        #同步仓库到gitee                  
        - name: 📌 sync to gitee
          image: tencentcom/git-sync
          settings:
            branch: master
            auth_type: https
            username: ${GIT_USERNAME}
            password: ${GIT_ACCESS_TOKEN}
            target_url: https://gitee.com/onlyonexl/vitepress-theme-teek-one-private.git
            # git_email: 'github-actions[bot]@users.noreply.github.com'

        # - name: 👀 build knowledge base
        #   image: cnbcool/knowledge-base
        #   settings:
        #     embedding_model: bge-m3
        #     include: "**/**.md"
        #     exclude: ""    

        - name: ⏱️ 计算耗时
          script: |
            # 计算耗时
            end_time=$(date +%s)
            duration=$((end_time - $CUSTOM_ENV_START_TIME))
            minutes=$((duration / 60))
            seconds=$((duration % 60))
            time_str="${minutes}分${seconds}秒"
            echo -n ${time_str}
          exports:
            info: CUSTOM_ENV_BUILD_TIME

        - name: 钉钉通知
          image: tencentcom/dingtalk-bot-msg:latest
          settings:
            content: "「One Blog」发布完成,耗时: ${CUSTOM_ENV_BUILD_TIME}"
            c_type: "text" # 支持: text, markdown, link, actionCard, multiActionCard, feedCard
            secret: $SECRET
            webhook: $WEBHOOK
            isAtAll: false
            debug: false # 新增: 是否启用调试模式

        # - name: 🔔 发布通知  
        #   image: tencentcom/wecom-message
        #   settings:
        #     robot: ${WECOM_BOT}
        #     msgType: markdown
        #     content: |
        #       > **🎉 One Blog 又一次发布成功啦!**   
        #       > **构建时间:** $CUSTOM_ENV_DATE_INFO      
        #       > **构建id:** $CNB_BUILD_ID  
        #       > **提交id:** $CNB_COMMIT_SHORT            
        #       > **构建分支:** $CNB_BRANCH
        #       > **提交信息:** $CNB_COMMIT_MESSAGE_TITLE
        #       > **提交者:** $CNB_COMMITTER
        #       > **仓库地址:** [$CNB_REPO_URL_HTTPS]($CNB_REPO_URL_HTTPS)
        #       > **我的网站:** [onedayxyy.cn](https://onedayxyy.cn/)

效果:耗时5min46s

image-20250814103153993

image-20250814103201604

部署到pages还是耗时

dist目录压缩包超过25M,就没法通过cnb传输到eo pages里去。

image-20250813204721270


image-20250813204843118

image-20250813204857471

警告

要想解决这个问题,只能等群主把teek的404问题解决才行。

最近更新