首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >cnb 云原生构建实践

cnb 云原生构建实践

原创
作者头像
TomoriNao
发布2025-07-19 21:12:36
发布2025-07-19 21:12:36
7230
举报
文章被收录于专栏:每月技术成长每月技术成长

CNB(Cloud Native Build)是由腾讯推出的 DevOps 平台,具有云原生构建、云原生开发等功能。云原生开发,相信绝大部分开发者都接触过,不再赘述。云原生构建则有 Github Action、GitLab CI/CD 等产品为代表,实现云端自动化构建。CNB 具备云原生构建功能,但相对而言,缺少相应实践文档,一番折腾后,终于成功,小有心得,谨以此文抛砖引玉。

.cnb.yml

CNB 的云原生开发配置及云原生构建配置均基于项目根目录下的 .cnb.yml ,可以通过修改此文件实现自定义云原生开发环境、为指定事件配置流水线等功能。

代码语言:yml
复制
# .cnb.yml
main:
  push:
    - docker:
    # 自定义云原生开发环境
        image: node:20
      stages:
    #  配置流水线
        - node -v
        - npm install
        - npm test

tag_push

CNB 可以通过 tag_push 事件自动化构建,也可通过 web_trigger、tag_deloy.* 手动触发构建,本文仅介绍 tag_push 事件。

tag_push 事件配置

tag_push 事件同样需要在 .cnb.yml 文件中设置,可以针对特定 tag分别配置流水线。

代码语言:yml
复制
# 对指定 tag 生效
v1.0.*:
  tag_push:
    - stages:
        - name: echo tag name
          script: echo  $CNB_BRANCH

# 对所有 tag 生效
$:
  tag_push:
    - stages:
        - name: echo tag name
          script: echo  $CNB_BRANCH

上传 Release 附件

tag_push 中可以通过内置任务 git:release 及插件 cnbcool/attachments:latest 上传release 附件。

git:release 与 cnbcool/attachments:latest 的区别在于 git:release 能够为对应 tag 创建对应 Release,但仅能将源码打包为 .zip/.tar 文件上传至 Release 中,而 cnbcool/attachments:latest 则支持将 任意 文件上传至 Release 中,前提是 tag 已有对应 Release。

因此,在 tag_push 的流水线中,建议先运行 git:release 创建对应 Release 并上传源码,再通过 cnbcool/attachments:latest 上传构建结果。

注意事项

流水线基于 docker exec -i命令,不会直接读取 .bashrc 配置,因此在配置流水线时,对于修改了 .bashrc(如:下载nvm) 后的任务,建议均进行 source .bashrc 操作,避免构建失败。以下为 git:release、cnbcool/attachments:latest、一个前端项目 tag_push 示例。

代码语言:yml
复制
$:
  # git:release 事件示例
  push:
    - stages:
        - name: git release
          type: git:release
          options:
            tag: Nightly
            description: description
代码语言:yml
复制
$:
  # cnbcool/attachments:latest 示例
  tag_push:
    - stages:
      - name: release 上传附件
        image: cnbcool/attachments:latest
        settings:
          attachments:
            - "./*.txt"
        exports:
          FILES: FILES
      - name: 输出 files
        script: echo $FILES
代码语言:yml
复制
# 一个前端项目 tag_push 示例
$:
  vscode:
    - runner:
        cpus: 2
      services:
        - vscode
        - docker
      stages:
        - name: hello world
          script: echo "Hello World"
  tag_push:
    - runner:
        cpus: 2
    - stages:
        # 运行 git:release ,创建对应 Release 并上传源码
        - name: upload release
          type: git:release
          options:
            title: release
            description: release test
        # 构建相关任务
        - name: init
          script: apt update
        - name: install packages
          script: apt install -y libvips-dev zip
        - name: install nvm
          script: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
        # 需要运行 source ~/.bashrc 来加载 nvm
        - name: install node
          script: source ~/.nvm/nvm.sh && nvm install node
        - name: install yarn
        # 需要运行 source ~/.bashrc 来加载 npm
          script: source ~/.bashrc && npm install -g yarn
        - name: install dependencies
          script: source ~/.bashrc && yarn install
        - name: build dist
        # 需要运行 source ~/.bashrc 来加载 npm
          script: source ~/.bashrc && yarn build 
        - name: zip dist
          script: zip -r dist.zip dist
        # 上传 dist.zip 到 Release 附件
        - name: upload dist
          image: cnbcool/attachments:latest
          settings:
            attachments:
              - "./dist.zip"

参考文献:

  1. CNB Docs-流水线语法
  2. CNB Docs-tag_push
  3. CNB Docs-git:release
  4. CNB Docs-cnbcool/attachments

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • .cnb.yml
  • tag_push
    • tag_push 事件配置
    • 上传 Release 附件
    • 注意事项
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档