Alternative solution for querying external systems, then prompting for human input

2 minute readKnowledge base

Issue

You have a requirement to query some system outside of CloudBees CI for some data, then you’d like a user to make a choice from a list, then based on their choice you’d like to query another external system and prompt again.

You might be currently using the community plugin called Active Choices plugin and are interested if there are any alternative implementations that use CloudBees supported plugins.

Resolution

An alternative would be to utilize multiple Pipeline stages, and use the Pipeline: Input Step plugin to prompt the end user for their choices, using the result of the choice to influence how the next stage is querying the external systems. This process can be repeated as many times as you require.

Here is an example Jenkinsfile demonstrating the concept:

def microservices=''
pipeline {
    agent none
    parameters {
        choice choices: ['dev', 'stage', 'prod'], description: '', name: 'environment'
    }
    stages {
        stage('fetch microservices'){
            agent any
            steps{
                script{
                    echo "environment=${params.environment}"
                    // In a real example, this 'sh' step will use `curl` or similar and `withCredentials` to reach out to an external API
                    def microservicesString=sh label: '', returnStdout: true, script: 'echo "a,b,c"'
                    microservices = microservicesString.split(',').collect{it as String}
                    echo "Got list:" + microservices
                }
            }
        }
        stage('choose microservices') {
            steps {
                input message: 'which microservice to query?', parameters: [choice(choices: microservices, description: '', name: 'microservice')]
            }
        }
    }
}

In this demo, the Build with parameters will allow you to choose your first option, in this case which environment should be queried.

In the fetch microservices stage, you would have to modify this example, and make the sh step use curl or similar and withCredentials to reach out to an external API, this demo just uses echo to list three microservices running in that environment: a,b,c.

In the choose microservices stage, we use the result of the previous stage to prompt the end user to choose which microservice they’d like to query.

This pattern of a stage to run a shell step to query an external API using a sh step, followed by a stage with human input, can be repeated as many times as you’d like.

Tested product/plugin versions