0
0
Jenkinsdevops~10 mins

Tools directive in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Tools directive
Start Pipeline
Tools Directive Declared
Jenkins Installs/Uses Tool
Tool Available in PATH
Pipeline Steps Use Tool
Pipeline Completes
The Tools directive tells Jenkins which tool version to use, Jenkins sets it up, then the pipeline uses that tool.
Execution Sample
Jenkins
pipeline {
  agent any
  tools {
    maven 'Maven 3.8.1'
  }
  stages {
    stage('Build') {
      steps {
        sh 'mvn -version'
      }
    }
  }
}
This Jenkins pipeline uses the Tools directive to specify Maven version 3.8.1, then runs 'mvn -version' to show the tool is available.
Process Table
StepActionTool Directive EffectPipeline StateOutput
1Start pipelineNo tool set yetPipeline initializingNo output
2Read tools directiveMaven 3.8.1 requestedPreparing tool environmentNo output
3Jenkins sets up Maven 3.8.1Maven installed and added to PATHTool environment readyNo output
4Run 'mvn -version' commandUses Maven 3.8.1 from PATHBuild stage runningApache Maven 3.8.1 ...
5Pipeline completesTool usage donePipeline finishedBuild success
💡 Pipeline ends after build stage completes successfully with specified tool
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Maven VersionNone3.8.1 requested3.8.1 installed3.8.1 used3.8.1 used
Key Moments - 3 Insights
Why does Jenkins install the tool even if it is already on the system?
Jenkins installs the tool version specified in the tools directive to ensure consistent environment regardless of system setup, as shown in step 3 of the execution table.
What happens if the tool version specified is not configured in Jenkins?
The pipeline will fail to start the tool because Jenkins cannot find or install the requested version, stopping before step 4 where the tool is used.
Does the tools directive run the tool commands automatically?
No, the tools directive only sets up the tool environment. The pipeline steps must explicitly run commands like 'mvn -version' as in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does Jenkins add the tool to the PATH?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check the 'Tool Directive Effect' column for when Maven is installed and added to PATH.
According to the variable tracker, what is the Maven version after step 4?
A3.8.1 used
B3.8.1 requested
C3.8.1 installed
DNone
💡 Hint
Look at the 'After Step 4' column for 'Maven Version' in the variable tracker.
If the tools directive was removed, what would likely happen at step 4?
A'mvn -version' runs with default system Maven
B'mvn -version' fails because Maven is not set up by Jenkins
CPipeline skips the build stage
DPipeline installs Maven automatically anyway
💡 Hint
Refer to the explanation in key moments about tool setup and usage.
Concept Snapshot
Tools directive in Jenkins pipeline:
- Declared inside 'tools' block
- Specifies tool name and version (e.g., maven '3.8.1')
- Jenkins installs and adds tool to PATH
- Pipeline steps use the tool commands
- Ensures consistent tool environment across builds
Full Transcript
The Tools directive in Jenkins pipelines tells Jenkins which tool and version to use. When the pipeline starts, Jenkins reads this directive and installs the specified tool version if needed. It then adds the tool to the PATH so pipeline steps can run commands using that tool. For example, specifying Maven 3.8.1 ensures that when the pipeline runs 'mvn -version', it uses that exact Maven version. This setup guarantees consistent builds regardless of the system's default tools. The pipeline completes after running the build steps with the configured tool.