7

I have a git repository with 2 modules in it. One is SpringBoot based backend module and another one is VueJS based frontend module.

app-root
  - backend
  - frontend

I have a declarative style Jenkinsfile to build my backend module with relevant maven commands. I want to execute all those maven commands from inside backend directory.

One option is to use dir("backend") { ....} for all commands separately, which looks ugly to me.

Is there any other option to instruct Jenkins to execute the entire pipeline from inside a sub-directory?

K. Siva Prasad Reddy
  • 11,786
  • 12
  • 68
  • 95
  • Do you want to have two separated pipelines for frontend and backend or one pipeline? – rmpestano Feb 11 '19 at 13:03
  • Yes. Currently, I have pipeline only for backend but I will be creating a separate pipeline for frontend as well. – K. Siva Prasad Reddy Feb 11 '19 at 13:08
  • The "dir" function is exactly designed for this purpose. You can't externally configure the project (eg. configure page) to run everything in a subdir. The dir() function IMO is quite intuitive, a bunch of rm and cp operation is not only slower but also makes the code less readable. – ThomasMX Mar 14 '23 at 13:17

2 Answers2

1

I ended up with a "prepare project" stage that puts the subdirectory content into the root.

It's also probably a good idea to remove all the root contents (stage "clean") to be absolutely sure there are no leftovers from previous builds.

node {
    def dockerImage

    stage('clean') {
      sh "rm -rf *"
    }

    stage('checkout') {
        checkout scm
    }

    // need to have only 'backend' subdir content on the root level
    stage('prepare project') {
        // The -a option is an improved recursive option, that preserve all file attributes, and also preserve symlinks.
        // The . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.
        sh "cp -a ./backend/. ."
        sh "rm -rf ./backend"
        // List the final content
        sh "ls -la"
    }

    stage('build docker image') {
        dockerImage = docker.build("docker-image-name")
    }

    stage('publish docker image') {
        docker.withRegistry('https://my-private-nexus.com', 'some-jenkins-credentials-id') {
            dockerImage.push 'latest'
        }
    }
}
cilf
  • 650
  • 7
  • 16
-2

You can have jenkinsfiles inside backend and frontend modules and just point to them on each pipeline, eg:

enter image description here

and on the pipeline itself you just cd to the submodule and execute its commands:

pipeline {
   agent any

   stages {
        stage('build') {
            steps {
                 sh 'cd backend'
                 sh 'mvn clean package'
             }
       }

    //other stages
   }

}

if you don't want to cd to a sub module you can use sparse checkouts but in this case you have to change the path to the jenkinsfile accordingly because it will be on the root folder.

rmpestano
  • 838
  • 1
  • 8
  • 17
  • 4
    This is the problem I mentioned..having to use dir("backend") or "cd module" for every command. I am thinking of is there anyway to configure so that entire pipeline to run from inside a directory. – K. Siva Prasad Reddy Feb 11 '19 at 13:32
  • 1
    Not needed for every command, just the first time. Also make your question clear, you only mention "dir is ugly" without saying what's the real problem. If cd is problem then sparse checkouts is the way to go. – rmpestano Feb 11 '19 at 13:33
  • I believe every stage will run in the context of root folder. So for all the steps in separate stages we need to cd into directory. – K. Siva Prasad Reddy Feb 11 '19 at 13:35
  • yes, you're right, so if `dir` is a problem to you I think you'll have to use sparse checkouts or have separated git repositories. – rmpestano Feb 11 '19 at 13:48
  • 1
    I use command similar to sh 'cd backend' just as you do, but the following command still not go to the subdirectory ,do you know why? – yuxh Aug 12 '19 at 09:39
  • "cd" doesn't work! – emeraldhieu Jan 20 '23 at 16:47