1

I'm very new to jenkins. I have a monorepo where I need to check what needs to be build. Created a pipeline element in Jenkins, named it Test and copy pasted a script to do a check what files changed:

def changedFiles = currentBuild.changeSets
    .collect { it.getItems() }
    .flatten() //Ensures that we look through each commit, not just the first.
    .collect { it.getAffectedPaths() }
    .flatten()
    .toSet() //Ensures uniqueness.
echo("Changed files: ${changedFiles}")

def changedDirectories = changedFiles.collect {
    if (it.contains('folder/in/monorepo/project/foo')) { //Code cut for simplicity
        return "Foo"
    }
    return ''
}
.unique()
echo("Changed directories: ${changedDirectories}")

if (changedDirectories.contains("Foo")) {
    stage('Foo Build') {
        node {
            env.NODEJS_HOME = "${tool 'NodeJS'}"
            env.PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
            dir("folder/in/monorepo/buildscripts/") {
                sh 'mybuildscript.sh'
            }
        }
    }
} else {
    echo("No Foo Build")
}

The code for loading NodeJS is documentend in the NodeJS plugin.

I installed Jenkins, tried some things and then installed NodeJS plugin and updates for other plugins. Now, with the updated plugins there is some trouble which basically is related to this: Why does Jenkins creates a subfolder within the workspace@script folder to checkout git code instead of the workspace@script itself?

Instead of cloning into the workspace folder, the git repo gets cloned to folder with a unique id. This leads to the fact, that my folder like in the command dir("folder/in/monorepo/buildscripts/") is not found or to be more precise: It's created and empty and not the one which is in the git repo because it is executed relative to my empty workspace instead relative to the cloned repo.

My questions are:

  1. Is there a much easier way to achieve what I try? AFAIK I need to implement the search for the folder as in the referenced so question.
  2. Doesn't this security fix from the referenced so question breaks all builds / jenkinsfiles? I mean: Eventually in a pipeline someone wants to call a build script, a build tool or a simple make with the makefile in the repo.

EDIT: I just realised that the only reason why the repo is cloned, is because I use the option "Pipeline script from SCM" with "Lightweight checkout" unchecked. I assume that the intended way would be to use a "Lightweight checkout" to just get the Jenkinsfile and then clone the repo as part of the script. A "Pipeline script" written in the gui, would also never get a clone of the repository.

Enak
  • 546
  • 3
  • 15

0 Answers0