Changeset by git repo

I’m using multiple git repositories for the build which I’m collecting using:

dir('repo1') {
  git credentialsId: "user", url: "git://git/repo.git"
}

All is working fine. I’m getting all changesets from all cloned repositories, but I want to limit a build when some files change only.
I know I can use:

when {
  changeset: "**/somefile"
}

but I want to limit this when somefile change in one cloned repo or any file change on another. Something like (it is not working, for obvious reasons):

when {
  anyOf {
    changeset: "**/somefile", repo: "repo1"
    changeset: "*", repo: "repo2"
  }
}

How can I limit my changeset filer to selected git repository?

Welcome back, @radekk.

As far as I know, the changeset condition in the when directive is used to control whether the stage should be executed based on changes in the source code.

However, I don’t think it supports filtering by specific Git repositories when multiple repositories are used in the same pipeline.

A workaround for this limitation would maybe to use a script step to manually check for changes in the specific repositories. :thinking:

You could use Git commands to check for changes in the specific files or directories you’re interested in.

stage('My Stage') {
    when {
        expression {
            def changesInRepo1 = false
            def changesInRepo2 = false

            dir('repo1') {
                changesInRepo1 = sh(script: "git diff --quiet HEAD HEAD~1 **/somefile", returnStatus: true) != 0
            }

            dir('repo2') {
                changesInRepo2 = sh(script: "git diff --quiet HEAD HEAD~1", returnStatus: true) != 0
            }

            return changesInRepo1 || changesInRepo2
        }
    }
    steps {
        // Your steps here
    }
}

In this example, the expression condition in the when directive is used to execute a shell script that checks for changes in the specific files or directories.
The git diff --quiet HEAD HEAD~1 command is used to check for changes between the current commit and the previous commit.
The **/somefile pattern is used to check for changes in a specific file, and the returnStatus: true option is used to return the exit status of the command instead of failing the build if the command fails.

I know it’s pretty verbose and not elegant; you may find a much better solution later on. :person_shrugging: