Sling Academy
Home/DevOps/Jenkins: How to set PATH environment variable (Mac, Windows)

Jenkins: How to set PATH environment variable (Mac, Windows)

Last updated: February 03, 2024

Introduction

Setting the PATH environment variable for Jenkins is a crucial step in ensuring that your automated jobs can reliably find and execute external tools and scripts. Whether you’re running Jenkins on a Mac or Windows system, properly configuring the PATH variable can streamline your continuous integration/continuous deployment (CI/CD) workflows. This guide will walk you through the process of setting the PATH variable within Jenkins on both Mac and Windows platforms, complemented by practical code examples.

Understanding PATH in Jenkins

Before diving into the details, it’s important to understand what the PATH environment variable is and why it’s important for Jenkins. The PATH variable is a system-wide setting that tells your operating system where to look for executable files. For Jenkins, setting the PATH correctly ensures that it can find the tools it needs to run builds, tests, and deployments without issues.

Basic Setup

Let’s start with the basics of setting the PATH in Jenkins.

Global Configuration

1. First, log in to your Jenkins dashboard.
2. Navigate to ‘Manage Jenkins’ > ‘Configure System’.
3. Scroll down to the ‘Global properties’ section.
4. Check the ‘Environment variables’ checkbox and click on ‘Add’.
5. In the ‘Name’ field, enter ‘PATH’ and in the ‘Value’ field, add the paths to your executable files, separated by a semicolon (;) on Windows or a colon (:) on macOS.

Note: Some tools might require their specific paths to be added. For example, if you’re using Git, Maven, or Node.js, ensure you append their bin directories to the PATH.

Mac-Specific Configuration

On a Mac, you’ll likely want to modify the PATH for Jenkins launched as a service. This requires a different approach.

Using launchd to Set PATH

1. Locate the Jenkins.plist file, typically found in /Library/LaunchDaemons/.
2. Open the file in a text editor with root privileges.
3. Find the section and add a new entry for PATH. Here’s an example:

<key>EnvironmentVariables</key>
<dict>
  <key>PATH</key>
  <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/path/to/your/tools</string>
</dict>

After making changes, restart Jenkins for them to take effect.

Windows-Specific Configuration

In a Windows environment, setting the PATH often involves either batch scripts or directly setting the system PATH variable.

Using a Batch File

Create a batch file to start Jenkins with a modified PATH. Here’s an example:

SET PATH=C:\Path\To\Your\Tools;%PATH%
java -jar jenkins.war

This will temporarily modify the PATH for the duration of Jenkins’ runtime.

Modifying System PATH

1. Right-click on This PC (or My Computer) and select Properties.
2. Go to Advanced system settings > Environment Variables.
3. In the System variables section, locate and select the PATH variable, then click Edit.
4. Append the paths to your tools, ensuring each is separated by a semicolon (;).

Advanced Configuration

Once you’re comfortable with the basics, you may need to dynamically set the PATH within your Jenkins pipelines or jobs.

Using the EnvInject Plugin

The EnvInject Plugin is a powerful tool for managing environment variables within Jenkins jobs.

1. Install the EnvInject Plugin from the Jenkins Plugin Manager.
2. In your job’s configuration, add a new ‘Inject environment variables’ build step.
3. Specify the PATH modification in the ‘Properties Content’ section like so:

PATH=/new/path/to/add:$PATH

This will prepend the specified path to the existing PATH variable for the duration of the job.

Conclusion

Correctly setting the PATH environment variable in Jenkins ensures that your CI/CD pipeline has seamless access to necessary tools and scripts across different stages of automation. Whether you’re working on a Mac or Windows system, the key is to understand how to effectively manage these settings at both a global and job-specific level.

Next Article: Jenkins: How to set a timeout for a build

Previous Article: Jenkins: How to terminate a zombie build (stuck in the queue)

Series: Jenkins Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide