In some cases, you would like to use Jenkins declarative pipeline with the dynamic agent. For instance, you want to provide a list of available agent nodes as a parameter for the pipeline job. In this blog post, I will explain how you can configure such a behavior in just a few steps.

Jenkins Pipeline agent label from the parameter

Listing 1. Jenkinsfile
pipeline {
    agent {
        label params.AGENT == "any" ? "" : params.AGENT (1)
    }

    parameters {
        choice(name: "AGENT", choices: ["any", "docker", "windows", "linux"]) (2)
    }

    stages {
        stage("Build") {
            steps {
                echo "Hello, World!"
            }
        }
    }
}

There is one thing worth explaining. You can see that in the line , we check if params.AGENT is equal to "any". If this is the case, we put an empty string instead. (An empty string in this case is an equivalent of agent any - source.) Otherwise, Jenkins would search for the node with label "any" instead.

In the line , we define a list of available nodes. When you start the job and choose any from the dropdown, any available node will be used to run the job. If you choose docker, windows, or linux (or any other label you will define in your pipeline), the node with that exact label will be used to run the job. And that’s it.

Thank you!

Thank you so much for reading up to this point! I hope this article met your expectations, and you have learned something valuable from it. If you have one more minute, leave me a comment below, please. This way, I can thank you for your time more in person.

And last but not least. If you find this blog post valuable, help me spread the word, please. You can share it on Twitter, LinkedIn, or anywhere else you can think of. This single tweet or post on social media helps a lot. One more time - thank you so much! You’re awesome!

(PS: Check "Related posts" below for more content like this one.)