<p>You can use the Jenkins provided step <a href="https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#dir-change-current-directory" rel="nofollow noreferrer">dir()</a> which changes the directory to one relative to the Jenkins <code>env.WORKSPACE</code> (<code>/var/lib/jenkins/workspace/JOB_NAME</code> by default).</p>

<pre><code>stage('Build') { 
    steps {
        dir('path/to/dir') {
            // do stuff
        }

        dir('path/to/other/dir') {
            // do other stuff
        }
    }
}
</code></pre>

CC BY-SA 4.0
2019-06-20T04:00:31.173Z
2019-06-20T04:00:31.173Z
Argyle
<p>There is no equivalent for <code>before_script</code>. However you can define a function in your pipeline and call it in the begining of each stage. </p>

<pre><code>pipeline {
    agent any 
    stages {
        stage('Build') { 
            steps {
                cd
                // 
            }
        }
        stage('Test') { 
            steps {
                 cd
                // 
            }
        }
        stage('Deploy') { 
            steps {
                 cd
                // 
            }
        }
    }
}

def cd() {
   //your code
}
</code></pre>

<p>When you want to use your function in other pipelines as well you might want to look into <a href="https://jenkins.io/doc/book/pipeline/shared-libraries/" rel="nofollow noreferrer">shared libraries</a></p>

CC BY-SA 4.0
2019-06-15T11:30:18.957Z
Thanks for answering, that is actually how I'm handling it. My question was on the existence of an equivalent to GitLab's `before_script`, though. The cd command was an example of context.
avazula
CC BY-SA 4.0
2019-06-15T12:50:43.727Z
Yeah as far as I have seen there is no equivalent
papanito
CC BY-SA 4.0
2019-06-15T07:22:19.650Z
2019-06-20T07:00:11.967Z
2019-06-20T07:00:11.967Z
papanito
<p>I'm looking for Jenkins' equivalent of the <code>before_script</code> keyword provided by GitLab. </p>

<p>So far what I found is the <code>input</code> keyword that you can use before entering the <code>steps</code> part of the stage, but this feature needs a prompt, whereas I only want to perform a <code>cd</code>. </p>

<p>Is there a Jenkins equivalent of GitLab's <code>before_script</code>keyword?</p>

CC BY-SA 4.0
2019-06-12T20:28:34.470Z
If my memory is correct, you cannot put build steps outside of a `steps` block with declarative Pipeline.  However, you can with scripted Pipeline.
jayhendren
CC BY-SA 4.0
2019-06-14T08:12:45.183Z
May I ask for the purpose of a "cd" as a preliminary step to the whole build process? Could you not just re-configure the whole build process?
triplem
CC BY-SA 4.0
2019-06-14T09:06:25.443Z
@triplem The Git project involves different languages. I'm at the stage where I want to perform statistical analysis on the Python modules. The `cd` command is solely here to navigate to the folder that contains all the Python code. But maybe that's useless?
avazula
CC BY-SA 4.0
2019-06-14T09:29:36.837Z
Sounds like this is just a navigation for one step, isn't it?
triplem
CC BY-SA 4.0
2019-06-14T09:44:38.860Z
I do this `cd` for like half a dozen stages. Not a big deal but since I come from using GitLab and are new to Jenkins I wanted to know whether there was an equivalent. It's just pure curiosity.
avazula
<jenkins-pipeline><gitlab>
Equivalent of GitLab's "before_script" for Jenkins?
avazula
# Equivalent of GitLab's "before_script" for Jenkins?

(Asked by: username_0 on 2019-06-12)

I'm looking for Jenkins' equivalent of the `before_script` keyword provided by GitLab.

So far what I found is the `input` keyword that you can use before entering the `steps` part of the stage, but this feature needs a prompt, whereas I only want to perform a `cd`.

Is there a Jenkins equivalent of GitLab's `before_script`keyword?

**Tags:** jenkins-pipeline, gitlab

> **Comments:**
> **username_1**: If my memory is correct, you cannot put build steps outside of a `steps` block with declarative Pipeline.  However, you can with scripted Pipeline.  
> **username_2**: May I ask for the purpose of a "cd" as a preliminary step to the whole build process? Could you not just re-configure the whole build process?  
> **username_0**: @username_2 The Git project involves different languages. I'm at the stage where I want to perform statistical analysis on the Python modules. The `cd` command is solely here to navigate to the folder that contains all the Python code. But maybe that's useless?  
> **username_2**: Sounds like this is just a navigation for one step, isn't it?  
> **username_0**: I do this `cd` for like half a dozen stages. Not a big deal but since I come from using GitLab and are new to Jenkins I wanted to know whether there was an equivalent. It's just pure curiosity.  

---

## 2 answers

### Answer by username_3 on 2019-06-20. Score: 1 | Accepted answer

You can use the Jenkins provided step dir() which changes the directory to one relative to the Jenkins env.WORKSPACE (/var/lib/jenkins/workspace/JOB_NAME by default).
stage('Build') { 
    steps {
        dir('path/to/dir') {
            // do stuff
        }

        dir('path/to/other/dir') {
            // do other stuff
        }
    }
}


---

### Answer by username_4 on 2019-06-15. Score: 0

There is no equivalent for before_script. However you can define a function in your pipeline and call it in the begining of each stage. 
pipeline {
    agent any 
    stages {
        stage('Build') { 
            steps {
                cd
                // 
            }
        }
        stage('Test') { 
            steps {
                 cd
                // 
            }
        }
        stage('Deploy') { 
            steps {
                 cd
                // 
            }
        }
    }
}

def cd() {
   //your code
}

When you want to use your function in other pipelines as well you might want to look into shared libraries

> **Comments:**
> **username_0**: Thanks for answering, that is actually how I'm handling it. My question was on the existence of an equivalent to GitLab's `before_script`, though. The cd command was an example of context.  
> **username_4**: Yeah as far as I have seen there is no equivalent  

---

