简体   繁体   English

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

[英]Use Jenkins WORKSPACE environment variable in Jenkins declarative pipeline parameter

Is there a way to use Jenkins WORKSPACE environment variable in Jenkins declarative pipeline parameters?有没有办法在 Jenkins 声明性管道参数中使用 Jenkins WORKSPACE 环境变量?

Below attempt failed.以下尝试失败。

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' didn't work either. propertyFile: '${WORKSPACE}/templates.properties'也不起作用。

The environment variable can be accessed in various place in Jenkinsfile like:可以在 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
                }
            }
        }
    }
}

The only way that worked is putting absolute path to Jenkins master workspace where properties file is located.唯一可行的方法是将绝对路径放置到属性文件所在的 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 { 
   ...
   }

It seems that environment variables are not available during pipeline parameters definition before the pipeline actually triggered.在管道实际触发之前,在管道参数定义期间似乎环境变量不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Jenkins声明性管道的代理部分中使用环境变量? - How to use an environment variable in the agent section of a Jenkins Declarative Pipeline? 在Jenkins管道的参数部分中使用环境变量 - Use environment variable in parameter section of Jenkins pipeline Jenkins 带有 boolean 环境变量的声明式管道表达式 - Jenkins declarative pipeline expression with boolean environment variable 如何使用Jenkins声明式管道修改环境变量 - How to modify environment variable with Jenkins declarative pipeline 在詹金斯声明性管道中具有插值的条件环境变量 - Conditional environment variable with interpolation in jenkins declarative pipeline 如何更改Jenkins Declarative Pipeline环境变量? - How to change a Jenkins Declarative Pipeline environment variable? 如何在 jenkins 声明式管道中使用文件参数? - How to use file parameter in jenkins declarative pipeline? 如何为詹金斯管道声明定义工作区体积 - How to define workspace volume for jenkins pipeline declarative 在Jenkins声明式管道中将git committer的名称设置为环境变量 - Setting git committer's name to environment variable in Jenkins declarative pipeline Jenkins 使用环境变量控制阶段的声明性管道问题 - Jenkins Declarative Pipeline Issues with using environment variable to control stage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM