Jenkinsfile poll specific repository

The git checkout step allows to configure the given repository to be included in the polling, at least the snippet generator provides a checkbox for this. Of course this can only work when the step is in the same pipeline. You can have multiple checkout steps in one job so you can potentially poll for multiple repos.

To avoid a full clone you could work with git ls-remote repo. You will need to persist the git hash you get yourself (so you need a fixed workspace, if the job can run on different agents that would not work out of the box) and then compare if something has changed and then trigger a build.

I do this myself ( the repo is over 10GiB and a clone takes about 1h and a simple fetch 2-3 minutes) and store the file with the last git hash on an nfs share. But the ls-remote check takes a second or so.

This is my poller job:

propFile = "/somenfs_mount/POLLER_job_for_<branch>/commit.prop"
triggerBuilds = false
node('pollerhost') {
    def d = [COMMIT: 'no build']
    props = readProperties defaults: d, file: propFile
    last_build_commit = props['COMMIT']
    echo("Last build commit was $last_build_commit")
    sshagent(['mycred']) {
        ls_remote = sh label: '', returnStdout: true, script: 'git ls-remote --heads ssh://user@gerrit.host:29418/repo git refs/heads/<branch>'
    }
    current_commit = ls_remote.split()[0]
    echo("Current commit of <branch> is $current_commit")
    if (last_build_commit != current_commit)
    {
        echo("new content found, trigger build")
        writeFile file: propFile, text: "COMMIT=$current_commit"
        triggerBuilds = true
    } else {
        echo("no new content found, no build necessary")
    }
}

if (triggerBuilds) {
    build job: 'otherjob', parameters: [string(name: 'COMMIT', value: current_commit)], propagate: false
}
2 Likes