首页 > 解决方案 > 如何在 Jenkinsfile 的 pieline.agent.node 块中使用构建参数?

问题描述

我正在为 PHP 项目开发 Jenkins 管道。由于我想从项目的根目录运行大部分步骤,因此我设置了customWorkspace

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

它工作正常,但我不喜欢路径是硬编码在Jenkinsfile.

所以我试图通过使用参数来解决这个问题:

在此处输入图像描述

问题是:我还没有找到访问 block 中参数的方法pipeline.agent.node。我可以在pipeline.stages.stage.steps章节中阅读和处理它们。但不在node街区内。

是否有可能/如何访问 Jenkinsnode部分中的项目参数Jenkinsfile

标签: jenkinsparametersjenkins-pipelineparameter-passingjenkins-2

解决方案


您可以像这样传递参数而不是硬编码值customWorkspace

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

推荐阅读