Home > Blockchain >  Is it possible to pass associative arguments in Jenkins?
Is it possible to pass associative arguments in Jenkins?

Time:07-02

Current Jenkins setup deploys single version of different services, if they are selected via Boolean parameter option.

Say, there are four services in total - A, B, C, D and the version to be deployed will remain same(1.0), for all services. If only A is checked/enabled, script will deploy version 1.0 for service A and the same logic applies for other services.

enter image description here

enter image description here

Now the change being, each service can have its own version to be deployed when its selected. I'm not sure how to handle this in Jenkins.

Ex . If service_A is selected, it should have a placeholder or mapping to get its corresponding deployment version also and the same goes for other services too.

Is it possible via Jenkins ?

CodePudding user response:

Update: Non-Pipeline Solution with Active choice plugin.

You can get this done using the enter image description here

For the Groovy script add the following content.

return ["Service01", "Service02"]

Next, add an Active Choices Reactive Reference Parameter like below.

enter image description here

enter image description here

For the groovy part add the following content.

def htmlContent = "<table><tr>"

if(services.contains("Service01")){
 htmlContent = """
    ${htmlContent}
    <tr>
    <td>
    <label title=\"Service01\" class=\" \">Service 01 </label>
    </td>
    <td>
    <input type=\"text\" class=\" \" name=\"value\" value=\"version\"> </br>
    </td>
    </tr>
"""
}

if(services.contains("Service02")){
 htmlContent  = """
    ${htmlContent}
    <tr>
    <td>
    <label title=\"Service02\" class=\" \">Service 02 </label>
    </td>
    <td>
    <input type=\"text\" class=\" \" name=\"value\" value=\"version\"> </br>
    </td>
    </tr>
"""
}


htmlContent = "${htmlContent}</tr></table>"

return htmlContent

Make sure Referenced parameters are added. This needs to refer to the variable you used in the first parameter.

Now the pipeline execution will look like the below.

enter image description here

Note: Active Choice Parameter plugin does not support blue ocean UI.

With A declarative Pipeline

If you are willing to take everything to a declarative pipeline you can do something like the below.

def INPUT_1 = ""
def INPUT_2 = ""

pipeline {
    agent any
    stages {
     stage("Get Inputs") {
                steps{
                    timeout(time: 300, unit: 'SECONDS') {
                    script {
                        // The Initial Choice
                        INPUT_1 = input message: 'Please select', ok: 'Next',
                                        parameters: [
                                        choice(name: 'SERVICE', choices: ['Service01','Service02'], description: 'Please select')]
                              
                        if(INPUT_1.equals("Service01")) {
                              INPUT_2 = input message: 'Please Select', ok: 'Next',
                                              parameters: [ choice(name: 'Version', choices: ['v01','v02'], description: 'Please select')] 
                        } else {
                              INPUT_2 = input message: 'Please Select', ok: 'Next',
                                        parameters: [
                                        choice(name: 'Version', choices: ["v1.1","v1.2","v1.3"], description: 'Select the file')]              
                        }

                        echo "INPUT 1 ::: ${INPUT_1}"
                        echo "INPUT 2 ::: ${INPUT_2}"
                        }
                    }
                }
            }
    }
    post {
        success {
            echo 'The process is successfully Completed....'
        }
    }
}

Which results in the following.

enter image description here

enter image description here

  • Related