CNB(Cloud Native Build)是由腾讯推出的 DevOps 平台,具有云原生构建、云原生开发等功能。云原生开发,相信绝大部分开发者都接触过,不再赘述。云原生构建则有 Github Action、GitLab CI/CD 等产品为代表,实现云端自动化构建。CNB 具备云原生构建功能,但相对而言,缺少相应实践文档,一番折腾后,终于成功,小有心得,谨以此文抛砖引玉。
CNB 的云原生开发配置及云原生构建配置均基于项目根目录下的 .cnb.yml ,可以通过修改此文件实现自定义云原生开发环境、为指定事件配置流水线等功能。
# .cnb.yml
main:
push:
- docker:
# 自定义云原生开发环境
image: node:20
stages:
# 配置流水线
- node -v
- npm install
- npm testCNB 可以通过 tag_push 事件自动化构建,也可通过 web_trigger、tag_deloy.* 手动触发构建,本文仅介绍 tag_push 事件。
tag_push 事件同样需要在 .cnb.yml 文件中设置,可以针对特定 tag分别配置流水线。
# 对指定 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_BRANCHtag_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 示例。
$:
# git:release 事件示例
push:
- stages:
- name: git release
type: git:release
options:
tag: Nightly
description: description$:
# cnbcool/attachments:latest 示例
tag_push:
- stages:
- name: release 上传附件
image: cnbcool/attachments:latest
settings:
attachments:
- "./*.txt"
exports:
FILES: FILES
- name: 输出 files
script: echo $FILES# 一个前端项目 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"参考文献:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。