Events
# Event-driven
The Cloud Native Build
pipeline is triggered by events.
Let's take the example of the push
event to explain the process from code changes to pipeline execution:
Users submit code and
push
it to the remote server.The
Cloud Native Build
receives apush
event notification and extracts information such asrepository
,event
,branch
, etc.The
Cloud Native Build
retrieves the configuration file.cnb.yml
from therepository
branch and parses the corresponding pipeline configuration based on theevent
.The
Cloud Native Build
executes the pipeline.
An operation can trigger multiple events, such as creating a branch
triggering both branch.create
and push
events, for example:
dev/1:
push:
- stages:
- name: echo
script: echo "do something for push"
# Match branch.create event for all branches
(**):
branch.create:
- stages:
- name: echo
script: echo "do something for branch create"
The pipeline corresponding to the event in the configuration file will be executed in parallel.
The following lists the events supported by Cloud Native Build
.
# Workspace Events
Events triggered by clicking the Workspace
button on the page.
For more details, refer to Workspace
# Branch Events
Events triggered by changes in remote code branches.
# push
Triggered when a branch is pushed
.
# branch.create
Triggered when a branch is created
, also triggers the push
event.
# branch.delete
Triggered when a branch is deleted
.
Pipeline configurations can be attached to the branch name
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 holds the value of the deleted branch
script: echo $CNB_BRANCH
$:
branch.delete:
- stages:
- name: echo
# CNB_BRANCH holds the value of the deleted branch
script: echo $CNB_BRANCH
# Pull Request Events
Events triggered by remote code Pull Request
related operations (referred to as PR
below).
# pull_request
Triggered by PR
creation, reopening, and source branch push
.
Refer to Configuration File
for the difference from pull_request.target
.
# pull_request.target
Triggered by PR
creation, reopening, and source branch push
.
Refer to Configuration File
for the difference from pull_request
.
# pull_request.mergeable
Triggered when an open PR
meets the following conditions:
- The target branch is a protected branch with the following rules enabled:
- Require approval from reviewers
- Require status checks to pass
- Mergeable:
- No code conflicts
- Status checks (if any) pass
- Approved by reviewers
# pull_request.merged
Triggered when a PR
is merged.
TIP
When branch a
is merged into branch b
,
it triggers the pull_request.merged
and push
events under branch b
.
# pull_request.approved
Triggered when a user approves the PR
for merging.
TIP
Having a user approve the review does not necessarily mean the PR
is in an approved state,
as the protected branch may require multiple reviewers' approval.
# pull_request.changes_requested
Triggered when a user requests changes to be made in the PR
.
# Tag Events
Events triggered by remote code and page Tag
related operations.
# tag_push
Triggered when a Tag
is pushed.
Example:
# match v1.0.0.x
v1.0.*:
tag_push:
- stages:
- name: echo tag name
script: echo $CNB_BRANCH
# match all
$:
tag_push:
- stages:
- name: echo tag name
script: echo $CNB_BRANCH
# auto_tag
Automatically generated Tag
event.
Triggering method:
Only supported on the repository's
Tag
list page, by clicking theAutomatically generate Tag
button.Implementation principle:
Starts a pipeline using the cnbcool/git-auto-tag plugin by default to automatically generate
Tags
.Format description:
The
Tag
is in the format of3.8.11
by default. If the latestTag
starts withv
, the automatically generatedTag
will also include thev
, such asv4.1.9
.Customizing the
auto_tag
event pipeline:Users can add a
.cnb.yml
file in the root directory and add the following configuration 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 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 clicking the Deploy
button on the repository's Tag/Release
page.
For more details, refer to Deployment
# Custom Events
Custom
events are triggered by API
, crontab tasks
, and page-related operations.
# API Custom Events
The event name is api_trigger
or starts with api_trigger_
, such as api_trigger_test
.
API custom events can be triggered in the following three ways:
- Triggered by cnb:apply
- Triggered by cnb:trigger
- Triggered by the API
Methods 1 and 2 are encapsulations of method 3
# Page Custom Events
The event name is web_trigger
or starts with web_trigger_
, such as web_trigger_test
.
Only supported for triggering events on the page.
Use cases:
- Can be used in conjunction with Deployment capabilities
- Custom buttons on the page
Manually trigger a build (supports inputting environment variables, only supports triggering web_trigger
events)
# Crontab Task Events
Events triggered by crontab tasks.
For more details, refer to Crontab Tasks.
# Issue Events
Events triggered by operations related to Issues
.
The pipeline configuration for Issue
events should be attached to $
.
# issue.open
Issue
creation event.
# issue.close
Issue
closure event.
# issue.reopen
Issue
reopening event.
# Branch Matching
# Matching Patterns
Branch names support matching using glob
patterns (What is glob matching? (opens new window)), such as:
.push_pipeline: &push_pipeline
- stages:
- name: do something
script: echo "do something"
# 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
"$":
push:
- *push_pipeline
tag_push:
- *push_pipeline
# Matching Strategy
The matching is done in stages, with priority given to each stage. Only if there is no match in the previous stage, the next stage will be attempted.
glob
matching of branch names$
fallback to match all branches not matched byglob
patterns
If multiple glob
rules match, all matched pipelines will be executed in parallel.
# Skipping Pipelines
In push
and branch.create
events, the pipeline will be skipped in the following two cases:
- The most recent commit message contains
[ci skip]
or[skip ci]
- git push -o ci.skip
For example:
git commit -m "feat: some feature [ci skip]"
git push origin main
or
git commit -m "feat: some feature"
git push origin main -o ci.skip
# Configuration File Version Selection
Since the configuration file is in the project repository and is subject to version control by Git,
the configuration file naturally has multiple versions.
Knowing which version of the configuration file is used
in a particular build is something that developers must be aware of.
Fortunately, the mechanism of Cloud Native Build
is very intuitive, and in most cases,
developers do not need to worry about it.
Below are the rules for selecting which version of the configuration file to use in each event.
It is worth noting that the pull_request
event has a relatively complex selection process:
- For
push
,branch.create
, andvscode
events, the configuration file is read from the latest commit node on 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 currentTag
. - For
pull_request
,pull_request.approved
, andpull_request.changes_requested
events, there are source and target branches, and the configuration file is read from the pre-merged state. - For
pull_request.merged
events, the configuration file is read from the merged state. - For
pull_request.target
andpull_request.mergeable
events, the configuration file is read from the target branch before merging. - For API custom events, the version can be specified, and cnb:apply is limited to the version corresponding to the current pipeline.
- For page custom events, the configuration file is read from the corresponding branch or Tag, depending on the specific use case.
- For crontab task events, the configuration file is read from the specified branch.