Search code examples
jenkinsjenkins-pipelinejenkins-workflowmultibranch-pipeline

Jenkins pipeline multibranch issue reusing node type after parallel jobs


I'm attempting to migrate our CI process from JobDSL to a multibranch pipelines setup. As a first step I've decided to just have the pipeline delegate back to the existing jobs (passing the required params) My pipeline looks like below (pseudo code)

stage('setup')
node('cotroller') {
  ...
}


stage('test') {
  parallel {
    'web' : {build job 'web-test' ..params...},
    'API' : {build job 'api-test' ..params...}
  }
}

stage('build') { 
  parallel {
    'web' : {build job 'web-build' ..params...},
    'API' : {build job 'api-build' ..params...}
  }
}

stage('publish') {
  node('controller'){
    sh './gradlew publishArtifacts'
  }
}

However Im getting issues with the last 'publish' stage. When it kicks off the gradle target it correctly reused the workspace from the 'setup' phase, but seems to execute in a 'durable' sub folder from the original checkout (i.e the past in the setup phase execute in /mnt/jenkins/workspace/<branchname>/<random_hash>/ however the last gradle target executes in a folder such as /mnt/jenkins/workspace/<branchname>/<random_hash>@tmp/durable-<hash>/script.sh) This is resulting in a gradlew not found error

I've tried playing around using the directory('/...'){...} but that doesnt seem to have resolved the issue...any help or guidance would be much appreciated!


Solution

  • Saving setup path

    You can try to save working directory from setup stage, e.g.:

    stage('setup')
    node('cotroller') {
      def setupPath = pwd()
      ...
    }
    
    
    stage('test') {
      parallel {
        'web' : {build job 'web-test' ..params...},
        'API' : {build job 'api-test' ..params...}
      }
    }
    
    stage('build') { 
      parallel {
        'web' : {build job 'web-build' ..params...},
        'API' : {build job 'api-build' ..params...}
      }
    }
    
    stage('publish') {
      node('controller'){
        dir("${setupPath}") {
          sh './gradlew publishArtifacts'
        }
      }
    }
    

    Using Global Tool Configuration

    The recommanded approach, according to Jenkins pipeliens tutorial, is to configure Gradle install path in the Jenkin's Global Tool configuration, name it anything (e.g. "Gradle") and then use it in your pipeline like this :

    ...
    
    stage('publish') {
      node('controller'){
        def gradleHome = tool 'Gradle'        
        sh "${gradleHome}/bin/gradlew publishArtifacts'
      }
    }