Jenkins pipeline part 1 – agents

Jenkins pipeline part 1 – agents

Jenkins pipeline part 1 – agents

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In today’s blog post, we will discuss how to use a Jenkins agent in a pipeline and what are different options available while using Jenkins agent in a pipeline with examples.

What is an agent is Jenkins?

An agent is typically a machine, or container, which connects to a Jenkins controller and executes tasks when directed by the controller.

How to use an agent in Jenkins pipeline?

You can use an agent block inside your pipeline definition to instruct the pipeline to get executed to that specific agent. The agent section specifies where the entire Pipeline, or a specific stage, will execute in the Jenkins environment depending on where the agent section is placed. The section must be defined at the top-level inside the pipeline block, but stage-level usage is optional.

Note: The difference between top-level agent and stage-level agent is that in case of top-level agent the options are invoked after entering the agent whereas in case of stage-level agent the options are invoked before entering the agent and before checking any when conditions.

Now let’s dig down a bit on different options available in Jenkins pipeline agent with examples.

Jenkins pipeline part 1 – agents:

What are different parameters available in Jenkins pipeline agent block?

In order to support the wide variety of use-cases Pipeline authors may have, the agent section supports a few different types of parameters. These parameters can be applied at the top-level of the pipeline block, or within each stage directive.

Prerequisites:

any:

Execute the Jenkins Pipeline, or stage, on any available agent.

Example:

label:

Execute the Pipeline, or stage, on an agent available in the Jenkins environment with the provided label.

none:

When applied at the top-level of the pipeline block no global agent will be allocated for the entire Pipeline run and each stage section will need to contain its own agent section.

Note: If top-level agent is set to none, you must declare stage-level agent for each stage in your pipeline.

node:

agent behaves the same as label, but node allows for additional options (such as customWorkspace).

Note: customWorkspace runs the Pipeline or individual stage this agent is applied to within this custom workspace, rather than the default. It can be either a relative path, in which case the custom workspace will be under the workspace root on the node, or an absolute path.

docker:

Execute the Pipeline, or stage, with the given container which will be dynamically provisioned on a node pre-configured to accept Docker-based Pipelines, or on a node matching the optionally defined label parameter. docker also optionally accepts an args parameter which may contain arguments to pass directly to a docker run invocation, and an alwaysPull option, which will force a docker pull even if the image name is already present.

docker also optionally accepts a registryUrl and registryCredentialsId parameters which will help to specify the Docker Registry to use and its credentials. The parameter registryCredentialsId could be used alone for private repositories within the docker hub.

Example:

dockerfile:

Execute the Pipeline, or stage, with a container built from a Dockerfile contained in the source repository. In order to use this option, the Jenkinsfile must be loaded from either a Multibranch Pipeline or a Pipeline from SCM. Conventionally this is the Dockerfile in the root of the source. If building a Dockerfile in another directory, use the dir option. If your Dockerfile has another name, you can specify the file name with the filename option. You can pass additional arguments to the docker build command with the additionalBuildArgs option.

Example:

kubernetes:

Execute the Pipeline, or stage, inside a pod deployed on a Kubernetes cluster. In order to use this option, the Jenkinsfile must be loaded from either a Multibranch Pipeline or a Pipeline from SCM. The Pod template is defined inside the kubernetes { } block.

Example:

Hope you have enjoyed this article, to get more details on Jenkins, please refer below Jenkins official documentation.

https://www.jenkins.io/doc/

Leave a Reply

Close Menu