Alexander Samoylov
Alexander Samoylov

Reputation: 2578

Trigger a job by polling multiple GIT repos inside Jenkinsfile

At the Jenkinsfile with two git repositories these is an example of using multiple GIT repositories in a single Jenkins job:

node {
    dir('RepoOne') {
        git url: 'https://github.com/somewhere/RepoOne.git'
    }
    dir('RepoTwo') {
        git url: 'https://github.com/somewhere/RepoTwo.git'
    }

    sh('. RepoOne/build.sh')
    sh('. RepoTwo/build.sh')
}

How can I configure this job to track SCM changes of these 2 repositories so that the job triggers each time when at least one of repositories has updates?

The problem is that the job is polling not the repositories mentioned inside Jenkinsfile, but the repository of the Jenkinsfile itself (it is stored in a special repository, not with the source code together) which is mentioned in the GUI configuration of the job.

With the old Jenkins (without coded pipeline) and SVN plugin it was very easy, because all N repositories could be mentioned in the GUI configuration, checked out to a separate sub-directories of the single workspace and simultaneously polled.

How can I reach the same result with GIT + Jenkins Pipeline-As-Code? I tried to use also "poll: true" option in the Jenkinsfile, but it did not help. What does then this option do?

UPDATE 1: Here is the pipeline script which I really use and which does not work:

properties([
    pipelineTriggers([
        scm('H/5 * * * *')
    ])
])

node {
  stage ('Checkout') {
    dir('cplib') {
      git(
      poll: true,
          url: 'ssh://git@<server>:<port>/base/cplib.git',
          credentialsId: 'BlueOceanMsl',
          branch: 'master'
      )
    }
    dir('cpmffmeta') {
      git(
      poll: true,
          url: 'ssh://git@<server>:<port>/base/cpmffmeta.git',
          credentialsId: 'BlueOceanMsl',
          branch: 'master'
        )
    }
  }

  stage ('Build') {
    ...
  }

Upvotes: 6

Views: 11583

Answers (2)

Alexander Samoylov
Alexander Samoylov

Reputation: 2578

I found the cause of the problem. It was a fault described by https://issues.jenkins-ci.org/browse/JENKINS-37731. I used a wrong syntax. The correct one looks so:

properties([
    pipelineTriggers([
        [$class: "SCMTrigger", scmpoll_spec: "H/5 * * * *"],
    ])
])

Upvotes: 4

Jake
Jake

Reputation: 199

The git step should have a "polling" option, which you set to true, and then the job is configured to poll on scm change. You can also use the generic scm step to so the git checkout and make sure it's configured to poll. If setting "poll: true" doesn't work, then I suspect it's a bug. However, you probably do need to run at least one job manually first.

Upvotes: 0

Related Questions