Declarative Pipeline multiple agents Error

66 views
Skip to first unread message

Matthew Ceroni

unread,
Mar 8, 2018, 4:19:31 PM3/8/18
to Jenkins Users
I have the following example pipeline

#!/usr/bin/env groovy
@Library('shared-...@1.1.0') _

pipeline {
  agent {
    kubernetes {
      label buildId()
      containerTemplate {
        name 'node'
        image 'node:8.9.4-alpine'
        ttyEnabled true
        command 'cat'
      }
    }
  }
  stages {

    
    stage('Build') {
      steps {
        sh "echo npm run build"
        
        // stash build directory
        //stash includes: 'build/**', name: 'app'
      }
    }

    stage('Unit Test') {
      steps {
        script {
          try {
            sh "echo npm run test"
          } catch (err) {
            println "Failed"
          //} finally {
           // junit 'build/reports/**/*.xml'
          }
        }
      }
    }
    
    stage('Package') {
      agent { node { label 'docker1' } }
      options { skipDefaultCheckout() }
      steps {
        sh "/bin/sleep 120"
      }
    }
    
  }
}


What happens is that on the Package stage I get the following error:

[_location-service_PR-2-head-RP4JKIYE4QYJREC2YOQQGNKQG5UC56YLZET3VDC2IPW4C5FSH7EQ] Running shell script
sh: can't create /home/jenkins/workspace/_location-service_PR-2-head-RP4JKIYE4QYJREC2YOQQGNKQG5UC56YLZET3VDC2IPW4C5FSH7EQ@tmp/durable-de461dce/jenkins-log.txt: nonexistent directory

Since the node I am accessing in that step is a container I thought maybe it was a permissions issue. However I validated this wasn't the case by making that agent (node { label 'docker1' }} the top level agent and the pipeline runs fine. 

So my thought is that during the switch between agents/nodes it is expecting that directory to exist (it does create the workspace directory though, just not durable).

Any help would be appreciated.

Thanks

Matthew Ceroni

unread,
Mar 8, 2018, 4:28:04 PM3/8/18
to Jenkins Users
Ran another test that seems to validate somewhat of what I said above. Instead of having a global agent I specified an agent per stage (2 stages)

In this situation the first agent created the workspace, checked out the code and the durable- directory existed.

When it got to the second stage and connected to the docker container it created the workspace but not the durable- directory but at the same time didn't complain about it like the above error message.


#!/usr/bin/env groovy
@Library('shared-...@1.1.0') _

pipeline {
  agent none
  stages {

    stage('Build') {
      agent {
        kubernetes {
          label buildId()
          containerTemplate {
            name 'node'
            image 'node:8.9.4-alpine'
            ttyEnabled true
            command 'cat'
          }
        }
      }
      steps {
        sh "echo npm run build"
        sh "/bin/sleep 120"
        
        // stash build directory
        //stash includes: 'build/**', name: 'app'
      }
    }

Matthew Ceroni

unread,
Mar 8, 2018, 4:33:24 PM3/8/18
to Jenkins Users
Take back what I said about the durable directory, they exist in both cases in this situation. So the below works. But if I have a global agent and try to over-ride that at a stage level I get the error about durable doesn't exist. 
Reply all
Reply to author
Forward
0 new messages