https://www.jenkins.io/doc/book/pipeline/syntax/

Declarative Pipeline

所有有效的声明性管道都必须包含在管道块中,例如:

1
2
3
pipeline {
/* insert Declarative Pipeline here */
}

agent

代理 agent 部分指定整个pipeline或特定阶段 stage 将在 Jenkins 环境中执行的位置,具体取决于代理部分的放置位置。
该部分必须在pipeline块内的顶层定义,但阶段级别的 stage 使用也是可以的的。

1
2
3
4
5
6
Required     Yes 是否必须有,

Parameters 有参数,详细说明往下看

Allowed 可以在顶层 pipeline 代码块里用,也可以在 stage 代码块里面用

Top Level Agents

1
2
3
4
5
6
7
8
9
10
11
12
13
In agents declared at the outermost level of the Pipeline, the options are invoked after entering the agent. As an example, when using timeout it will be only applied to the execution within the agent.
在 Pipeline 最外层声明的代理中,选项在进入代理后被调用。 例如,当使用超时时,它将仅应用于代理内的执行。这个时候 agent 代理已经分配好了,分配agent就不会算时间了

node("myAgent") {
timeout(unit: 'SECONDS', time: 5) {
stage("One"){
sleep 10
echo 'hello'
}
}
}


Stage Agents

1
2
3
4
5
6
7
8
9
10
11
12
In agents declared within a stage, the options are invoked before entering the agent and before checking any when conditions. In this case, when using timeout, it is applied before the agent is allocated.
在阶段内声明的代理中,在进入代理之前和检查任何 when 条件之前调用选项。例如, 在这种情况下,当使用超时时,它会在分配代理之前应用。
此超时将包括代理配置时间。 由于超时包括代理供应时间,因此在代理分配延迟的情况下,管道可能会失败。 这种情况,如果你的节点是 用完销毁,用完下线的话,这次分配新的节点,或者重新连上节点,这一段的时间都会算在超时里面

timeout(unit: 'SECONDS', time: 5) {
stage("One"){
node {
sleep 10
echo 'Hello'
}
}
}

agent 参数

  1. any Execute the Pipeline, or stage, on any available agent.
    For example:
    agent any

  2. none When applied at the top-level of the pipeline block no global agent will be allocated for the entire Pipeline run and each stage section will need to contain its own agent section.
    none 当应用于管道块的顶层时,不会为整个管道运行分配全局代理,每个阶段部分都需要包含自己的代理部分。
    For example:
    agent none

  3. label Execute the Pipeline, or stage, on an agent available in the Jenkins environment with the provided label.
    For example:
    agent { label ‘my-defined-label’ }

Label conditions can also be used. For example:
agent { label ‘my-label1 && my-label2’ }
or
agent { label ‘my-label1 || my-label2’ }

  1. node node 和label 类似,但是 node 支持 额外的更多的参数,例如customWorkspace
    agent { node { label ‘labelName’ } } behaves the same as agent { label ‘labelName’ },
    but node allows for additional options (such as customWorkspace).

  2. docker Execute the Pipeline, or stage, with the given container which will be dynamically provisioned on a node pre-configured
    to accept Docker-based Pipelines, or on a node matching the optionally defined label parameter.
    docker also optionally accepts an args parameter which may contain arguments to pass directly to a docker run invocation, and an alwaysPull option, which will
    force a docker pull even if the image name is already present.
    使用给定的容器执行管道或阶段,该容器将在预先配置的节点上动态配置, 接受基于 Docker 的管道,或在与可选定义的标签参数匹配的节点上。
    docker 也可以选择接受 一个 args 参数,它可能包含直接传递给 docker run 调用的参数,以及一个 alwaysPull 选项,它将 即使image名称已经存在,也强制执行 docker pull。

For example:
agent { docker ‘maven:3.8.1-adoptopenjdk-11’ }
or
agent {
docker {
image ‘maven:3.8.1-adoptopenjdk-11’
label ‘my-defined-label’
args ‘-v /tmp:/tmp’
}
}

docker 还可以选择接受 registryUrl 和 registryCredentialsId 参数,这将有助于指定要使用的 Docker Registry 及其凭据。 参数 registryCredentialsId 可以单独用于 docker hub 内的私有存储库。 例如:
agent {
docker {
image ‘myregistry.com/node’
label ‘my-defined-label’
registryUrl ‘https://myregistry.com/'
registryCredentialsId ‘myPredefinedCredentialsInJenkins’
}
}

  1. dockerfile Execute the Pipeline, or stage, with a container built from a Dockerfile contained in the source repository.
    In order to use this option, the Jenkinsfile must be loaded from either a Multibranch Pipeline or a Pipeline from SCM.
    Conventionally this is the Dockerfile in the root of the source repository: agent { dockerfile true }.
    If building a Dockerfile in another directory, use the dir option: agent { dockerfile { dir ‘someSubDir’ } }.
    If your Dockerfile has another name, you can specify the file name with the filename option.
    You can pass additional arguments to the docker build …​ command with the additionalBuildArgs option,
    like agent { dockerfile { additionalBuildArgs ‘–build-arg foo=bar’ } }.
    For example, a repository with the file build/Dockerfile.build, expecting a build argument version:
    使用从源存储库中包含的 Dockerfile 构建的容器执行管道或阶段。 为了使用此选项,必须从多分支管道或来自 SCM 的管道加载 Jenkinsfile。 通常这是源存储库根目录中的 Dockerfile:agent { dockerfile true }。 如果在另一个目录中构建 Dockerfile,请使用 dir 选项:agent { dockerfile { dir ‘someSubDir’ } }。 如果您的 Dockerfile 有其他名称,您可以使用 filename 选项指定文件名。 您可以使用 additionalBuildArgs 选项将其他参数传递给 docker build … 命令,例如 agent { dockerfile { additionalBuildArgs ‘–build-arg foo=bar’ } }。 例如,具有文件 build/Dockerfile.build 的存储库,需要构建参数版本:
    agent {
    // Equivalent to “docker build -f Dockerfile.build –build-arg version=1.0.2 ./build/
    dockerfile {
    filename ‘Dockerfile.build’
    dir ‘build’
    label ‘my-defined-label’
    additionalBuildArgs ‘–build-arg version=1.0.2’
    args ‘-v /tmp:/tmp’
    }
    }

    dockerfile also optionally accepts a registryUrl and registryCredentialsId parameters which will help to specify the Docker Registry to use and its credentials. For example:
    dockerfile 还可以选择接受 registryUrl 和 registryCredentialsId 参数,这将有助于指定要使用的 Docker Registry 及其凭据。 例如:
    agent {
    dockerfile {
    filename ‘Dockerfile.build’
    dir ‘build’
    label ‘my-defined-label’
    registryUrl ‘https://myregistry.com/'
    registryCredentialsId ‘myPredefinedCredentialsInJenkins’
    }
    }

  2. kubernetes Execute the Pipeline, or stage, inside a pod deployed on a Kubernetes cluster.
    In order to use this option, the Jenkinsfile must be loaded from either a Multibranch Pipeline or a Pipeline from SCM.
    The Pod template is defined inside the kubernetes { } block. For example, if you want a pod with a Kaniko container inside it,
    you would define it as follows:
    在 Kubernetes 集群上部署的 pod 内执行管道或阶段。 为了使用此选项,必须从多分支管道或来自 SCM 的管道加载 Jenkinsfile。
    Pod 模板在 kubernetes { } 块内定义。 例如,如果你想要一个里面有 Kaniko 容器的 pod,你可以这样定义它:
    agent {
    kubernetes {
    label podlabel
    yaml “””
    kind: Pod
    metadata:
    name: jenkins-agent
    spec:
    containers:

    • name: kaniko
      image: gcr.io/kaniko-project/executor:debug
      imagePullPolicy: Always
      command:
      • /busybox/cat
        tty: true
        volumeMounts:
        • name: aws-secret
          mountPath: /root/.aws/
        • name: docker-registry-config
          mountPath: /kaniko/.docker
          restartPolicy: Never
          volumes:
      • name: aws-secret
        secret:
        secretName: aws-secret
      • name: docker-registry-config
        configMap:
        name: docker-registry-config
        “””
        }

    You will need to create a secret aws-secret for Kaniko to be able to authenticate with ECR. This secret should contain the contents of ~/.aws/credentials. The other volume is a ConfigMap which should contain the endpoint of your ECR registry. For example:
    您需要为 Kaniko 创建一个秘密 aws-secret 才能使用 ECR 进行身份验证。 此机密应包含 ~/.aws/credentials 的内容。 另一个卷是一个 ConfigMap,它应该包含您的 ECR 注册表的端点。 例如:
    {
    “credHelpers”: {
    .dkr.ecr.eu-central-1.amazonaws.com”: “ecr-login”
    }
    }

    Refer to the following example for reference: https://github.com/jenkinsci/kubernetes-plugin/blob/master/examples/kaniko.groovy

agent Common Options

  1. label A string. The label or label condition on which to run the Pipeline or individual stage.

    在使用 node、docker、dockerfile、的时候有使用 这个label选项,并且是 使用node时候这个label是必须的

  2. customWorkspace A string. Run the Pipeline or individual stage this agent is applied to within this custom workspace, rather than the default. It can be either a relative path, in which case the custom workspace will be under the workspace root on the node, or an absolute path. For example:
    一个字符串。 在此自定义工作区中运行此代理应用到的管道或单个阶段,而不是默认阶段。 它可以是相对路径,在这种情况下,自定义工作区将位于节点上的工作区根目录下,也可以是绝对路径。 例如:
    agent {
    node {
    label ‘my-defined-label’
    customWorkspace ‘/some/other/path’
    }
    }
    在使用 node、docker、dockerfile、的时候有使用 这个customWorkspace选项,

  3. reuseNode A boolean, false by default. If true, run the container on the node specified at the top-level of the Pipeline, in the same workspace, rather than on a new node entirely.
    布尔值,默认为 false。 如果为 true,则在同一工作区中的 Pipeline 顶层指定的节点上运行容器,而不是完全在新节点上运行。
    此选项对 docker 和 dockerfile 有效,并且仅在用于单个阶段的代理时有效。

  4. args A string. Runtime arguments to pass to docker run.
    此选项对 docker 和 dockerfile 有效.

agent Example

Example 1. Docker Agent, Declarative Pipeline

1
2
3
4
5
6
7
8
9
10
pipeline {
agent { docker 'maven:3.8.1-adoptopenjdk-11' }
stages {
stage('Example Build') {
steps {
sh 'mvn -B clean verify'
}
}
}
}

在给定名称和标签的新创建容器中执行此管道中定义的所有步骤 , docker 镜像使用 ‘maven:3.8.1-adoptopenjdk-11’

Example 2. Stage-level Agent Section

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
pipeline {
agent none //在 Pipeline 的顶层定义 agent none 可确保 不必要地分配 Executor。 使用 agent none 还会强制每个阶段 stage 部分必须要配置代理部分。
stages {
stage('Example Build') {
agent { docker 'maven:3.8.1-adoptopenjdk-11' } //使用此镜像在新创建的容器中执行此阶段的步骤。 这个容器使用的 'maven:3.8.1-adoptopenjdk-11' 这个镜像
steps {
echo 'Hello, Maven'
sh 'mvn --version'
}
}
stage('Example Test') {
agent { docker 'openjdk:8-jre' } //使用与上一阶段不同的映像在新创建的容器中执行此阶段中的步骤。 这个容器使用的 'openjdk:8-jre' 这个镜像
steps {
echo 'Hello, JDK'
sh 'java -version'
}
}
}
}

post

post 部分定义了一个或多个在管道或阶段运行完成时运行的附加步骤(取决于管道中 post 部分的位置)。
post 可以支持以下任何后置条件块:always、changed、fixed、regression、aborted、failure、success、unstable、unsuccessful 和 cleanup。
这些条件块允许根据管道或阶段的完成状态在每个条件内执行步骤。 条件块按如下所示的顺序执行。

1
2
3
4
5
6
Required     no是否必须有,不是

Parameters 没有参数

Allowed 可以在顶层 pipeline 代码块里用,也可以在 stage 代码块里面用

  1. always
    Run the steps in the post section regardless of the completion status of the Pipeline’s or stage’s run.
  2. changed
    Only run the steps in post if the current Pipeline’s or stage’s run has a different completion status from its previous run.
  3. fixed
    Only run the steps in post if the current Pipeline’s or stage’s run is successful and the previous run failed or was unstable.
  4. regression
    Only run the steps in post if the current Pipeline’s or stage’s run’s status is failure, unstable, or aborted and the previous run was successful.
  5. aborted
    Only run the steps in post if the current Pipeline’s or stage’s run has an “aborted” status, usually due to the Pipeline being manually aborted. This is typically denoted by gray in the web UI.
  6. failure
    Only run the steps in post if the current Pipeline’s or stage’s run has a “failed” status, typically denoted by red in the web UI.
  7. success
    Only run the steps in post if the current Pipeline’s or stage’s run has a “success” status, typically denoted by blue or green in the web UI.
  8. unstable
    Only run the steps in post if the current Pipeline’s or stage’s run has an “unstable” status, usually caused by test failures, code violations, etc. This is typically denoted by yellow in the web UI.
  9. unsuccessful
    Only run the steps in post if the current Pipeline’s or stage’s run has not a “success” status. This is typically denoted in the web UI depending on the status previously mentioned.
  10. cleanup
    Run the steps in this post condition after every other post condition has been evaluated, regardless of the Pipeline or stage’s status.

stages

Containing a sequence of one or more stage directives, the stages section is where the bulk of the “work” described by a Pipeline will be located.
At a minimum, it is recommended that stages contain at least one stage directive for each discrete part of the continuous delivery process, such as Build, Test, and Deploy.
阶段部分包含一系列一个或多个阶段指令,是流水线描述的大部分“工作”所在的位置。 对于持续交付过程的每个离散部分,建议阶段至少包含一个阶段指令,例如构建、测试和部署。
这个和 angent,post, environment 等都是并列的,在 pipeline 代码块内

1
2
3
4
5
6
Required     Yes, 是否必须有,是

Parameters 没有参数

Allowed Only once, inside the pipeline block.只能在顶层 pipeline 代码块里用

1
2
3
4
5
6
7
8
9
10
11
12
13
pipeline {
agent any
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
post {

}
}

stage

The stage directive goes in the stages section and should contain a steps section, an optional agent section, or other stage-specific directives.
Practically speaking, all of the real work done by a Pipeline will be wrapped in one or more stage directives.
stage 指令位于 stage 部分,应该包含一个 steps 部分、一个可选的 agent 部分或其他特定于 stage 的指令。 实际上,流水线完成的所有实际工作都将包含在一个或多个阶段指令中。

1
2
3
4
5
6
Required    At least one 至少有一个

Parameters 一个强制参数,stage名称的字符串。

Allowed Inside the stages section. 在 stages 代码块中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
pipeline {
agent any
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
}

pipeline {
agent any
stages {
stage('Example Build') { //像这个 stage 在 stages 下面,可以出现多个的
steps {
echo 'Hello World'
}
}
stage('Example Deploy') {
steps {
echo 'Deploying'
}
}
}
}

steps

The steps section defines a series of one or more steps to be executed in a given stage directive.
steps 部分定义了在给定阶段指令中要执行的 一个 或 多个 步骤。

1
2
3
4
5
6
Required     Yes, 是否必须有,是

Parameters None 没有参数

Allowed Inside each stage block. 只能用在 stage 代码块中

1
2
3
4
5
6
7
8
9
10
11
12
pipeline {
agent any
stages {
stage('Example') {
steps {
echo 'Hello World' // 像这样的 可以在 steps 中出现多个的
sh 'xxxxx'
junit 'xxxx'
}
}
}
}

Sequential Stages 连续阶段

Stages in Declarative Pipeline may have a stages section containing a list of nested stages to be run in sequential order.
Note that a stage must have one and only one of steps, stages, parallel, or matrix. It is not possible to nest a parallel or
matrix block within a stage directive if that stage directive is nested within a parallel or matrix block itself. However,
a stage directive within a parallel or matrix block can use all other functionality of a stage, including agent, tools, when, etc.
声明式管道中的 有一个 stages 部分,其中包含要 按顺序运行的嵌套 stages列表。 请注意,一个stage 必须具有且只有一个steps、stages、parallel 或 matrix。 这里说白了就是 嵌套 stages 怎么书写

如果阶段指令嵌套在并行或矩阵块本身内,则不可能在阶段指令内嵌套并行或矩阵块。 但是,并行或矩阵块中的阶段指令可以使用阶段的所有其他功能,包括代理、工具、何时等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
pipeline {
agent none
stages {
stage('Non-Sequential Stage') {
agent {
label 'for-non-sequential'
}
steps {
echo "On Non-Sequential Stage"
}
}

stage('Sequential') { // 嵌套了
agent {
label 'for-sequential'
}
environment {
FOR_SEQUENTIAL = "some-value"
}

stages {
stage('In Sequential 1') {
steps {
echo "In Sequential 1"
echo "In Sequential 1"
}
}

stage('In Sequential 2') {
steps {
echo "In Sequential 2"
echo "In Sequential 2"
}
}

stage('Parallel In Sequential') {
parallel {
stage('In Parallel 1') {
steps {
echo "In Parallel 1"
}
}
stage('In Parallel 2') {
steps {
echo "In Parallel 2"
}
}
}
}
}// 嵌套在内容的一个 stages 然后里面又是一个一个的 stage、然后stage 里面又是一个 steps
} //end stage('Sequential') { // 嵌套了


}// end stages
}

Parallel 并行阶段

Stages in Declarative Pipeline may have a parallel section containing a list of nested stages to be run in parallel.
Note that a stage must have one and only one of steps, stages, parallel, or matrix. It is not possible to nest a
parallel or matrix block within a stage directive if that stage directive is nested within a parallel or matrix block itself.
However, a stage directive within a parallel or matrix block can use all other functionality of a stage, including agent, tools, when, etc.
In addition, you can force your parallel stages to all be aborted when any one of them fails, by adding failFast true to the
stage containing the parallel. Another option for adding failfast is adding an option to the pipeline definition: parallelsAlwaysFailFast()
声明式管道中的阶段可能有一个并行部分,其中包含要并行运行的嵌套阶段列表。 请注意,一个stage必须具有且只有一个steps、stages、parallel或matrix。
如果阶段指令嵌套在并行或矩阵块本身内,则不可能在阶段指令内嵌套并行或矩阵块。 但是,并行或矩阵块中的阶段指令可以使用阶段的所有其他功能,包括代理、工具、何时等。
此外,您可以通过将 failFast true 添加到包含并行的阶段来强制您的并行阶段在其中任何一个失败时中止。
添加 failfast 的另一个选项是在管道定义中添加一个选项:parallelsAlwaysFailFast()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
pipeline {
agent any
stages {
stage('Non-Parallel Stage') {
steps {
echo 'This stage will be executed first.'
}
}//end stage('Non-Parallel Stage')

stage('Parallel Stage') {
when {
branch 'master'
}

failFast true

parallel { // 这里是并行, 这个 Sequential Stages 连续阶段 中那个例子里面的 和 stages 并列的
stage('Branch A') {
agent {
label "for-branch-a"
}
steps {
echo "On Branch A"
}
}

stage('Branch B') {
agent {
label "for-branch-b"
}
steps {
echo "On Branch B"
}
}

stage('Branch C') {
agent {
label "for-branch-c"
}
stages {
stage('Nested 1') {
steps {
echo "In stage Nested 1 within Branch C"
}
}
stage('Nested 2') {
steps {
echo "In stage Nested 2 within Branch C"
}
}
}
}//end stage('Branch C')

}//end parallel
}//end stage('Parallel Stage')


}//end stages 最外层的那个 stages
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

pipeline {
agent any
options {
parallelsAlwaysFailFast()
}
stages {
stage('Non-Parallel Stage') {
steps {
echo 'This stage will be executed first.'
}
}

stage('Parallel Stage') {
when {
branch 'master'
}
parallel {
stage('Branch A') {
agent {
label "for-branch-a"
}
steps {
echo "On Branch A"
}
}
stage('Branch B') {
agent {
label "for-branch-b"
}
steps {
echo "On Branch B"
}
}
stage('Branch C') {
agent {
label "for-branch-c"
}
stages {
stage('Nested 1') {
steps {
echo "In stage Nested 1 within Branch C"
}
}
stage('Nested 2') {
steps {
echo "In stage Nested 2 within Branch C"
}
}
}
}//end stage('Branch C')
}//end parallel
}//end stage('Parallel Stage')

}//end stages 最外层的那个 stages
}

Matrix 矩阵

Stages in Declarative Pipeline may have a matrix section defining a multi-dimensional matrix of name-value combinations to be run in parallel.
We’ll refer these combinations as “cells” in a matrix.
Each cell in a matrix can include one or more stages to be run sequentially using the configuration for that cell.
Note that a stage must have one and only one of steps, stages, parallel, or matrix.
It is not possible to nest a parallel or matrix block within a stage directive if that stage directive is nested within a parallel or matrix block itself.

However, a stage directive within a parallel or matrix block can use all other functionality of a stage, including agent, tools, when, etc.
In addition, you can force your matrix cells to all be aborted when any one of them fails, by adding failFast true to the stage containing the matrix.
Another option for adding failfast is adding an option to the pipeline definition: parallelsAlwaysFailFast()
The matrix section must include an axes section and a stages section.
The axes section defines the values for each axis in the matrix.
The stages section defines a list of stages to run sequentially in each cell.
A matrix may have an excludes section to remove invalid cells from the matrix.
Many of the directives available on stage, including agent, tools, when, etc., can also be added to matrix to control the behavior of each cell.

声明式管道中的阶段可以有一个 矩阵matrix 部分,定义了要并行运行的 名称-值 组合的多维矩阵。我们将这些组合称为矩阵中的 单元cell 。
矩阵中的每个 单元 可以包括 一个或多个 阶段stages,以使用该单元的配置顺序运行。请注意,一个 阶段stage 必须具有且只有一个 步骤steps、阶段stages、并行parallel 或 矩阵matrix。
如果阶段指令嵌套在并行或矩阵块本身内,则不能在阶段指令内嵌套并行或矩阵块。

但是,matrix 或 parallel 块中的 阶段指令可以使用阶段的所有其他功能,包括agent、tools、when等。
此外,您可以通过将 failFast true 添加到包含矩阵的阶段来强制矩阵单元在其中任何一个失败时中止。
添加 failfast 的另一个选项是在管道定义中添加一个option:parallelsAlwaysFailFast()
矩阵部分必须包括 axes 部分和 stages 部分。axes 部分定义矩阵中每个轴的值。stages 部分定义了在每个单元中按顺序运行的阶段列表。
矩阵可以有一个 excludes 部分以从矩阵中删除无效单元。stage上可用的许多指令,包括agent、tools、when等,也可以添加到矩阵中以控制每个单元格的行为.

  1. axes 矩阵坐标
    轴部分指定一个或多个轴指令。 每个轴都包含一个名称和一个值列表。 每个轴的所有值都与其他值组合以生成单元格。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
matrix {
axes {
axis {
name 'PLATFORM'
values 'linux', 'mac', 'windows'
}
}
// ... 1x3 矩阵组合
}


matrix {
axes {
axis {
name 'PLATFORM'
values 'linux', 'mac', 'windows'
}
axis {
name 'BROWSER'
values 'chrome', 'edge', 'firefox', 'safari'
}
}
// ... 3x4 矩阵组合
}

matrix {
axes {
axis {
name 'PLATFORM'
values 'linux', 'mac', 'windows'
}
axis {
name 'BROWSER'
values 'chrome', 'edge', 'firefox', 'safari'
}
axis {
name 'ARCHITECTURE'
values '32-bit', '64-bit'
}
}
// ... 3x4 矩阵组合
}
  1. stages 矩阵中使用的stages
    stages 部分指定在每个单元中顺序执行的一个或多个阶段。 axes定义坐标组合,stages定义执行阶段

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    matrix {
    axes {
    axis {
    name 'PLATFORM'
    values 'linux', 'mac', 'windows'
    }
    }
    stages {
    stage('build') {
    // ...
    }
    stage('test') {
    // ...
    }
    stage('deploy') {
    // ...
    }
    }
    }

    matrix {
    axes {
    axis {
    name 'PLATFORM'
    values 'linux', 'mac', 'windows'
    }
    axis {
    name 'BROWSER'
    values 'chrome', 'edge', 'firefox', 'safari'
    }
    }
    stages {
    stage('build-and-test') {
    // ...
    }
    stage('deploy-and-test') {
    // ...
    }
    }
    }
  2. excludes (optional) 排除某些坐标组合,坐标组合很多,可以排除不合理的
    The optional excludes section lets authors specify one or more exclude filter expressions that select cells to be excluded from the
    expanded set of matrix cells (aka, sparsening). Filters are constructed using a basic directive structure of one or more of exclude
    axis directives each with a name and values list.
    The axis directives inside an exclude generate a set of combinations (similar to generating the matrix cells). The matrix cells that match
    all the values from an exclude combination are removed from the matrix. If more than one exclude directive is supplied,
    each is evaluated separately to remove cells.
    When dealing with a long list of values to exclude, exclude axis directives can use notValues instead of values.
    These will exclude cells that do not match one of the values passed to notValues.
    可选的 excludes 部分允许作者指定一个或多个排除过滤表达式,这些表达式选择要从 矩阵组合(又名稀疏)中排除 单元格。
    过滤器是使用一个或多个 排除轴指令 结构构建的,每个排除轴指令都有一个名称和值列表。
    exclude 中的轴指令生成一组组合(类似于生成矩阵单元)。 从矩阵中删除与排除组合中的所有值匹配的矩阵单元。 如果提供了多个排除指令,则分别评估每个指令以删除单元格。
    在处理要排除的一长串值时,排除轴指令可以使用 notValues 代替值。 这些将排除与传递给 notValues 的值之一不匹配的单元格。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
matrix {
axes {
axis {
name 'PLATFORM'
values 'linux', 'mac', 'windows'
}
axis {
name 'BROWSER'
values 'chrome', 'edge', 'firefox', 'safari'
}
axis {
name 'ARCHITECTURE'
values '32-bit', '64-bit'
}
} //矩阵组合一共有 3x4x2 = 24 组
excludes {
exclude {
axis {
name 'PLATFORM'
values 'mac'
}
axis {
name 'ARCHITECTURE'
values '32-bit'
}
}
}
// ...排除 了 1x1x4 = 4 组 mac-32-bit 这个,BROWSER不关心,都会排除掉, mac_{chrome,edge,firefox,safari}_32-bit 这4个都会排除掉的
}


matrix {
axes {
axis {
name 'PLATFORM'
values 'linux', 'mac', 'windows'
}
axis {
name 'BROWSER'
values 'chrome', 'edge', 'firefox', 'safari'
}
axis {
name 'ARCHITECTURE'
values '32-bit', '64-bit'
}
} //矩阵组合一共有 3x4x2 = 24 组
excludes {
exclude {
// 4 cells
axis {
name 'PLATFORM'
values 'mac'
}
axis {
name 'ARCHITECTURE'
values '32-bit'
}
} // 这个是 能匹配上 mac_{chrome,edge,firefox,safari}_32-bit 这4个
exclude {
// 2 cells
axis {
name 'PLATFORM'
values 'linux'
}
axis {
name 'BROWSER'
values 'safari'
}
} // 这个是 能匹配上 linux_safari_{32-bit, 64-bit} 这2个
exclude {
// 3 more cells and '32-bit, mac' (already excluded)
axis {
name 'PLATFORM'
notValues 'windows'
}
axis {
name 'BROWSER'
values 'edge'
}
} // windows_edge_{32-bit, 64-bit}
}
// ...
}
  1. Matrix cell-level directives (optional)

Matrix 允许用户通过在矩阵本身下添加阶段级指令来有效地配置每个单元的整体环境。 这些指令的行为与它们在舞台上的行为相同,但它们也可以接受矩阵为每个单元格提供的值。

轴和排除指令定义构成矩阵的静态单元格集。 这组组合是在管道运行开始之前生成的。 另一方面,“per-cell”指令在运行时进行评估。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
pipeline {
parameters {
choice(name: 'PLATFORM_FILTER', choices: ['all', 'linux', 'windows', 'mac'], description: 'Run on specific platform')
}
agent none
stages {
stage('BuildAndTest') {
matrix {
agent {
label "${PLATFORM}-agent"
}
when { anyOf {
expression { params.PLATFORM_FILTER == 'all' }
expression { params.PLATFORM_FILTER == env.PLATFORM }
} }
axes {
axis {
name 'PLATFORM'
values 'linux', 'windows', 'mac'
}
axis {
name 'BROWSER'
values 'firefox', 'chrome', 'safari', 'edge'
}
}
excludes {
exclude {
axis {
name 'PLATFORM'
values 'linux'
}
axis {
name 'BROWSER'
values 'safari'
}
}
exclude {
axis {
name 'PLATFORM'
notValues 'windows'
}
axis {
name 'BROWSER'
values 'edge'
}
}
}
stages {
stage('Build') {
steps {
echo "Do Build for ${PLATFORM} - ${BROWSER}"
}
}
stage('Test') {
steps {
echo "Do Test for ${PLATFORM} - ${BROWSER}"
}
}
}
}
}
}
}

script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
pipeline {
agent any
stages {
stage('Example') {
steps {
echo 'Hello World'

script {
def browsers = ['chrome', 'firefox']
for (int i = 0; i < browsers.size(); ++i) {
echo "Testing the ${browsers[i]} browser"
}
}
}
}
}
}