Triggering Branch Indexing on Multibranch Pipelines (Jenkins/Git)

后端 未结 3 567
借酒劲吻你
借酒劲吻你 2021-01-02 08:25

I\'m trying to automatically trigger \'Branch Indexing\' on a Multibranch Pipelines job in Jenkins.

At the moment, only one method seems to actually work, which is p

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 08:40

    The method ComputedFolder.scheduleBuild() can be invoked from a groovy script.

    I have just triggered branch indexing in one multibranch pipeline project from the groovy code in a different multibranch pipeline project, which is then triggering a downstream build in that project.

    The code is something like:

    @NonCPS
    void scanRepo(String downStreamProjectName) {
        Jenkins.instance.getItemByFullName(downStreamProjectName).scheduleBuild()
    }
    ...
    String downStreamProject = 'my-folder/my-multibranch-project'
    String downStreamJob = "${downStreamProject}/${env.BRANCH_NAME}"
    if (Jenkins.instance.getItemByFullName(downStreamJob) == null) {
        scanRepo(downStreamProject)
        while (Jenkins.instance.getItemByFullName(downStreamJob) == null) {
            sleep(1)
        }
    }
    build([job: downStreamJob, wait: false, quietPeriod: 0])
    

    Notice that Jenkins.instance.getItemByFullName(downStreamProjectName) is the WorkflowMultiBranchProject which is not Serializable, so some care needs to be taken.

提交回复
热议问题