Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 899
Stanley Luo Avatar asked Dec 19 '17 00:12

Stanley Luo


People also ask

Can a repo have multiple Jenkinsfile?

Ultimately, a branch's Jenkinsfile describes the Pipeline for that branch. Fundamentally, there can be only one Jenkinsfile, and therefore one Pipeline, per Git repository branch.

What is Jenkins Multibranch pipeline?

What's a Jenkins Multibranch Pipeline? A multibranch job is simply a folder of pipeline jobs. For every branch you have, Jenkins will create a folder. So instead of creating a pipeline job for each of the branches you have in a git repository, you could use a multibranch job.

How does Jenkins define pipeline?

Jenkins Pipeline (or simply "Pipeline") is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins. A continuous delivery pipeline is an automated expression of your process for getting software from version control right through to your users and customers.

Who uses Monorepos?

Google, Facebook, Microsoft, Uber, Airbnb, and Twitter all employ very large monorepos with varying strategies to scale build systems and version control software with a large volume of code and daily changes.


1 Answers

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

like image 124
Antonio O. Avatar answered Oct 17 '22 04:10

Antonio O.