jenkinsparametersjenkins-pipelineparameter-passingjenkins-2

How to use build parameters in the pieline.agent.node block of a Jenkinsfile?


I'm developing a Jenkins pipeline for a PHP project. Since I want to run the most of the steps from the project's root directory, I set the customWorkspace:

pipeline {
    agent {
        node {
            label 'devvm-slave-01'
            customWorkspace '/path/to/my/project'
        }
    }
    stages {
        stage('build') {
            steps {
                sh 'pwd'
                ...
            }
        }
    }
}

It's working fine, but I don't like, that the path is hard-coded in the Jenkinsfile.

So I tried to solve this by using parameters:

enter image description here

The problem is: I haven't found a way to access parameters in the block pipeline.agent.node. I can read and handle them in the pipeline.stages.stage.steps sections. But not in the node block.

Is it possible / How to access Jenkins project parameters in the node section of the Jenkinsfile?


Solution

  • You can pass the parameter instead of hard-coded value for customWorkspace like this:

    pipeline {
        agent {
            node {
                label 'devvm-slave-01'
                customWorkspace PROJECT_ROOT
            }
        }
        stages {
            stage('build') {
                steps {
                    sh 'pwd'
                    ...
                }
            }
        }
    }