JenkinsScriptedPipeline

nicolaw 18th July 2018 at 3:18pm
CodeSnippets Groovy Jenkins
node() {
  echo "Hello world!"
  if ("${sourceBranch}" ==~ /^(myprefix\/)?(epic|story|task)\/.+/) {
    echo "sourceBranch = >${sourceBranch}<"
  }
}
// Dynamic pipeline stages
def tests = params.Tests.split(',')
for (int i = 0; i < tests.length; i++) {
    stage("Test ${testd[i]}") {
        sh '....'
    }
}
// Dynamic pipeline stages per leaf directory
def leaves = sh(script: 'find . -not -path "*/.git/*" -type d -links 2', returnStdout: true).trim().split("\n")
leaves.each{ dir ->
  dir = dir.substring(2)
  stage("Stage ${dir}") {
    echo "${dir}"
  }
}
def transformDeployBuildStep(OS) {
    return {
        node ('master') { 
        wrap([$class: 'TimestamperBuildWrapper']) {
...
        } } // ts / node
    } // closure
} // transformDeployBuildStep

stage("Yum Deploy") {
  stepsForParallel = [:]
  for (int i = 0; i < TargetOSs.size(); i++) {
      def s = TargetOSs.get(i)
      def stepName = "CentOS ${s} Deployment"
      stepsForParallel[stepName] = transformDeployBuildStep(s)
  }
  stepsForParallel['failFast'] = false
  parallel stepsForParallel
} // stage
// https://stackoverflow.com/questions/46834998/scripted-jenkinsfile-parallel-stage
// https://www.cloudbees.com/blog/parallelism-and-distributed-builds-jenkins

node('ecs') {
  stage('Checkout sources') {
    git branch: 'master', credentialsId: 'id_rsa-jenkins', url: 'git@bitbucket.org:foo/bar.git'
    sh "ls -al"
  }
  parallel (
    phase1: {
      stage('Parallel phase 1') {
        echo "phase 1"
        sleep 10
        echo "phase 1"
      }
    },
    //sh "echo p1; sleep 10s; echo phase1" },
    phase2: {
      stage('Parallel phase 2') {
        echo "phase 2"
        sleep 20
        echo "phase 2"
      }
      //sh "echo p2; sleep 20s; echo phase2" }
    }
  )
  stage('Final thingie cleanup') {
    sh "ps aux"   
  }
}
myStringVar.contains(<someOtherValue>)
myStringVar.equals(<someOtherValue>)
myStringVar.endsWith(<suffix>)
myStringVar.startsWith(<prefix>)
myStringVar.equalsIgnoreCase(<someOtherValue>)
myStringVar.isEmpty()
myStringVar.matches(<someOtherValue>)
myStringVar.replace(<oldVal>,<newVal>)
myStringVar.replaceAll(<oldValRegex>,<newVal>)
myStringVar.split(<regexPattern>)