Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run jenkins pipeline relative to a sub-directory?

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?

like image 597
K. Siva Prasad Reddy Avatar asked Nov 17 '22 15:11

K. Siva Prasad Reddy


1 Answers

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'
        }
    }
}
like image 193
cilf Avatar answered Nov 30 '22 23:11

cilf