### JSON Reviewers Configuration Example Source: https://docs.cnb.cool/zh/build/internal-steps.md Example of a JSON configuration file for specifying reviewers based on file paths. Keys are file paths and values are comma-separated reviewer names. ```json { "./src": "name1,name2", ".cnb.yml": "name3" } ``` -------------------------------- ### CODEOWNERS File Format Example Source: https://docs.cnb.cool/zh/build/internal-steps.md Example of a CODEOWNERS file format for specifying reviewers based on file paths. Supports gitignore-style pattern matching and comments. ```text # 通用规则写前,具体规则写后 * @global-team *.js @fe-team /src/frontend/ @frontend-owner /src/frontend/auth.js @security-team ``` -------------------------------- ### Tag Push Event Example Source: https://docs.cnb.cool/zh/build/trigger-rule.md Example configurations for the `tag_push` event, which triggers when a tag is pushed. It shows how to apply the rule to specific tag patterns (e.g., `v1.0.*`) or all tags. ```yaml # 对指定 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 ``` -------------------------------- ### 基本云原生构建配置示例 Source: https://docs.cnb.cool/zh/build/configuration.md 定义当 `main` 分支收到 `push` 事件时,使用 `node:22` Docker 镜像执行 `npm install` 和 `npm test` 任务。 ```yaml main: # 目标分支 push: # 触发事件 - docker: image: node:22 # 流水线执行环境,可使用任意 Docker 镜像 stages: - name: install script: npm install - name: test script: npm test ``` -------------------------------- ### Lock Key Variable Substitution Example Source: https://docs.cnb.cool/zh/build/env.md Shows how to use a variable defined in an imported YAML file for the lock key in a .cnb.yml configuration. This example includes a scenario where multiple pipelines attempt to acquire the same lock. ```yaml # env.yml build_key: build key ``` ```yaml .build: &build imports: env.yml lock: key: $build_key stages: - name: echo script: echo "hello world" main: push: # 以下两条流水线,一条占用了锁成功执行,另一条位占到锁执行失败 - *build - *build ``` -------------------------------- ### Auto Tag Configuration Example Source: https://docs.cnb.cool/zh/build/trigger-rule.md Custom configuration for the `auto_tag` event, which triggers when tags are automatically generated. This example specifies a custom tag format and uses the `cnbcool/git-auto-tag` plugin. ```yaml main: # 默认分支,可使用仓库实际默认分支代替 auto_tag: - stages: - name: release rules # 该环境变量在触发构建时传入,可查看打印内容看 release 规则 script: echo -e "$RELEASE_RULES" - name: auto tag image: cnbcool/git-auto-tag:latest settings: tagFormat: 'v\${version}' branch: $CNB_BRANCH repoUrlHttps: $CNB_REPO_URL_HTTPS releaseRules: $RELEASE_RULES exports: tag: NEW_TAG - name: show tag script: echo $NEW_TAG ``` -------------------------------- ### Configure Custom Trigger Events for Environment Creation Source: https://docs.cnb.cool/zh/workspaces/custom-dev-pipeline.md Automate the creation of a development environment based on specific trigger events like branch creation or custom API triggers. This example configures environment creation upon branch creation. ```yaml # 匹配所有分支 (**): # 创建分支时创建开发环境 branch.create: - name: vscode services: # 声明使用 vscode 服务 - vscode docker: # 自定义开发环境 build: .ide/Dockerfile stages: - name: 执行自定义脚本 script: - npm install - npm run start ``` -------------------------------- ### Squash Merge with Custom Message and Footer Source: https://docs.cnb.cool/zh/build/internal-steps.md This example demonstrates configuring squash merge with a custom commit message and a specific footer. The output shows how the message, footer, and automatically appended PR details are combined. ```yaml main: pull_request.mergeable: - stages: - name: automerge type: git:auto-merge options: mergeType: squash mergeCommitMessage: "add feature for some jobs" mergeCommitFooter: "--story=123 --story=456" ``` -------------------------------- ### Squash Merge Commit Message Example Source: https://docs.cnb.cool/zh/build/internal-steps.md When using squash merge, the default commit message is the PR's first commit message, with appended PR and reviewer information. This example shows the resulting commit message structure. ```text feat(model-a): give module A a new feature Due to some reason, add new features close #10 PR-URL: !3976 Reviewed-By: tom Reviewed-By: jerry Co-authored-by: jack ``` -------------------------------- ### Branch Matching Rules in .cnb.yml Source: https://docs.cnb.cool/zh/build/trigger-rule.md This snippet demonstrates various glob pattern matching rules for branch names in a .cnb.yml file. It includes examples for matching specific prefixes, multiple branches, excluding branches, and a catch-all rule. ```yaml .push_pipeline: &push_pipeline stages: - name: do something script: echo "do something" # 匹配以 dev/ 开头的所有分支 "dev/*": push: - *push_pipeline # 匹配 main 或 dev 分支 "(main|dev)": push: - *push_pipeline # 匹配除 main 和 dev 以外的所有分支 "**/!(main|dev)": push: - *push_pipeline # 匹配所有分支 "**": push: - *push_pipeline # 兜底,匹配没有 glob 匹配的分支 "$": push: - *push_pipeline tag_push: - *push_pipeline ``` -------------------------------- ### Branch Delete Event Example Source: https://docs.cnb.cool/zh/build/trigger-rule.md Example configuration for the `branch.delete` event, which triggers when a remote code branch is deleted. The `CNB_BRANCH` environment variable will contain the name of the deleted branch. ```yaml dev/1: branch.delete: - stages: - name: echo # CNB_BRANCH 值为删除的分支 script: echo $CNB_BRANCH $: branch.delete: - stages: - name: echo # CNB_BRANCH 值为删除的分支 script: echo $CNB_BRANCH ``` -------------------------------- ### Conditional Reviewer/Assignee Addition Based on Time Source: https://docs.cnb.cool/zh/build/internal-steps.md Add reviewers or assignees based on a condition, such as the time of day. This example adds specific reviewers outside of business hours. ```yaml main: pull_request: - stages: - name: 下班时间发版本?需指定人评审。 if: | [ $(date +%H) -ge 18 ] type: git:reviewer options: type: add-reviewer reviewers: bbb - name: 下班时间发版本?需指定人处理。 if: | [ $(date +%H) -ge 18 ] type: git:reviewer options: type: add-assignee reviewers: ccc ``` -------------------------------- ### Random Reviewer Selection and Assignment Source: https://docs.cnb.cool/zh/build/internal-steps.md Randomly selects a reviewer from a list and assigns them to the pull request. This example uses a separate image to pick a random reviewer and then assigns them. ```yaml main: pull_request: - stages: - name: random image: tencentcom/random settings: from: - aaa - bbb exports: result: CURR_REVIEWER - name: show CURR_REVIEWER script: echo ${CURR_REVIEWER} - name: add reviewer type: git:reviewer options: type: add-reviewer reviewers: ${CURR_REVIEWER} ``` -------------------------------- ### Auto-Merge and Notification on Review Approval Source: https://docs.cnb.cool/zh/build/internal-steps.md Configures automatic merging of a pull request after review approval and sends a notification to a specified channel. This example demonstrates squashing merges and notifying via a WeCom robot. ```yaml main: review: - stages: - name: CR 通过后自动合并 type: git:auto-merge options: mergeType: squash removeSourceBranch: true mergeCommitMessage: $CNB_LATEST_COMMIT_MESSAGE exports: reviewedBy: REVIEWED_BY # 将评审消息发送到企业微信机器人 - name: notify image: tencentcom/wecom-message settings: msgType: markdown robot: "155af237-6041-4125-9340-000000000000" content: | > CR 通过后自动合并 <@${CNB_BUILD_USER}> > > ${CNB_PULL_REQUEST_TITLE} > [${CNB_EVENT_URL}](${CNB_EVENT_URL}) > > ${REVIEWED_BY} ``` ```yaml pull_request: - stages: # ...省略其他任务 # 发送到 CR 专用群 - name: add reviewer type: git:reviewer options: reviewers: aaa,bbb,ccc,ddd count: 2 exports: reviewersForAt: CURR_REVIEWER_FOR_AT - name: notify image: tencentcom/wecom-message settings: msgType: markdown robot: "155af237-6041-4125-9340-000000000000" content: | > ${CURR_REVIEWER_FOR_AT} > > ${CNB_PULL_REQUEST_TITLE} > [${CNB_EVENT_URL}](${CNB_EVENT_URL}) > > from ${CNB_BUILD_USER} ``` -------------------------------- ### Managing Environment Variables: Add, Modify, Delete Source: https://docs.cnb.cool/zh/build/env.md Demonstrates how to add new environment variables, modify existing ones, and delete variables by setting them to null or an empty string. ```yaml main: push: - env: CUSTOM_ENV_DATE_INFO: default CUSTOM_ENV_FOR_DELETE: default stages: - name: set env script: echo -n $(date "+%Y-%m-%d %H:%M") exports: # 新增 code: CUSTOM_ENV_DATE_CODE # 修改 info: CUSTOM_ENV_DATE_INFO # 删除 CUSTOM_ENV_FOR_DELETE: null # 删除 # CUSTOM_ENV_FOR_DELETE: - name: echo env script: - echo $CUSTOM_ENV_DATE_CODE - echo $CUSTOM_ENV_DATE_INFO - echo $CUSTOM_ENV_DATE_STDOUT - echo $CUSTOM_ENV_FOR_DELETE - echo $CUSTOM_ENV_GIT_TAG ``` -------------------------------- ### Define Deployment Environments Source: https://docs.cnb.cool/zh/build/deploy.md Configure development, staging, and production environments by adding a `.cnb/tag_deploy.yml` file to your repository root. This allows users to select the deployment environment from the UI. ```yaml environments: # name: 环境名,点击该环境对应的部署按钮将触发 .cnb.yml 中的 tag_deploy.development 事件流水线 - name: development description: Development environment # 环境变量(触发流水线时,会将环境变量传入流水线,包括部署流水线、web_trigger 流水线) env: name: development # CNB_BRANCH: 环境变量,部署事件中,为 tag 名 tag_name: $CNB_BRANCH # 部署按钮权限控制 # 不配置:有仓库写权限和 tag_push 权限的用户可点击部署按钮 # 配置:有仓库写权限,且满足 roles 或 users 其中之一才有权限点击部署按钮 permissions: # roles 和 users 配置其中之一或都配置均可,二者满足其一即可 # 角色非向上包含关系。例如如下配置,表示仅 master 或 developer 角色才有权限,owner 即使仓库权限更高,但此处无权限 roles: - master - developer users: - name1 - name2 - name: staging description: Staging environment # 自定义触发的部署流水线标题 # title: 部署预发布环境 env: name: staging # CNB_BRANCH: 环境变量,部署事件中,为 tag 名 tag_name: $CNB_BRANCH - name: production description: Production environment # 环境变量(触发流水线时,会将环境变量传入流水线,包括部署流水线、web_trigger 流水线) env: name: production # CNB_BRANCH: 环境变量,部署事件中,为 tag 名 tag_name: $CNB_BRANCH button: - name: 按钮名1 # 如存在,则将作为流水线 title,否则流水线使用默认 title;可支持环境变量替换(仅支持替换一级,环境变量指的是当前配置的 env 和 inputs。仅支持 ${xxx} 写法。) description: 按钮描述 # 触发的 CI 事件名,需要在 .cnb.yml 中配置 event: web_trigger_one # 是否默认按钮 isDefault: false # 权限控制,不配置则有仓库写权限的用户可触发构建 # 如果配置,则需要有仓库写权限,并且满足 roles 或 users 其中之一才有权限触发构建 # 注意:仅支持在页面检查 permissions 权限 permissions: # roles 和 users 配置其中之一或都配置均可,二者满足其一即可 # 角色非向上包含关系。例如如下配置,表示仅 master 或 developer 角色才有权限,owner 即使仓库权限更高,但此处无权限 roles: - master - developer users: - name1 - name2 # 环境变量 env: # 默认传入的环境变量,其中 key 值(a,b,c)为环境变量名,支持如下两种格式 a: 1 b: 2 c: # 环境变量别名 name: 变量c # 环境变量值 value: 3 # 可输入环境变量,可覆盖上述 env 的变量配置 inputs: # 目前支持以下三种格式:输入框(input)、多行文本输入框(textarea)、下拉选择框(select 支持单选和多选),switch 开关(switch),radio 单选框(radio) # 其中 key 值(var1、var2、var3、var4、var5、var6)为环境变量名 # inputs 也支持分组,见下方按钮二 inputs 配置 var1: # 输入框 name: 变量1 description: 变量1描述 placeholder: 请输入变量1 required: true # 是否必填 type: input default: 默认值1 var2: # 输入框 name: 变量2 description: 变量2描述 placeholder: 请输入变量2 required: true type: textarea default: 默认值2 var3: # 单选下拉选择框 name: 变量3 description: 变量3描述 placeholder: 请选择变量3 required: false type: select default: value1 options: - name: 选项1 value: value1 description: 选项1描述 - name: 选项2 value: value2 description: 选项2描述 var4: # 多选下拉选择框 name: 变量4 description: 变量4描述 placeholder: 请选择变量4 required: false type: select # 是否支持多选,多选结果用分号分隔 multiple: true default: value1,value2 options: - name: 选项1 value: value1 description: 选项1描述 - name: 选项2 value: value2 description: 选项2描述 - name: 选项3 value: value3 description: 选项3描述 var5: # switch 开关 name: 变量5 description: 变量5描述 required: false type: switch default: value1 options: - name: 选项1 value: value1 description: 选项1描述 - name: 选项2 value: value2 description: 选项2描述 var6: # radio 单选框 name: 变量6 description: 选择变量6 required: false type: radio default: value1 options: - name: 选项1 value: value1 description: 选项1描述 - name: 选项2 value: value2 description: 选项2描述 - name: 按钮名2 description: 按钮描述 event: web_trigger_two inputs: # inputs 支持分组,如下所示,用数组格式,每个数组元素代表一个分组。 # 分组仅在页面显示上有区别,不影响实际的环境变量传入 - name: 分组一 inputs: # 目前支持以下三种格式:输入框(input)、多行文本输入框(textarea)、下拉选择框(select 支持单选和多选),switch 开关(switch),radio 单选框(radio) ``` -------------------------------- ### Variable Substitution in Dockerfile LABEL Source: https://docs.cnb.cool/zh/build/env.md Demonstrates that `settingsFrom` specified in a Dockerfile's `LABEL` instruction also supports variable substitution. ```dockerfile FROM node:20 LABEL cnb.cool/settings-from="$address" ``` -------------------------------- ### VSCode YAML 插件配置 Source: https://docs.cnb.cool/zh/build/configuration.md 在 VSCode 的 `settings.json` 文件中配置 `yaml.schemas`,以启用对 `.cnb.yml` 等文件的语法检查和自动补全。 ```json { "yaml.schemas": { "https://docs.cnb.cool/conf-schema-zh.json": ".cnb.yml", "https://docs.cnb.cool/tag-deploy-schema-zh.json": ".cnb/tag_deploy.yml", "https://docs.cnb.cool/web-trigger-schema-zh.json": ".cnb/web_trigger.yml" } } ``` -------------------------------- ### 配置仅预览模式 Source: https://docs.cnb.cool/zh/workspaces/only-preview.md 通过 `.cnb.yml` 文件配置云原生开发的仅预览模式,包括启用预览、设置启动命令、保活时间和进程模式。 ```yaml $: vscode: - docker: build: .ide/Dockerfile services: # launch 启动命令中如需使用到 docker 命令或 docker compose 命令,docker service 需写在 vscode service 前面 - docker - name: vscode options: # 启用预览模式 onlyPreview: true # 启动业务端口的命令,端口必须启动在 8686 launch: node index.js # 离线保活时间,单位毫秒,不设置默认 10 分钟没有心跳(检测不到开发环境内的 http 连接)即关闭开发环境 keepAliveTimeout: 10m # 是否以守护进程模式启动,默认为 false # true:launch 命令启动的服务直接运行在后台,无法查看启动日志 # false:可以查看启动日志,等待服务启动完成并主动退出后继续后续流程 # 推荐设置为 false,方便通过启动日志定位问题 daemon: true # 是否备份/恢复用户数据(仅预览模式下生效),默认为 false # 设置为 true 时:启动时恢复上次用户漫游数据和未提交更改,运行时定时备份,关闭时再次备份 backup: true # 开发环境启动后会执行的任务 stages: - name: ls script: ls -al ``` -------------------------------- ### Configure Custom Resource Specifications Source: https://docs.cnb.cool/zh/workspaces/custom-dev-pipeline.md Specify the desired CPU cores for the development environment runner. The maximum supported is 64 cores, with memory scaling at 2 GB per CPU. ```yaml $: vscode: - runner: cpus: 64 docker: build: .ide/Dockerfile services: - vscode - docker stages: - name: ls script: ls -al ``` -------------------------------- ### Pipeline Name Variable Substitution Source: https://docs.cnb.cool/zh/build/env.md Demonstrates how pipeline, stage, and job names can be dynamically set using environment variables in a .cnb.yml file. ```yaml main: push: - name: build in $CNB_REPO_SLUG env: platform: amd64 imports: - env1.yml - env2.yml stages: - name: stage_$SOME_ENV script: echo "hello world" ``` -------------------------------- ### Basic .cnb.yml for Push Event Source: https://docs.cnb.cool/zh/build/trigger-rule.md This is a basic .cnb.yml configuration for a push event on the main branch. It defines two pipelines that will execute sequentially. ```yaml main: # 触发分支 push: # 触发事件,对应一个构建,可以包含多条 Pipeline。既可以是数组,也可以是对象。 - stages: # 流水线 1 - echo "do some job" - stages: # 流水线 2 - echo "do some job" ``` -------------------------------- ### 云原生构建配置:对象形式流水线 Source: https://docs.cnb.cool/zh/build/configuration.md 展示了如何使用对象形式定义 `main` 分支的 `push` 和 `pull_request` 事件下的多条流水线,这些流水线将并发执行。 ```yaml # 流水线结构:对象形式 main: push: # main 分支的 push 事件包含两条流水线 push-pipeline1: # 流水线名称,必须唯一 stages: - name: job1 script: echo 1 push-pipeline2: # 流水线名称,必须唯一 stages: - name: job2 script: echo 2 pull_request: # main 分支的 pull_request 事件包含两条流水线 pr-pipeline1: # 流水线名称,必须唯一 stages: - name: job1 script: echo 1 pr-pipeline2: # 流水线名称,必须唯一 stages: - name: job2 script: echo 2 ``` -------------------------------- ### 云原生构建配置:数组形式流水线 Source: https://docs.cnb.cool/zh/build/configuration.md 展示了如何使用数组形式定义 `main` 分支的 `push` 和 `pull_request` 事件下的多条流水线,这些流水线将并发执行。 ```yaml # 流水线结构:数组形式 main: push: # main 分支的 push 事件包含两条流水线 - name: push-pipeline1 # 流水线名称,可选 stages: - name: job1 script: echo 1 - name: push-pipeline2 # 流水线名称,可选 stages: - name: job2 script: echo 2 pull_request: # main 分支的 pull_request 事件包含两条流水线 - name: pr-pipeline1 # 流水线名称,可选 stages: - name: job1 script: echo 1 - name: pr-pipeline2 # 流水线名称,可选 stages: - name: job2 script: echo 2 ``` -------------------------------- ### 在非 VSCode 事件中启动开发环境 Source: https://docs.cnb.cool/zh/workspaces/workspace-vs-build.md 在任何流水线中声明 `service.vscode` 即可启动云原生开发环境。此示例展示了在分支创建时自动创建开发环境并推送通知。 ```yaml feature/**: # 分支创建 branch.create: - services: # 声明 vscode service,创建云原生开发环境 - vscode # 引用密钥仓库文件导入环境变量 企业微信机器人地址 imports: https://cnb.cool//-/blob/main/xxx/envs.yml stages: - name: notify image: tencentcom/wecom-message settings: robot: $ROBOT content: | 环境创建好啦~ [$CNB_BRANCH]($CNB_VSCODE_WEB_URL) ``` -------------------------------- ### Variable Substitution in Pipeline Docker Configuration Source: https://docs.cnb.cool/zh/build/env.md Shows variable substitution for Docker `image`, `build`, and `volumes` properties. It illustrates how to use environment variables to specify either an image to use or a Dockerfile to build from, and to define volume paths. ```yaml .docker-volume: &docker-volume docker: # image 和 build 选择一个使用 image: $image build: $build volumes: - $volume_path main: push: install: env: volume_path: node_modules # image无值,便会使用 build 构建一个镜像 # image: node:22-alpine build: Dockerfile <<: *docker-volume stages: - name: install script: npm install axios # 通知其他流水线执行 - name: resolve type: cnb:resolve options: key: install build: env: volume_path: node_modules image: node:22-alpine # build 无值,便会使用 image # build: Dockerfile <<: *docker-volume stages: # 等待 install 流水线 - name: await type: cnb:await options: key: install - name: ls script: ls node_modules ``` -------------------------------- ### 安装 openssh 的 Dockerfile Source: https://docs.cnb.cool/zh/workspaces/only-preview.md 在自定义开发环境中安装 openssh 服务,以支持 SSH 连接到仅预览模式的云原生开发环境。 ```dockerfile # .ide/Dockerfile # 可将 node 替换为需要的基础镜像 FROM node:20 # 安装 ssh 服务,用于支持 VSCode 等客户端通过 Remote-SSH 访问开发环境(也可按需安装其他软件) RUN apt-get update && apt-get install -y git wget unzip openssh-server # 指定字符集支持命令行输入中文(根据需要选择字符集) ENV LANG C.UTF-8 ENV LANGUAGE C.UTF-8 ``` -------------------------------- ### Configure Custom Development Environment Image Source: https://docs.cnb.cool/zh/workspaces/custom-dev-pipeline.md Define a custom Docker image for your development environment. This snippet shows how to specify the image and the services to be included. ```yaml $: # vscode 事件:专供页面中启动云原生开发用 vscode: - docker: # 自定义镜像作为开发环境 image: node:20 services: - vscode - docker stages: - name: ls script: ls -al ``` -------------------------------- ### Variable Substitution in Built-in Task Options Source: https://docs.cnb.cool/zh/build/env.md Shows how environment variables defined in the `env` section are substituted into the `options` and `optionsFrom` properties of a built-in task. The `$address` variable is replaced by `options.yml`, and `$description` by the value of `description` in the `env` section. ```yaml # options.yml name: Nightly ``` ```yaml main: push: - env: address: options.yml description: publish for xx task stages: - name: git release type: git:release # $address 会被替换成 env 中的 "options.yml" optionsFrom: $address # options.yml 中的 name 会合并到 options 中 options: # $description 会被替换成 env 中的 "publish for xx task" description: $description ``` -------------------------------- ### Output for Adding Reviewers Source: https://docs.cnb.cool/zh/build/internal-steps.md The output structure when reviewers are added, including a list of effective reviewers and their corresponding at-mention format for notifications. ```json { // 当前有效的评审人 "reviewers": [], // reviewers 对应的 at 消息格式,方便发送通知 "reviewersForAt": [] } ``` -------------------------------- ### Variable Substitution with Imports Source: https://docs.cnb.cool/zh/build/env.md Illustrates how variables declared in imported YAML files can be substituted into subsequent imports and task configurations. Variables defined in earlier files in an import array are available to later files. ```yaml # env1.yml address: env2.yml platform: amd64 ``` ```yaml # env2.yml # 读取 env1.yml 中的 platform 属性值进行替换 action: build for $platform ``` ```yaml main: push: - imports: - env1.yml # env1.yml 中声明了 address,$address 会被替换成 env2.yml - $address stages: - name: echo action # 读取 env2.yml 中的 action 属性值进行替换 script: echo $action ``` -------------------------------- ### JavaScript Interface for ReviewersConfig Source: https://docs.cnb.cool/zh/build/internal-steps.md Defines the structure for reviewer configuration in JavaScript, where keys are file paths and values are strings of reviewer names. ```javascript { [key: String]: String } ``` -------------------------------- ### Allow Failure Configuration Source: https://docs.cnb.cool/zh/build/env.md Illustrates how to set a stage to allow failure using the `allowFailure` attribute, even if the script within the stage produces an error. The variable `$allow_fail` is used to control this behavior. ```yaml main: push: - env: allow_fail: true stages: - name: echo allowFailure: $allow_fail # 脚本执行会报错,但 allowFailure 为 true,任务被认为是成功的 script: echo1 1 ``` -------------------------------- ### Variable Substitution in Plugin Task Settings Source: https://docs.cnb.cool/zh/build/env.md Illustrates variable substitution for `settingsFrom` and `settings` in a plugin task. The `$address` variable is replaced by `settings.yml`, and `$message` by the value of `message` in the `env` section. ```yaml # settings.yml robot: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx ``` ```yaml main: push: - env: address: settings.yml message: pr check stages: - name: notify image: tencentcom/wecom-message # $address 会被替换成 env 中的 "settings.yml" settingsFrom: $address # settings.yml 中的 robot 会合并到 settings 中 settings: # $message 会被替换成 env 中的 "pr check" content: $message ``` -------------------------------- ### Pipeline Configuring Plugin Task with Settings Source: https://docs.cnb.cool/zh/build/file-reference.md Pipeline configuration for a plugin task that uses settings from a remote secret file. It specifies the image and the URL for the settings. ```yaml # 流水线配置文件 .cnb.yml 配置插件任务 name: image job image: registry.com/image1/print:latest settingsFrom: - https://cnb.cool//-/blob/main/xxx/image-settings.yml ``` -------------------------------- ### Add Reviewers and Assignees Source: https://docs.cnb.cool/zh/build/internal-steps.md Use this to add specific users as reviewers or assignees for a pull request. ```yaml main: pull_request: - stages: - name: 添加评审人 type: git:reviewer options: type: add-reviewer reviewers: aaa;bbb - name: 添加处理人 type: git:reviewer options: type: add-assignee reviewers: ccc;ddd ``` -------------------------------- ### Output for Adding Assignees Source: https://docs.cnb.cool/zh/build/internal-steps.md The output structure when assignees are added, including a list of effective assignees and their corresponding at-mention format for notifications. ```json { // 当前有效的处理人 "assignees": [], // assignees 对应的 at 消息格式,方便发送通知 "assigneesForAt": [] } ``` -------------------------------- ### Squash Merge with Custom Message and Footer Output Source: https://docs.cnb.cool/zh/build/internal-steps.md This is the resulting commit message after applying the configuration with a custom message and footer for squash merging. It includes the custom message, footer lines, and automatically appended PR information. ```text add feature for some jobs --story=123 --story=456 PR-URL: #916 Reviewed-By: tom Reviewed-By: jerry Co-authored-by: jack ``` -------------------------------- ### Define Tasks to Run Before Environment Destruction Source: https://docs.cnb.cool/zh/workspaces/custom-dev-pipeline.md Specify tasks that must be executed before the development environment is destroyed. This snippet defines two end stages with echo commands. ```yaml $: vscode: - docker: image: node:20 services: - vscode - docker # 开发环境启动后执行的任务 stages: - name: ls script: ls -al # 开发环境销毁前执行的任务 endStages: - name: end stage 1 script: echo "end stage 1" - name: end stage 2 script: echo "end stage 2" ``` -------------------------------- ### Add Reviewer from Repository Members Source: https://docs.cnb.cool/zh/build/internal-steps.md Automatically selects and adds one reviewer from the current repository's members. ```yaml main: pull_request: - stages: - name: add reviewer type: git:reviewer options: type: add-reviewer-from-repo-members ``` -------------------------------- ### Exporting Conditional Skip Reason Source: https://docs.cnb.cool/zh/build/env.md When using conditional logic like 'if' in a job, you can export the skip reason as an environment variable if the job is skipped. ```yaml - name: use if if: exit -1 exports: skip: REASON - name: tell last # $REASON 的值为 if 这个字符串 script: echo $REASON ``` -------------------------------- ### Shell: Exporting Simple Variables Source: https://docs.cnb.cool/zh/build/env.md Export simple string variables directly from a shell script using the '##[set-output key=value]' format. ```bash echo "##[set-output redline_msg=some value]" ``` -------------------------------- ### Variable Substitution in Nested Env Declarations Source: https://docs.cnb.cool/zh/build/env.md Shows how environment variables declared in a parent `env` block can be referenced and substituted in a nested `env` block within a stage. ```yaml main: push: - env: cat_name: tomcat stages: - name: echo env env: # 使用上层 env 声明的 cat_name 值进行替换 name: "cat $cat_name" # 输出 cat tomcat script: echo $name ``` -------------------------------- ### Variable Substitution in Pipeline Runner Tags Source: https://docs.cnb.cool/zh/build/env.md Demonstrates using environment variables to dynamically set pipeline runner tags. The `$CNB_PIPELINE_NAME` variable is used to construct a tag, allowing for architecture-specific builds. ```yaml # 构建不同架构下的镜像 .build: &build runner: tags: cnb:arch:$CNB_PIPELINE_NAME services: - docker stages: - name: docker build script: echo "docker build for $CNB_PIPELINE_NAME" main: push: # 下面 "amd64" 和 "arm64:v8" 会被声明为内置环境变量 CNB_PIPELINE_NAME 的值 amd64: *build "arm64:v8": *build ``` -------------------------------- ### Using Environment Variables in Script Tasks Source: https://docs.cnb.cool/zh/build/env.md Demonstrates how built-in and self-defined environment variables are passed to script tasks within a pipeline. The `CNB_BRANCH` is a built-in variable, while `cat_name` is self-defined. ```yaml main: push: - stages: - name: test internal env # CNB_BRANCH 为内置环境变量 script: echo $CNB_BRANCH - name: test self defined env env: cat_name: tomcat script: echo $cat_name ``` -------------------------------- ### Node.js: Exporting Base64 and Escaped Variables Source: https://docs.cnb.cool/zh/build/env.md Use Node.js to output variables with newlines, encoded in Base64 or escaped format, for CI to parse into environment variables. ```javascript // test.js const value = '测试字符串\ntest string'; // 输出 base64 编码的变量值 console.log(`##[set-output redline_msg_base64=base64,${Buffer.from(value, 'utf-8').toString('base64')}]`); // 输出 escape 编码的变量值 console.log(`##[set-output redline_msg_escape=${escape(value)}]`) ``` ```yaml main: push: - docker: image: node:20-alpine stages: - name: set output env script: node test.js # 将 test.js 输出的变量导出为环境变量 exports: redline_msg_base64: BASE64_KEY redline_msg_escape: ESCAPE_KEY - name: echo env script: - echo "BASE64_KEY $BASE64_KEY" - echo "ESCAPE_KEY $ESCAPE_KEY" ``` -------------------------------- ### Shell: Exporting Base64 Encoded Variables Source: https://docs.cnb.cool/zh/build/env.md Export Base64 encoded variables directly from a shell script. Use the '-w 0' option with the base64 command to prevent unwanted newlines. ```yaml main: push: - stages: - name: set output env script: echo "##[set-output redline_msg_base64=base64,$(echo -e "测试字符串\ntest string" | base64 -w 0)]" exports: redline_msg_base64: BASE64_KEY - name: echo env script: - echo -e "BASE64_KEY $BASE64_KEY" ``` -------------------------------- ### Auto-Merge with Push Event Trigger Source: https://docs.cnb.cool/zh/build/internal-steps.md This configuration combines auto-merging on 'pull_request.mergeable' events with subsequent build and publish steps triggered by a 'push' event on the 'main' branch. The last reviewer to approve becomes the trigger for the push event. ```yaml main: push: - stages: - name: build script: npm run build - name: publish script: npm run publish pull_request.mergeable: - stages: - name: automerge type: git:auto-merge options: mergeType: merge ``` -------------------------------- ### Plugin Task Secret Configuration Source: https://docs.cnb.cool/zh/build/file-reference.md Configuration file for image settings in a secret repository. It defines allowed images and slugs, along with arguments for plugin tasks. ```yaml # 密钥仓库中的配置文件 image-settings.yml allow_images: "registry.com/image1/**" allow_slugs: "p1/**" arg1: arg1 arg2: arg2 ``` -------------------------------- ### Configure Knowledge Base Update Task in Pipeline Source: https://docs.cnb.cool/zh/ai/knowledge-base.md Define a pipeline task in `.cnb.yml` to automatically update the knowledge base when code is committed to the main branch. This task handles document slicing, tokenization, and vectorization. ```yaml main: push: - stages: - name: build knowledge base type: knowledge:update options: include: "**/**.md" ``` -------------------------------- ### Exporting Built-in Task Results Source: https://docs.cnb.cool/zh/build/env.md Export specific outputs from built-in tasks, such as version, URL, or deep object properties, as environment variables. ```yaml main: push: - stages: - name: xxxx type: xxx:xxx options: product: public name: cnb dist: release/ exports: version: CUSTOM_ENV_VERSION url: CUSTOM_ENV_URL # 支持对象深层取值 nextRelease.gitTag: CUSTOM_ENV_GIT_TAG - name: echo env script: - echo $CUSTOM_ENV_VERSION - echo $CUSTOM_ENV_URL ``` -------------------------------- ### Pipeline Secret Configuration Source: https://docs.cnb.cool/zh/build/file-reference.md Configuration file for secrets in a secret repository. It specifies allowed slugs, events, and branches, along with Docker credentials. ```yaml # 密钥仓库中的配置文件 secret.yml allow_slugs: "p1/**" allow_events: push allow_branches: main # dockerhub 用户名和密码 DOCKER_USER: docker-user DOCKER_PWD: docker-pwd ```