如何防止矩阵构建在 Jenkins 管道中共享相同的工作区?

How to prevent matrix builds to share the same workspace in Jenkins pipelines?

我正在尝试使用 Jenkins 多分支管道来构建我的多平台代码。 我认为声明矩阵是实现 DRY 的好方法。

无论如何,似乎每个轴元素的构建都是在同一个检出文件夹上执行的:这会导致并发构建失败。如果我为单一平台构建一切正常。

我确定 100% 共享工作区,因为工作区文件夹最终包含 rootfs_A.tar 和 rootfs_B.tar。

我如何以 ./build.sh 在单独的工作区上工作的方式重做?

这是我实际使用的 Jenkinsfile。

pipeline {
    agent any
    stages {
        stage('Build') {
            matrix {
                axes {
                    axis {
                        name 'PLATFORM'
                        values 'A', 'B'
                    }
                }
                stages {
                    stage('Prep') {
                        steps {
                            sh "cp /opt/${PLATFORM}/rootfs.tar rootfs_${PLATFORM}.tar"
                        }
                    }
                    stage('Build') {
                        steps {
                            echo "Do Build for ${PLATFORM}"
                            sh "./build.sh -p ${PLATFORM}
                        }
                    }
                }
            }
        }
    }
}

您可以使用 customWorkspace 代理选项配置矩阵管道,以 运行 单独工作区中的每个维度。考虑以下示例:

pipeline {
    agent none

    stages {
        stage("Build") {
            matrix {
                agent {
                    node {
                        label ""
                        customWorkspace "workspace/${JOB_NAME}/PLATFORM/${PLATFORM}/" 
                    }
                }
                axes {
                    axis {
                        name "PLATFORM"
                        values "A", "B"                        
                    }
                }
                stages {
                    stage("Prepare") {
                        steps {
                            sh "pwd"
                            sh "ls -lah"
                        }
                    }
                }
            }
        }
    }
}

关键部分是 matrix 块中的代理配置。在这里,我使用带有空标签的 node 代理(相当于您示例中的 agent any),并在 JENKINS_HOME 相对路径中设置自定义工作区。此配置 运行 包含单独工作区内的每个维度(包括将存储库克隆到每个自定义工作区。)

您可以在下面找到说明此声明性管道执行流程的示例性输出。

[Pipeline] Start of Pipeline
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] parallel
[Pipeline] { (Branch: Matrix - PLATFORM = 'A')
[Pipeline] { (Branch: Matrix - PLATFORM = 'B')
[Pipeline] stage
[Pipeline] { (Matrix - PLATFORM = 'A')
[Pipeline] stage
[Pipeline] { (Matrix - PLATFORM = 'B')
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] node
Running on Jenkins in /home/wololock/.jenkins/workspace/jenkins-matrix-workspaces_master
[Pipeline] node
[Pipeline] {
[Pipeline] ws
Running in /home/wololock/.jenkins/workspace/jenkins-matrix-workspaces/master/PLATFORM/A
[Pipeline] {
[Pipeline] checkout
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
No credentials specified
Cloning the remote Git repository
Cloning with configured refspecs honoured and without tags
Cloning repository file:///home/wololock/workspace/jenkins-matrix-workspaces
 > git init /home/wololock/.jenkins/workspace/jenkins-matrix-workspaces/master/PLATFORM/A # timeout=10
Fetching upstream changes from file:///home/wololock/workspace/jenkins-matrix-workspaces
 > git --version # timeout=10
 > git --version # 'git version 2.26.3'
 > git fetch --no-tags --force --progress -- file:///home/wololock/workspace/jenkins-matrix-workspaces +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url file:///home/wololock/workspace/jenkins-matrix-workspaces # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
Checking out Revision c6083445649b0c73b70aefcb1bc662c30577bd00 (master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f c6083445649b0c73b70aefcb1bc662c30577bd00 # timeout=10
Commit message: "update"
 > git rev-list --no-walk 8d67e2047ad7a0e65899f9855ae289eda90ea974 # timeout=10
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Prepare)
[Pipeline] sh
+ pwd
/home/wololock/.jenkins/workspace/jenkins-matrix-workspaces/master/PLATFORM/A
[Pipeline] sh
+ ls -lah
total 20K
drwxrwxr-x 3 wololock wololock 4.0K Apr 21 12:17 .
drwxrwxr-x 4 wololock wololock 4.0K Apr 21 12:17 ..
-rw-rw-r-- 1 wololock wololock   11 Apr 21 12:17 file
drwxrwxr-x 8 wololock wololock 4.0K Apr 21 12:17 .git
-rw-rw-r-- 1 wololock wololock  762 Apr 21 12:17 Jenkinsfile
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // ws
[Pipeline] }
Running on Jenkins in /home/wololock/.jenkins/workspace/jenkins-matrix-workspaces_master
[Pipeline] // node
[Pipeline] {
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] ws
Running in /home/wololock/.jenkins/workspace/jenkins-matrix-workspaces/master/PLATFORM/B
[Pipeline] {
[Pipeline] // stage
[Pipeline] }
[Pipeline] checkout
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
No credentials specified
Cloning the remote Git repository
Cloning with configured refspecs honoured and without tags
Cloning repository file:///home/wololock/workspace/jenkins-matrix-workspaces
 > git init /home/wololock/.jenkins/workspace/jenkins-matrix-workspaces/master/PLATFORM/B # timeout=10
Fetching upstream changes from file:///home/wololock/workspace/jenkins-matrix-workspaces
 > git --version # timeout=10
 > git --version # 'git version 2.26.3'
 > git fetch --no-tags --force --progress -- file:///home/wololock/workspace/jenkins-matrix-workspaces +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url file:///home/wololock/workspace/jenkins-matrix-workspaces # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
Checking out Revision c6083445649b0c73b70aefcb1bc662c30577bd00 (master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f c6083445649b0c73b70aefcb1bc662c30577bd00 # timeout=10
Commit message: "update"
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Prepare)
[Pipeline] sh
+ pwd
/home/wololock/.jenkins/workspace/jenkins-matrix-workspaces/master/PLATFORM/B
[Pipeline] sh
+ ls -lah
total 20K
drwxrwxr-x 3 wololock wololock 4.0K Apr 21 12:17 .
drwxrwxr-x 6 wololock wololock 4.0K Apr 21 12:17 ..
-rw-rw-r-- 1 wololock wololock   11 Apr 21 12:17 file
drwxrwxr-x 8 wololock wololock 4.0K Apr 21 12:17 .git
-rw-rw-r-- 1 wololock wololock  762 Apr 21 12:17 Jenkinsfile
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // ws
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS