简体   繁体   English

在Jenkins管道的参数部分中使用环境变量

[英]Use environment variable in parameter section of Jenkins pipeline

I have an environment variable which represents a gitrepo and I use this repo in my git parameter: 我有一个代表gitrepo的环境变量,我在git参数中使用了这个仓库:

pipeline {
    agent any

    options {
        buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '1'))
        disableConcurrentBuilds()
    }

    environment {
        GitRepo = 'https://my-repo/repo.git'
    }

    parameters {
        gitParameter branchFilter: 'origin/(.*support.*)', defaultValue: 'develop', name: 'SUPPORTBRANCH', type: 'PT_BRANCH', useRepository: env.GitRepo , sortMode: 'DESCENDING_SMART'
    }
...}

When I use env.GitRepo inside the stages of my pipeline it works, but not when I use it in a parameter section. 当我在管道的各个阶段中使用env.GitRepo时,它可以工作,但是当我在参数部分中使用它时,它却无法工作。

How can I make this work? 我该如何进行这项工作?

Does it really need to be an environment variable? 是否真的需要一个环境变量? If not a simple property could do the trick. 如果不是一个简单的属性就可以解决问题。 Of course you can still use it to define the environment variable if required... 当然,如果需要,您仍然可以使用它来定义环境变量...

I used a string parameter just because for me it seemed too difficult to get a simple example running using the gitParameter plugin. 我使用string参数只是因为对我来说,使用gitParameter插件运行一个简单的示例似乎太困难了。 But of course you can use the property also for the git parameter plugin. 但是,当然您也可以将该属性用于git参数插件。

// simply define the property here...
gitRepo = 'https://my-repo/repo.git'

pipeline {
    agent any

    environment {
        // optional, not used below. Use only if you need to have an environment variable
        GitRepo = gitRepo
    }

    options {
        buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '1'))
        disableConcurrentBuilds()
    }

    parameters {
        string defaultValue: gitRepo, description: '', name: 'Test', trim: false
    }

    stages {
        stage ('foo') {
            steps {
                echo 'Hi'
            }
        }
    }
}

声明:本站的技术帖子网页,遵循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 声明性管道参数中使用 Jenkins WORKSPACE 环境变量 - Use Jenkins WORKSPACE environment variable in Jenkins declarative pipeline parameter Jenkins Pipeline:根据参数设置环境变量 - Jenkins Pipeline: Set environment variable based on parameter Jenkins Pipeline:使用带有'('的Windows环境变量 - Jenkins Pipeline: use Windows environment variable with '(' in it Jenkins 管道代理使用环境变量 - Jenkins pipeline agent to use environment variable Jenkins 用于报告工具的阶段后期部分中的管道环境变量 - Jenkins pipeline environment variable in stage post section for reporting tools Jenkins管道中的环境变量 - Environment variable in Jenkins Pipeline 将环境变量作为管道参数传递给 Jenkins 共享库 - Passing environment variable as a pipeline parameter to Jenkins shared library 从构建参数设置 Jenkins 管道阶段的环境变量 - Setting environment variable in Jenkins pipeline stage from build parameter 如何在 jenkins 管道的触发器部分使用 env 变量? - How to use env variable inside triggers section in jenkins pipeline?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM