In this post, we will go through the steps to retrieve and list all environment variables in Jenkins using a pipeline script. This is especially useful for debugging, auditing, or understanding the environment Jenkins is running in.
Step 1: Create a New Pipeline Job
- Navigate to your Jenkins Dashboard.
- Click on New Item to create a new job.
- Enter a name for your job, e.g., ListAllEnvVars.
- Select Pipeline from the available project types and click OK.
Step 2: Define the Pipeline Script
- In the Pipeline section, choose Pipeline script from the Definition dropdown.
- Enter the following pipeline script in the Script field:
pipeline { agent any stages { stage('List All Environment Variables') { steps { script { echo "Listing all environment variables:" if (isUnix()) { sh 'printenv' // On Unix-based agents (Linux, macOS), use 'printenv' } else { bat 'set' // On Windows agents, use 'set' to list environment variables } } } } } }
Step 3: Save the Pipeline Configuration
- After entering the pipeline script, scroll to the bottom of the page and click Save.
Step 4: Run the Pipeline Job
- Click on the job name in the Jenkins Dashboard.
- Under Build History, click Build Now to trigger the pipeline.
Step 5: View the Build Output
- Once the build completes, click on the build number in Build History.
- Click on Console Output to see the results.
- You will see the environment variables listed in the output.
Example