0

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_scriptkeyword?

5
  • 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
    Jun 12 '19 at 20:28
  • 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
    Jun 14 '19 at 8:12
  • @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
    Jun 14 '19 at 9:06
  • Sounds like this is just a navigation for one step, isn't it?
    – triplem
    Jun 14 '19 at 9:29
  • 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
    Jun 14 '19 at 9:44
1

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
        }
    }
}
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

2
  • 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
    Jun 15 '19 at 11:30
  • Yeah as far as I have seen there is no equivalent
    – papanito
    Jun 15 '19 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.