简体   繁体   中英

how to call parameters from groovy map script to Jenkins Pipeline

simply I need to write jenkinsfile pipeline which contains choices and this choices come come from another groovy script that provides a map in this map I can add environment name and 3 values DB , APP , WEB so when I start build a new job I get parameter build with choices to pick the environment name DEV, QA , UAT and according to this choice it will pass map/list of the three IPs for DB,APP, WEB so I can use this values during build

/env.groovy

def DEV = [ DB : 10.0.0.5 , APP : 10.0.0.10 , WEB : 10.0.0.15 ] 
def UAT = [ DB : 10.0.0.20 , APP : 10.0.0.25 , WEB : 10.0.0.30 ] 
def QA = [ DB : 10.0.0.35 , APP : 10.0.0.40 , WEB : 10.0.0.45 ] 

take this values from env.groovy file and pass it to choice in Jenkinsfile so I can get drop down menu with ( DEV - UAT - QA ) before I click build

I do not want to add this values inside the Jenkinsfile , I need to added it to separate groovy script (env.groovy)

this is for new pipeline Jenkinsfile which is running on ubuntu Jenkins server

String[] env =  env()

pipeline {
    agent any
    properties([
            parameters([
                    choice(
                    choices: env,
                    description: 'Select the environment',
                    name: 'ENV',
                    )
            ])
    ])

    stages {
        stage('deploy') {
            steps {
                sh "echo ${ENV}"
            }
        }    
   }    
}

expecting to echo list of environment variables DB , APP and WEB , so I can be sure I can pass this values to another shell script that I will add later to deploy , but I didn't get this menu in the 1st place and I get this error

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 5: The ‘properties’ section has been renamed as of version 0.8. Use ‘options’ instead. @ line 5, column 5.
       properties([
       ^

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:133)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:126)
    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:320)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE

IMHO there is no possibility to use variable as maps name directly, but we have switch statement.

Try this one:

def map

pipeline {
    agent any

    parameters {
            choice( name: 'env', choices: ['DEV', 'UAT', 'QA'] , description: "Choose ENV?" )
    }

    stages {
        //I'm a bit lazy, in Your case use regular file :) 
        stage('create file') {
            steps {
                script {
                    sh "echo \"DEV = [ DB : '10.0.0.5' , APP : '10.0.0.10' , WEB : '10.0.0.15' ]\" > env.groovy"
                    sh "echo \"UAT = [ DB : '10.0.0.20' , APP : '10.0.0.25' , WEB : '10.0.0.30' ]\" >> env.groovy"
                    sh "echo \"QA = [ DB : '10.0.0.35' , APP : '10.0.0.40' , WEB : '10.0.0.45' ]\" >> env.groovy"
                }
            }
        }
        stage('switch time') {
            steps {
                script{
                    load "$JENKINS_HOME/workspace/box1/env.groovy"
                    switch (params.env) {
                    case 'DEV':
                        map = DEV
                        break
                    case 'UAT':
                        map = UAT
                        break
                    case 'QA':
                        map = QA
                        break
                    default:
                        map = []
                        break
                    }
                }
            }
        }
        stage('deploy') {
            steps {
                println map.DB
                println map.APP
                println map.WEB
            }
        }
    }
}

Expected output:

Started by user 3sky
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /app/jenkins/home/workspace/box1
[Pipeline] {
[Pipeline] stage
[Pipeline] { (create file)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
[box1] Running shell script
+ echo 'DEV = [ DB : '\''10.0.0.5'\'' , APP : '\''10.0.0.10'\'' , WEB : '\''10.0.0.15'\'' ]'
[Pipeline] sh
[box1] Running shell script
+ echo 'UAT = [ DB : '\''10.0.0.20'\'' , APP : '\''10.0.0.25'\'' , WEB : '\''10.0.0.30'\'' ]'
[Pipeline] sh
[box1] Running shell script
+ echo 'QA = [ DB : '\''10.0.0.35'\'' , APP : '\''10.0.0.40'\'' , WEB : '\''10.0.0.45'\'' ]'
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (switch time)
[Pipeline] script
[Pipeline] {
[Pipeline] load
[Pipeline] { (/app/jenkins/home/workspace/box1/env.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (deploy)
[Pipeline] echo
10.0.0.5
[Pipeline] echo
10.0.0.10
[Pipeline] echo
10.0.0.15
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM