jenkins-pipelineshared-librariesjenkins-shared-libraries

Jenkins function of shared library executed on wrong node


I have a simple shared library and want to call a function which creates a file. My pipeline script looks like this:

@Library('jenkins-shared-library')_

pipeline {
    agent {
        node {
             label 'node1'
        }
    }
 
    stages {
        stage('test') {
            
            steps {
                script {
                    def host = sh(script: 'hostname', returnStdout: true).trim()
                    echo "Hostname is: ${host}"
                    
                    def testlib = new TestLibrary(script:this)
                    testlib.createFile("/tmp/file1")
                }
            }
        }
    }
}

This pipeline job is triggered by another job which runs on the built-in master node. The stage 'test' is correctly executed on the 'node1'.

Problem: The created file "/tmp/file1" is created on the jenkins master, instead of "node1"

I also tried it without the shared library and load a groovy script directly in a step:

pipeline {
    agent {
        node {
             label 'node1'
        }
    }
 
    stages {
        stage('test') {
            steps {
                script {
                    
                    def script = load "/path/to/script.groovy"
                    script.createFile("/tmp/file1")
                }
            }
        }
    }
}

This also creates the file on the master node, instead on "node1".

Is there no way of loading external libs or classes and execute them on the node where the stage is running on ? I dont want to place all the code directly into the step.


Solution

  • Ok I found it myself, groovy scripts run by definition always on the master node. No way to run scripts or shared-library code on a node other than master.