Skip to content

Instantly share code, notes, and snippets.

@josefsalyer
Forked from oifland/Jenkinsfile
Created February 22, 2022 15:33
Show Gist options
  • Save josefsalyer/f9599eafbc9692ceeb3eda1986eaece2 to your computer and use it in GitHub Desktop.
Save josefsalyer/f9599eafbc9692ceeb3eda1986eaece2 to your computer and use it in GitHub Desktop.

Revisions

  1. @oifland oifland revised this gist Feb 8, 2017. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions Jenkinsfile
    Original file line number Diff line number Diff line change
    @@ -26,14 +26,15 @@ def echo_all(list) {
    echo "Hello ${item}"
    }
    }
    // outputs all items as expected

    @NonCPS
    def loop_of_sh(list) {
    list.each { item ->
    sh "echo Hello ${item}"
    }
    }

    // outputs only the first item

    @NonCPS
    def loop_with_preceding_sh(list) {
    @@ -42,6 +43,7 @@ def loop_with_preceding_sh(list) {
    sh "echo Hello ${item}"
    }
    }
    // outputs only the "Going to echo a list" bit

    //No NonCPS required
    def traditional_int_for_loop(list) {
    @@ -50,4 +52,4 @@ def traditional_int_for_loop(list) {
    sh "echo Hello ${list[i]}"
    }
    }

    // echoes everything as expected
  2. @oifland oifland created this gist Feb 8, 2017.
    53 changes: 53 additions & 0 deletions Jenkinsfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    // Related to https://issues.jenkins-ci.org/browse/JENKINS-26481

    abcs = ['a', 'b', 'c']

    node('master') {
    stage('Test 1: loop of echo statements') {
    echo_all(abcs)
    }

    stage('Test 2: loop of sh commands') {
    loop_of_sh(abcs)
    }

    stage('Test 3: loop with preceding SH') {
    loop_with_preceding_sh(abcs)
    }

    stage('Test 4: traditional for loop') {
    traditional_int_for_loop(abcs)
    }
    }

    @NonCPS // has to be NonCPS or the build breaks on the call to .each
    def echo_all(list) {
    list.each { item ->
    echo "Hello ${item}"
    }
    }

    @NonCPS
    def loop_of_sh(list) {
    list.each { item ->
    sh "echo Hello ${item}"
    }
    }


    @NonCPS
    def loop_with_preceding_sh(list) {
    sh "echo Going to echo a list"
    list.each { item ->
    sh "echo Hello ${item}"
    }
    }

    //No NonCPS required
    def traditional_int_for_loop(list) {
    sh "echo Going to echo a list"
    for (int i = 0; i < list.size(); i++) {
    sh "echo Hello ${list[i]}"
    }
    }