17

I've been interested in converting our bespoke Jenkins integrations into a pipeline. However, I can't seem to figure out how to do it.

Can anyone help me with the Jenkins script that could do the following?

1---2---3-----------9---10
    |           |
    |---4-------|
    |           |
    |---5---6---|
        |       |
        |---7---|

1: Start pipeline
10: End pipeline
5: Build some files
   * needed by 6, 7,
   * needed as artifacts at the end
2, 3, 4, 6, 7: Have jUnit result files, should be available at end of
   test (somewhere), even if one failed

Is this even possible? Or should I just join after 3, 4, 5? Like this:

1---2---3-------6-------9---10
    |       |   |   |
    |---4---|   7---|
    |       |
    |---5---|
  • 1
    I'd vote for second option, sounds less likely to break and easier to extend if the need arise. – Tensibai Apr 21 '17 at 9:32
  • 2
    possibly being dense, but i haven't understood what the more complex version adds yet. can you document what "9" does and what it depends on? there is no 8, in case you want to revise and add that. :) – burnettk Apr 23 '17 at 14:41
  • 1
    @BertGoethals You can use Pipeline and still have different jobs. It's possible I'm missing something here. – avi Apr 26 '17 at 12:21
  • 1
    I was trying to do something similar a few days ago. I could get it to work with nested parallel commands, but the flow didn't look right in BlueOcean. – lawnmowerlatte May 4 '17 at 16:13
  • 1
    @BertGoethals Can't you start another pipeline doing 5 and then 6 and 7 from your main pipeline ? – Tensibai May 11 '17 at 12:05
9

Based on the comments to my question, and some basic testing the following seems to work:

4

I had a similar situation in which I wanted to nest other parallel jobs threads inside another parallel one. This code worked for me:

def performDeploymentStages(String node, String app) {
    stage("build") {
        echo "Building the app [${app}] on node [${node}]"
    }
    stage("deploy") {
        echo "Deploying the app ${app}] on node [${node}]"
    }
    stage("test") {
        echo "Testing the app [${app}] on node [${node}]"
    }
}

pipeline {
    agent {
        label 'master'
    }
    parameters {
        string(name: 'NODES', defaultValue: '1,2,3', description: 'Nodes to build, deploy and test')
        choice(name: 'ENV', choices: 'qa', description: 'Environment')
        string(name: 'APPS', defaultValue: 'app01,app02', description: 'App names')
    }

    stages {
        stage('parallel stage') {
            steps {
                script {
                    def nodes = [:]
                    for (node in params.NODES.tokenize(',')) {
                        def apps = [:]
                        for (app in params.APPS.tokenize(',')) {
                            performDeploymentStages(node, app)
                        }
                        parallel apps
                    }
                    parallel nodes
                }
            }
        }
    }
}

To fully benefit from parallel run remember to assign enough executors.

  • Does this snippet work without assigning values to the nodes and apps variables? – jellenberger Feb 8 at 5:17
  • @jellenberger it will not work if these vars will be empty (null/empty string). But it's fine to pass single items (single app, or node) without commas. – biniosuaf Feb 15 at 12:26

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.