Trigger workflow on Github push - Pipeline plugin

2020-05-20 02:30发布

We are using the pipeline plugin with multibranch configuration for our CD. We have checked in the Jenkinsfile which works off git.

git url: "$url",credentialsId:'$credentials'

The job works fine, but does not auto trigger when a change is pushed to github. I have set up the GIT web hooks correctly.

Interestingly, when I go into a branch of the multibranch job and I click "View Configuration", I see that the "Build when a change is pushed to Github" is unchecked. There is no way to check it since I can not modify the configuration of the job (since it takes from parent) and the same option is not there in parent.

Any ideas how to fix this?

7条回答
小情绪 Triste *
2楼-- · 2020-05-20 03:02
node{
stage('Build and Test') {
    properties([pipelineTriggers([[$class: 'GitHubPushTrigger'], pollSCM('* * * * *')])])
    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'xxx-xxx-xxxx[your credentails Id]', url: 'https://github.com/git']]])
    echo 'Build and Test has been done'
}

}

查看更多
在下西门庆
3楼-- · 2020-05-20 03:04

For declarative pipelines try:

pipeline {
    ...
    triggers {
        githubPush()
    }
    ...
}

For me this enables the checkbox "GitHub hook trigger for GITScm polling", but polling is not actually required. This requires the GitHub plugin.

查看更多
The star\"
4楼-- · 2020-05-20 03:10

For declarative pipelines, try this:

pipeline {
    agent any
    triggers {
        pollSCM('') //Empty quotes tells it to build on a push
    }
}
查看更多
老娘就宠你
5楼-- · 2020-05-20 03:14

Resorting to polling adds latency - time that it takes for a build to start and hence finish giving back a result.

It seemed to me that the basic plugins have a low level of abstraction, so I switched to the Github Organization Folder plugin, which depends on all of them and sets up an organization hook for triggering builds branches and/or pull requests.

查看更多
再贱就再见
6楼-- · 2020-05-20 03:16

I found a way to check the checkbox "Build when a change is pushed to Github".

This line did the trick:

properties([pipelineTriggers([[$class: 'GitHubPushTrigger'], pollSCM('H/15 * * * *')])])

I think the polling is needed to make it work. Would be nice if no polling is needed.

Here's a Jenkinsfile example with this implemented:

#!/usr/bin/env groovy

node ('master'){
    stage('Build and Test') {
        properties([pipelineTriggers([[$class: 'GitHubPushTrigger'], pollSCM('H/15 * * * *')])])
        checkout scm
        env.PATH = "${tool 'Maven 3'}/bin:${env.PATH}"
        sh 'mvn clean package'
    }
}
查看更多
神经病院院长
7楼-- · 2020-05-20 03:19

If you use Stash for example you can register a Post-Receive WebHook where you have to insert your URL form Jenkins like : http://jenkinsHost:9090/git/notifyCommit?url=ssh://git@gitHost:1234/test.git

In your jenkins Job you have to set at least the Build trigger "Poll SCM". And set a polling time of e.g 5 mins. This enables also the automatic branch indexing for your multibranch project configuration.

查看更多
登录 后发表回答