Stage
# Stage Introduction
- type:
Job
|Array<Job>
|Object<name: Job>
Stage
represents a build stage that can consist of one or more Job
s. See Job Introduction for more information.
# Single Job
If a Stage
has only one Job
, the Stage
can be omitted, and the Job
can be written directly.
stages:
- name: stage1
jobs:
- name: job A
script: echo hello
It can be simplified as follows:
- stages:
- name: job A
script: echo hello
# Serial Jobs
When the value is an array (ordered), the group of Job
s will be executed serially.
# Serial Jobs
stages:
- name: install
jobs:
- name: job1
script: echo "job1"
- name: job2
script: echo "job2"
# Parallel Jobs
When the value is an object (unordered), the group of Job
s will be executed in parallel.
# Parallel Jobs
stages:
- name: install
jobs:
job1:
script: echo "job1"
job2:
script: echo "job2"
Multiple Job
s can be organized flexibly in serial or parallel. Here's an example of first serial, then parallel execution:
main:
push:
- stages:
- name: serial first
script: echo "serial"
- name: parallel
jobs:
parallel job 1:
script: echo "1"
parallel job 2:
script: echo "2"
- name: serial next
script: echo "serial next"
# name
- type:
String
The name of the Stage
.
# ifNewBranch
- type:
Boolean
- default:
false
If set to true
,
the Stage
will only be executed when the current branch is a new branch (i.e., CNB_IS_NEW_BRANCH
is true
).
If any of the conditions
ifNewBranch
/ifModify
/if
are satisfied simultaneously, theStage
will be executed.
# ifModify
- type:
Array<String>
|String
To specify that a stage should only be executed when the corresponding file changes,
you can use a glob
expression string or an array of strings.
# if
- type:
Array<String>
|String
One or more Shell scripts that determine whether the Stage
should be executed based on the exit code of the script.
When the exit code is 0
, it indicates that the step should be executed.
Example 1: Checking the value of a variable
Shell expression syntax (opens new window)
main:
push:
- env:
IS_NEW: true
stages:
- name: is new
if: |
[ "$IS_NEW" = "true" ]
script: echo is new
- name: is not new
if: |
[ "$IS_NEW" != "true" ]
script: echo not new
Example 2: Checking the output of a task
main:
push:
- stages:
- name: make info
script: echo 'haha'
exports:
info: RESULT
- name: run if RESULT is haha
if: |
[ "$RESULT" = "haha" ]
script: echo $RESULT
# env
- type:
Object
Same as Pipeline env, but only applicable to the current Stage
.
Stage env
takes precedence over Pipeline env
.
# imports
- type:
Array<String>
|String
Same as Pipeline imports, but only applicable to the current Stage
.
# retry
- type:
Number
- default:
0
Number of retry attempts in case of failure. 0
means no retries.
# lock
- type:
Boolean
|Object
Sets a lock for the Stage
. The lock is automatically released after the Stage
is executed.
Locks cannot be used across repositories.
Behavior: If task A acquires the lock, task B will wait for the lock to be released before it can acquire the lock and continue executing the task.
lock.key
- type:
String
Custom lock name. The default value is
branchName-pipelineName-stageIndex
.- type:
lock.expires
- type:
Number
- default:
3600
(1 hour)
Lock expiration time in seconds. After expiration, the lock is automatically released.
- type:
lock.wait
- type:
Boolean
- default:
false
Specifies whether to wait if the lock is already occupied.
- type:
lock.timeout
- type:
Number
- default:
3600
(1 hour)
Specifies the timeout for waiting for the lock in seconds.
- type:
Example 1: lock
in Boolean format
main:
push:
- stages:
- name: stage1
lock: true
jobs:
- name: job1
script: echo "job1"
Example 2: lock
in Object format
main:
push:
- stages:
- name: stage1
lock:
key: key
expires: 600 # 10 minutes
wait: true
timeout: 60 # maximum wait time of 1 minute
jobs:
- name: job1
script: echo "job1"
# image
- type:
Object
|String
Specifies the environment image for the current Stage
.
All tasks under this Stage
will be executed in this image environment by default.
image.name
:string
Image name, such asnode:20
.image.dockerUser
:String
Specifies the Docker username used to pull the specified image.image.dockerPassword
:String
Specifies the Docker user password used to pull the specified image.
If image
is specified as a string, it is equivalent to specifying image.name
.