1

I have this test pipeline code, it runs 2 build on different nodes on build_servers as expected.

pipeline {
    agent { label 'myserver' }
    stages {
        stage('1') {
            steps {
                script {
                    def tests = [:]
                    for (f in [ 'x', 'y' ]) {
                        tests["${f}"] = {
                            node('build_servers') {
                                stage('Clean Workspace') {
                                     echo "cleaning up Ws ${f}"
                                     step([$class: 'WsCleanup'])
                                }
                                stage('create Wspace txt file') {
                                        script {
                                            props="Wspace=${workspace}"
                                            dir ( 'wspace') {
                                                    writeFile file: 'params.txt', text: props
                                            }
                                        }
                                }
                                stage('Run other job') {
                                    script {
                                        println('calling other project')
                                        props = readProperties file: "wspace/params.txt"
                                           file_params = props.collect {
                                                string(name: it.key, value: it.value)
                                        }
                                        build job: "other_job2", parameters: file_params, wait: true, propagate: true
                                    }
                                }
                            }
                        }
                    }
                    parallel tests
                }
            }
        }
    }
}

I expect it to trigger 2 different builds on other_job2, but it submits just one job and both tests['x'] and tests['y'] waits for the same job to finish, why is that? should it not create 2 different jobs in other_job2 project, one for x and one for y?

in the console output, I see build #5 for both [x] and [y] exactly as shown below:

[x] Starting building: TEST » other_job2 #5
[y] Starting building: TEST » other_job2 #5

Also, I am printing ${f} in echo "cleaning up WS... line, I expected to see both x and y, but I see only cleaning up Ws y printed for both [x] and [y] as below

[x] cleaning up Ws y
    [Pipeline] cleanWs
[x] [WS-CLEANUP] Deleting project workspace...
    [WS-CLEANUP] Deferred wipeout is used...
    [WS-CLEANUP] done
    [Pipeline] echo
[y] cleaning up Ws y
    [Pipeline] cleanWs
[y] [WS-CLEANUP] Deleting project workspace...
    [WS-CLEANUP] Deferred wipeout is used...
    [WS-CLEANUP] done
1

Something needs to be different in other_job2 for tests['x'] and tests['y'].

Example(untested),

for (f in [ 'x', 'y' ]) { {
    {some code}
    build job: "other_job2", parameters: [file_params, string(name:'dummy', value: "${f}")], wait: true, propagate: true
    // the dummy parameter is for preventing mutation
}

More info: https://www.jenkins.io/doc/pipeline/examples/#jobs-in-parallel

| improve this answer | |
  • thanks a lot, you pointed me the right link, can you edit your answer by pasting the dummy parameter is for preventing mutation... comment line, I added for (k in [ 'x', 'y' ]) { def f = k ... and it started working. – rodee May 5 at 20:28
  • I spent lots of time figuring this, you saved my time, but please add this info too to your answer, so it will be useful for others def index = i //if we tried to use i below, it would equal 4 in each job execution. which I see in the link you pasted. – rodee May 5 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.