在 Jenkins 声明性管道参数中使用 Jenkins WORKSPACE 环境变量

时间:2021-02-21 17:12:01

标签: jenkins environment-variables jenkins-pipeline jenkins-plugins extended-choice-parameter

有没有办法在 Jenkins 声明性管道参数中使用 Jenkins WORKSPACE 环境变量?

以下尝试失败。

pipeline {
    parameters {
        extendedChoice description: 'Template in project',
                        multiSelectDelimiter: ',', name: 'TEMPLATE',
                        propertyFile:  env.WORKSPACE + '/templates.properties', 
                        quoteValue: false, saveJSONParameterToFile: false, type: 'PT_MULTI_LEVEL_SINGLE_SELECT',
                        value: 'Project,Template', visibleItemCount: 6
   ...
   } 
   stages { 
   ...
   }

propertyFile: '${WORKSPACE}/templates.properties' 也没有用。

2 个答案:

答案 0 :(得分:0)

可以在 Jenkinsfile 中的不同位置访问环境变量,例如:

def workspace
node {
    workspace = env.WORKSPACE
}
pipeline {
    agent any;
    parameters {
        string(name: 'JENKINS_WORKSPACE', defaultValue: workspace, description: 'Jenkins WORKSPACE')
    }
    stages {
        stage('access env variable') {
            steps {
                // in groovy
                echo "${env.WORKSPACE}"
                
                //in shell
                sh 'echo $WORKSPACE'
                
                // in groovy script 
                script {
                    print env.WORKSPACE
                }
            }
        }
    }
}

答案 1 :(得分:0)

唯一有效的方法是将绝对路径放置到属性文件所在的 Jenkins 主工作区。

pipeline {
    parameters {
        extendedChoice description: 'Template in project',
                        multiSelectDelimiter: ',', name: 'TEMPLATE',
                        propertyFile:  'absolute_path_to_master_workspace/templates.properties', 
                        quoteValue: false, saveJSONParameterToFile: false, type: 'PT_MULTI_LEVEL_SINGLE_SELECT',
                        value: 'Project,Template', visibleItemCount: 6
   ...
   } 
   stages { 
   ...
   }

在管道实际触发之前,管道参数定义期间似乎环境变量不可用。