有没有办法在ExtendedChoice参数中使用管道环境变量?

时间:2019-06-12 12:35:25

标签: jenkins groovy jenkins-pipeline jenkins-plugins

我正在为我的Jenkins管道使用extendedChoice插件。它从存储桶中获取s3对象,并使用简短的Groovy脚本提供值列表。问题是我需要使用管道的“环境”部分中定义的相应变量来对s3存储桶进行参数化。我该怎么办?

所以我尝试了很多不同的代码片段来获取env var,但是没有结果。

import jenkins.model.*

// This will print out the requested var from the global Jenkins config.

def envVars = Jenkins.instance.getGlobalNodeProperties()[0].getEnvVars()
return envVars['S3_BUCKET']

// This will print out values from the env vars of the node itself where the Jenkins is running.

def env = System.getenv('S3_BUCKET')
return env

// This is what I have now
def domainsList = "aws s3api list-objects-v2 --bucket someRandomBucket --output text --delimiter /".execute() | 'cut -d / -f 1'.execute() | 'sed 1d'.execute()
domainsList.waitFor()

def output = domainsList.in.text
return output.split('COMMONPREFIXES')
// This is the Jenkinsfile
pipeline {
    agent any
    environment {
    DOMAIN_NAME = "${params.DOMAIN_NAME}"
    MODEL_VERSION = "${params.MODEL_VERSION}"
    S3_BUCKET = "someRandomBucket"
    }
    parameters {
      extendedChoice(
        bindings: '',
        defaultValue: '',
        description: '',
        descriptionPropertyValue: '',
        groovyClasspath: '',
        groovyScript: '''
        def domainsList = "aws s3api list-objects-v2 --bucket someRandomBucket --output text --delimiter /".execute() | 'cut -d / -f 1'.execute() | 'sed 1d'.execute()
        domainsList.waitFor()
        def output = domainsList.in.text
        return output.split('COMMONPREFIXES')
        ''',
        multiSelectDelimiter: ',',
        name: 'DOMAIN_NAME',
        quoteValue: false,
        saveJSONParameterToFile: false,
        type: 'PT_SINGLE_SELECT',
        visibleItemCount: 10)
      choice(
        choices: ['a', 'b'],
        description: 'Select a model version for processing',
        name: 'MODEL_VERSION')
    }
    stages {
        stage('Clean workdir') {
            steps {
                cleanWs()
            }
        }
        stage('build') {
            steps {
                sh "echo $S3_BUCKET"
                sh "echo $DOMAIN_NAME"
                sh "echo $MODEL_VERSION"
            }
        }
    }
}

如上所述,我需要在groovy脚本的extendedChoice参数中用S3_BUCKET env var值替换someRandomBucket硬代码

已解决-可以通过Jenkins作业UI专门为参数注入环境变量

0 个答案:

没有答案