How to manage Monorepos within a Jenkinsfile in CloudBees CI

Issue

  • How to manage Monorepos within a Jenkinsfile in CloudBees CI?

Environment

Resolution

The main idea behind it is using built-in conditions for different modules from a monorepo when { changeset “**path/to/module-example/.” }

An example would be as follows:

  • Monorepo-example is composed by 3 modules (frontend, backend-web, backend-api):
. (monorepo-example)
├── backend
│   ├── api
│   └── web
├── frontend
└── Jenkinsfile
  • A valid Jenkinsfile would be similiar to:
pipeline {
    agent none
    stages {
        stage('Build Frontend') {
            agent { docker { image 'my-node-agent' } }
            when {
                changeset "**/frontend/*.*"
                beforeAgent true
            }
            steps {
                dir('frontend') {
                  sh 'npm install'
                  sh '...'
                }
            }
        }
        stage('Build Web') {
            agent { docker { image 'my-maven-agent' } }
            when {
                changeset "**/backend/web/*.*"
                beforeAgent true
            }
            steps {
               dir (backend/web) {
                sh 'mvn -B -DskipTests clean package'
                sh '...'
               }
            }
        }
        stage('Build REST API') {
            agent { docker { image 'my-golang-agent' } }
            when {
                changeset "**/backend/api/*.*"
                beforeAgent true
            }
            steps {
               dir (backend/api) {
                 sh 'go build'
                 sh '...'
               }
            }
        }
    }
}

Alternatively, you could replace this section:

...
    stage('Build Frontend') {
        agent { docker { image 'my-node-agent' } }
        when {
            changeset "**/frontend/*.*"
            beforeAgent true
        }
        steps {
            dir('frontend') {
                sh 'npm install'
                sh '...'
            }
        }
    }
...

by loading a Jenkinsfile per module which is stored in your Shared Library. Refer to Share a standard Pipeline across multiple projects with Shared Libraries.

@Library("shared-libs@master") _
...

    stage('Build Frontend') {
        when {
            changeset "**/frontend/*.*"
            beforeAgent true
        }
        steps {
            frontEndPipeline {
                agent = 'my-node-agent'
                serverPort = '8080'
                developmentServer = 'dev-myproject.mycompany.com'
                stagingServer = 'staging-myproject.mycompany.com'
                productionServer = 'production-myproject.mycompany.com'
            }
        }
    }
...

Have more questions?

0 Comments

Please sign in to leave a comment.