jenkins - 从 Jenkins 中的参数创建环境变量

标签 jenkins groovy jenkins-pipeline

我有一个参数化的 jenkinsfile。根据输入参数我想设置某些环境变量。但我无法获得正确的语法。

parameters {
    choice choices: ['insure-base-docker/insure-base', 'insure-ide/insure-sd', 'insure-ide/insure-ansible','insure-ide/ansible-test-vini'], description: 'Auf welche repository sollte die Tag erstellt?', name: 'repository'
    choice choices: ['tag', 'branch'], description: 'Tag oder branch erstellen', name: 'git_entity'
    string defaultValue: '21.x.x', description: 'Version die als branch oder Tag ersellt werden muss', name: 'version', trim: false
}
environment {
    GIT_URL = "${'https://my_repo/scm/'+param.repository+'.git'}"
    GIT_BRANCH = "${'Release/'+param.version}"
    CHECKOUT_BRANCH = '${${git_entity} == "tag" ? "master" : "develop"}'      
}

环境变量总是错误的。如何正确设置环境变量?

最佳答案

现在,Jenkins 中的参数和环境变量之间没有太大区别。甚至您使用它们的方式(前面带有 env. 关键字)也是相同的。

尝试这样的事情。

pipeline {
    parameters {
      choice choices: ['insure-base-docker/insure-base', 'insure-ide/insure-sd', 'insure-ide/insure-ansible','insure-ide/ansible-test-vini'], description: 'Auf welche repository sollte die Tag erstellt?', name: 'GIT_PROJECT'
      string defaultValue: '21.x.x', description: 'Version die als branch oder Tag ersellt werden muss', name: 'GIT_BRANCH', trim: false
    }

    agent any

    stages {
        stage('Cloning Git repository') {
            steps {
                script {
                    git branch: "${env.GIT_BRANCH}", credentialsId: 'MY_GIT_CREDENTIALS_PREVIOUSLY_ADDED_TO_JENKINS', url: "http://github.com/user/${env.GIT_PROJECT}.git"
                }
            }
        }
    }
}

您不仅可以将分支用作GIT_BRANCH,还可以将标签用作GIT_BRANCH

致以诚挚的问候。

关于jenkins - 从 Jenkins 中的参数创建环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66873465/

相关文章:

java - Jenkins 不重构文件夹名称,SonarQue 给出响应

Jenkins:可以将注释添加到 Jenkinsfile 中吗?

html - 在 Groovy 1.7 中使用包含混合内容的 HTML 构建器的正确语法是什么?

java - 将 BufferedInputStream 传递给接受 InputStream 的方法

linux - Jenkins 切换用户并执行命令

jenkins - 如何获取多分支管道作业的所有分支/作业?

jenkins - 如何在 jenkins 上并行运行动态阶段,并为每个阶段使用单独的 kubernetes 代理

Jenkins 使用 groovy 更新生成器 shell 命令

jenkins 当 gitlab 发生更改时如何触发构建

grails - 如何传递列表值以使分页正确呈现?