Events
# Event-Driven
The Cloud Native Build
pipeline is triggered by events.
Let's take the push
event as an example to explain the process from code changes to pipeline execution:
Users commit code and
push
it to the remote server (CNB).The
Cloud Native Build
receives thepush
event trigger request and analyzes information such asrepository
,event
, andbranch
.The
Cloud Native Build
retrieves the configuration file (default:.cnb.yml
) from therepository
branch and parses the pipeline configuration corresponding to theevent
.The
Cloud Native Build
executes the pipeline.
If multiple events
are triggered, such as creating a branch triggering both branch.create
and push
,
the corresponding events in the configuration file will be treated as different builds
and executed in parallel.
Multiple event
example:
dev/1:
push:
- stages:
- name: echo
script: echo 1
# Match branch.create events for all branches
(**):
branch.create:
- stages:
- name: echo
script: echo 2
# Git Events
Git events are triggered by remote code operations.
# push
Branch push
event.
# branch.create
Branch creation event, also triggers the push
event.
# branch.delete
Branch deletion event.
Pipeline configurations can be attached to branch names or $
.
The pipeline will use the configuration file of the default branch (reason: the branch has been deleted at runtime).
Example:
dev/1:
branch.delete:
- stages:
- name: echo
# CNB_BRANCH value is the deleted branch
script: echo $CNB_BRANCH
$:
branch.delete:
- stages:
- name: echo
# CNB_BRANCH value is the deleted branch
script: echo $CNB_BRANCH
# pull_request
Branch merge event triggered by the creation, reopening, and source branch push of a PR.
Refer to Configuration File for the difference between pull_request
and pull_request.target
.
Example: Dynamic Notification of Pull Requests to a Group
# pull_request.target
Branch merge event triggered by the creation, reopening, and source branch push of a PR.
Refer to Configuration File for the difference between pull_request
and pull_request.target
.
# pull_request.approved
Branch merge event triggered when a user approves the PR
for merging.
TIP
Note that having a user approve the PR
does not necessarily mean that the PR
is in an approved state,
as protected branches may require multiple reviewers' approval.
# pull_request.changes_requested
Branch merge event triggered when a user requests changes to be made in the PR.
Example: Dynamic notification to a group for PR
# pull_request.mergeable
Triggered when an open PR
meets the following conditions:
- The target branch is a protected branch with the following rules checked:
- Requires approval from reviewers
- Requires passing status checks
- It is mergeable:
- No code conflicts
- Passes status checks (if any)
- Approved by reviewers
Example: Dynamic notification to a group for PR
# pull_request.merged
Event triggered when a branch is successfully merged.
TIP
When branch a is merged into branch b, it triggers the pull_request.merged and push events under branch b.
Example: Dynamic notification to a group for PR
# tag_push
tag push
event.
Example:
v*:
tag_push:
- stages:
- name: echo
# CNB_BRANCH value is the tag name
script: echo $CNB_BRANCH
# vscode
Event triggered by clicking the "Cloud Native Development" button on the page.
Refer to Customize the Creation Process for Cloud Native Development
# auto_tag
Event for automatically generating tags, with the event name auto_tag
.
Trigger Method: It only supports triggering cloud-native builds by clicking the "Auto Generate Tag" button on the tag list page of the repository. The cloud-native build will start a pipeline to automatically generate tags. The pipeline uses the cnbcool/git-auto-tag image to implement automatic tag generation. Tag Format Explanation: By default, the tag format is
0.0.1
or a similar format. If the latest tag starts withv
, the automatically generated tag will also include thev
prefix, such asv0.0.1
.How to Customize the Pipeline for Auto-Generating Tags:
Users can add a .cnb.yml file in the root directory and include the following configuration format to override the default template.
# User-defined .cnb.yml configuration
main: # Default branch, can be replaced with the actual default branch of the repository
auto_tag: # Event name
- stages:
- name: Auto-generate Tag
image: "cnbcool/git-auto-tag:latest"
settings:
tagFormat: 'v\${version}'
branch: $CNB_BRANCH
repoUrlHttps: $CNB_REPO_URL_HTTPS
exports:
tag: NEW_TAG
- name: show tag
script: echo $NEW_TAG
# tag_deploy.*
Events triggered by the deployment button on the tag/release page of the repository, see documentation
# Issue Events
Issue events are triggered by actions such as creating or closing an issue.
Pipeline configurations for issue events should be placed under $
.
# issue.open
Event triggered when an issue is created.
Example: Notification of issues to an enterprise WeChat group
# issue.close
Event triggered when an issue is closed.
# issue.reopen
Event triggered when a closed issue is reopened.
# Custom Events
Custom events must have event names that are either api_trigger
or start with api_trigger_
, such as api_trigger_test
.
There are two ways to trigger custom events:
- Triggered by cnb:apply
- Triggered by cnb:trigger
# Web Page Custom Events
Web page custom events must have event names that are either web_trigger
or start with web_trigger_
, such as web_trigger_test
.
These events can only be triggered on the web page.
Use Cases:
- Can be used in conjunction with Deployment capabilities
- You can define a Custom Button on the page to manually trigger builds
(supporting input of environment variables, only supports triggering the
web_trigger
event)
# Crontab Events
Crontab events are triggered by crontab. For more details, please refer to the Crontab documentation.
# Branch Matching
# Matching Branches with glob Patterns
Branch names can be matched using glob patterns (learn more about glob matching (opens new window)). For example:
# Match all branches starting with dev/
"dev/*":
push:
- push_pipeline
# Match main or dev branches
"(main|dev)":
push:
- push_pipeline
# Match all branches except main and dev
"**/!(main|dev)":
push:
- push_pipeline
# Match all branches
"**":
push:
- push_pipeline
# Fallback, match branches not matched by glob patterns
"$":
tag_push:
- push_pipeline
# Matching Strategy
The matching strategy follows a staged approach, where matching is done based on priority. Only if there is no match in the previous stage, the next stage is attempted.
- Match branch names using
glob
patterns. - Fallback to match all branches not matched by
glob
patterns using$
.
If multiple glob
rules match, all matched rules will be executed in parallel.
# Skipping Pipelines
In push
and branch.create
events, the pipeline will be skipped under the following conditions:
- The most recent commit message contains
[ci skip]
or[skip ci]
. - The
git push -o ci.skip
command is used.
For example:
git commit -m "feat: Add some feature [ci skip]"
git push origin main
or
git commit -m "feat: Add some feature"
git push origin main -o ci.skip
# Configuration File Version Selection
Since configuration files are stored in the project repository and are subject to Git version control,
there may be multiple versions of the configuration file. During the build process,
developers need to know which version of the configuration file to use. Fortunately,
the Cloud Native Build
mechanism is very intuitive, and in most cases, developers do not need to worry about this.
The following describes how Cloud Native Build
selects which version of the configuration file to use in each event.
It is important to note that the pull_request
event has a more complex selection process:
- For
push
,branch.create
, andvscode
events, the configuration file is read from the latest commit node of the current branch. - For
auto_tag
,branch.delete
, andissue.*
events, the configuration file is read from the default branch. - For
tag_push
andtag_deploy.*
events, the configuration file is read from the current tag. - For
pull_request
,pull_request.approved
, andpull_request.changes_requested
events, where there is a source branch and a target branch, the configuration file is read from the pre-merged configuration file. - For
pull_request.merged
events, the configuration file is read from the merged configuration file. - For
pull_request.target
andpull_request.mergeable
events, the configuration file is read from the configuration file of the target branch before merging. - For custom events, please refer to cnb:apply and cnb:trigger.
- For web page custom events, the configuration file is read from the corresponding branch or tag. Please refer to the specific use case documentation.
- For crontab events, the configuration file is read from the specified branch.