Stan Luo
Stan Luo

Reputation: 3889

How can Jenkins pipeline work with monorepo

Am very new to Jenkins. My repository is a monorepo - contains two sub projects, web_app and native_app. I want to use Jenkins as CI engine so that every time code pushes to the repo Jenkins will help do the build-test-delivery workflow automatically.

I created a pipeline project, intuitively seems I should create two Jenkinsfile, each under related folder, i.e.:

web_app/
  |-Jenkinsfile
native_app/
  |-Jenkinsfile

However, I soon realized this will result in problems - I need to change working directory for nearly every stage/step. Tried

stage('Build') { 
  steps {
      sh 'cd ./web_app/'
      sh 'ls'
      sh 'git pull'
    }
}

but doesn't work, the working directory is not changed.

I haven't found an effective method to change workspace for entire pipeline, and am worried that this monorepo structure would result in more problem with Jenkins in the future. Should I split this repository, or is there some handy way to change work directory?

Upvotes: 9

Views: 9858

Answers (1)

Antonio O.
Antonio O.

Reputation: 555

You can use the dir step to change the directory of a block of steps. Your sample code would look like this:

stage('Build') {
  dir('web_app') {
    sh 'ls'
    sh 'git pull'
  }
}

Documentation of the dir step

dir: Change current directory

Change current directory. Any step inside the dir block will use this directory as current and any relative path will use it as base path. path

Type: String

Upvotes: 5

Related Questions