Trigger workflow on Github push - Pipeline plugin

2020-05-20 02:52发布

问题:

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?

回答1:

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'
    }
}


回答2:

For declarative pipelines, try this:

pipeline {
    agent any
    triggers {
        pollSCM('') //Empty quotes tells it to build on a push
    }
}


回答3:

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.



回答4:

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.



回答5:

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:

Before I start, I would like to emphasize that I had no previous experience with Jenkins so far, so there might be a bunch of better solutions out there.

What I wanted to achieve in a nutshell:

  • After every push made to a Bitbucket repo(test2), on every branch,
    pull and build another Bitbucket repo(test1), from an identical
    branch name and right after that, build test2 using test1 as a
    dependency.

How I managed to achieve that?

  • I started a new job with type 'Multibranch Pipeline'
  • I added the following Jenkinsfile to test2:

pipeline {
    agent any
    stages {
        stage('build') {
            steps {
                dir('test1') {
                    git branch: BRANCH_NAME, url: 'git@bitbucket.org:user/test1.git', credentialsId: 'credentials_id'
                }
                sh('build_process')
            }
        }
    }
}

  • I come across the issue that you can't set up a Bitbucket hook for pipelines

  • I added Bitbucket Branch Source Plugin to Jenkins

  • I selected Bitbucket at 'Branch Sources' when setting up the job

  • I added credentials and put a checkmark to Auto-register webhook

  • Under 'Scan Multibranch Pipeline Triggers' I put a checkmark to Periodically if not otherwise run, with an interval of 1 min

  • I added a webhook to my Bitbucket repo

  • I updated all my plugins, restarted Jenkins and it's ready to go

Other plugins I have installed: Bitbucket Plugin, Pipeline plugin. Hope this helps for somebody, I did manage to solve it this way after hours of struggling without the Bitbucket Branch Source Plugin.



回答7:

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'
}

}