Jenkins 声明式:根据条件创建全局变量
Jenkins Declarative: Create global variables based from conditions
我想问一下,如果传递给 jenkins 的参数是“dev[,我该如何声明全局变量 USE“NON-PROD” =33=]" 或 "qa" 然后 "PROD" 如果传递给 jenkins 的参数是 "uat”或“分期”。这是条件的完整详细信息
IF ENV = dev OR qa
then
USE = NON-PROD
ELSE IF ENV = uat OR staging
then
USE = PROD
这是我的詹金斯设置
Jenkins 参数设置
到目前为止,这是我使用的测试声明脚本,但没有幸运地获得预期的响应。我还不确定 ELSE-IF 条件是否正常(对于 uat 或暂存)
def USE
pipeline {
agent any
stages {
stage('Declare Vars') {
when {
expression { params.ENV == 'qa' || params.ENV == 'dev' }
}
steps {
USE = "NON-PROD"
}
}
stage('Check Vars') {
steps {
sh """#!/bin/bash +x
echo USE = ${USE}
"""
}
}
}
}
这是我遇到的错误
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 12: Expected a step @ line 12, column 11.
USE = "NON-PROD"
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:428)
Finished: FAILURE
以下内容可行。一个简单的 if..else
条件即可。
pipeline {
agent any
parameters {
choice choices: ['UAT', 'STAG', 'DEV', 'QA'], description: '', name: 'chooseEnvironment'
}
stages {
stage('Set ') {
steps {
script {
echo "${params.chooseEnvironment}"
if ( "${chooseEnvironment}" == 'DEV' || "${chooseEnvironment}" == 'QA' ) {
env.USE = "NON-PROD"
} else if ("${chooseEnvironment}" == 'UAT' || "${chooseEnvironment}" == 'STAG') {
env.USE = "PROD"
} else {
echo "No environment selected. Exit the build"
sh "exit 1"
}
}
}
}
stage('Check Vars'){
steps{
echo "Environment choosen is ${env.USE}"
// your-other-shell actions. Address the variable with ${env.USE}
}
}
}
}
我想问一下,如果传递给 jenkins 的参数是“dev[,我该如何声明全局变量 USE“NON-PROD” =33=]" 或 "qa" 然后 "PROD" 如果传递给 jenkins 的参数是 "uat”或“分期”。这是条件的完整详细信息
IF ENV = dev OR qa
then
USE = NON-PROD
ELSE IF ENV = uat OR staging
then
USE = PROD
这是我的詹金斯设置
Jenkins 参数设置
到目前为止,这是我使用的测试声明脚本,但没有幸运地获得预期的响应。我还不确定 ELSE-IF 条件是否正常(对于 uat 或暂存)
def USE
pipeline {
agent any
stages {
stage('Declare Vars') {
when {
expression { params.ENV == 'qa' || params.ENV == 'dev' }
}
steps {
USE = "NON-PROD"
}
}
stage('Check Vars') {
steps {
sh """#!/bin/bash +x
echo USE = ${USE}
"""
}
}
}
}
这是我遇到的错误
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 12: Expected a step @ line 12, column 11.
USE = "NON-PROD"
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:428)
Finished: FAILURE
以下内容可行。一个简单的 if..else
条件即可。
pipeline {
agent any
parameters {
choice choices: ['UAT', 'STAG', 'DEV', 'QA'], description: '', name: 'chooseEnvironment'
}
stages {
stage('Set ') {
steps {
script {
echo "${params.chooseEnvironment}"
if ( "${chooseEnvironment}" == 'DEV' || "${chooseEnvironment}" == 'QA' ) {
env.USE = "NON-PROD"
} else if ("${chooseEnvironment}" == 'UAT' || "${chooseEnvironment}" == 'STAG') {
env.USE = "PROD"
} else {
echo "No environment selected. Exit the build"
sh "exit 1"
}
}
}
}
stage('Check Vars'){
steps{
echo "Environment choosen is ${env.USE}"
// your-other-shell actions. Address the variable with ${env.USE}
}
}
}
}