1. Overview

Jenkins pipeline automates a project’s entire CI/CD process using Jenkinsfile. A wide range of applications can be built, tested, and deployed using the pipeline. In addition, whenever code changes, the Jenkins pipeline can automatically run, saving time and reducing errors.

In Jenkins, sometimes we need to run a series of actions at a specific location on the file system. Thus, in such cases, we can change the working directory using the “dir” or “sh” steps.

In this tutorial, we’ll look at how to change the working directory in a Jenkins pipeline.

2. Using the dir Step

There are several ways to change the working directory of Jenkins pipelines. The dir step is a built-in step in Jenkins that allows us to change to a different directory for the duration of a block. This is very useful when we want to run specific commands from a particular directory.

Let’s look at the dir step to change our current working directory to a subdirectory “scripts”:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                dir('scripts') {
                    /* execute commands in the scripts directory */
                }
            }
        }
    }
}

We defined a Build stage in our pipeline that changes the working directory. Using the dir function, the script switches to the directory “scripts”, then runs the build steps. In addition, we used “any” agent in the above script, which means that any available Jenkins agent can execute this pipeline job. As soon as the block ends, the working directory will return to its previous state.

Additionally, we can change to any directory using this method, not just subdirectories. In order to change directories outside of the current working directory, we need to provide the absolute path:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                dir('/var/jenkins_home/workspace/SamplePipeline/scripts') {
                    /* execute commands in the scripts directory */
                }
            }
        }
    }
}

In the above case, we’ve successfully changed the working directory to /var/jenkins_home/workspace/SamplePipeline/scripts.

3. Using the sh Step

Another way to change directories in a Jenkins pipeline is to use the sh step with the cd command. Using the sh step, we can change our working directory to the scripts directory:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'cd scripts'
                    /* execute commands in the scripts directory */
            }
        }
    }
}

In this case, the sh step uses the cd command to change the current working directory to “scripts”. The sh step allows us to execute shell commands in a pipeline. As soon as the working directory is changed, the pipeline runs the build steps within the “scripts” directory.

4. Conclusion

In this article, we learned how to change the working directory in a Jenkins pipeline. First, we looked at changing the directory using the dir step. After that, we explored the same using the cd command with the sh step.

We can find the script for the directory change pipeline over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.