12

enter image description here I have created a jenkins pipeline job called "pipelinejob" with the below script:

pipeline {
    agent any
    
    stages {
        
        stage ('Setup'){
            steps{
                //echo "${BRANCH_NAME}"
                echo "${env.BRANCH_NAME}"
                //echo "${GIT_BRANCH}"
                echo "${env.GIT_BRANCH}"
            }
        }
    }
}
  1. Under General, I have selected "GitHub project" and inserted my company's github in the form:

https://github.mycompany.com/MYPROJECTNAME/MY_REPOSITORY_NAME/

  1. Under Build Triggers, i have checked "GitHub hook trigger for GITScm polling

  2. I have created a simple job called "simplejob" with same configuration as 1) and 2)

  3. In my company's Github, i have created a webhook like "jenkins_url/jenkins/github-webhook/"

  4. I commit a change in "mybranch" in "MY_REPOSITORY_NAME"

  5. My simple job "simplejob" is triggered and built successfully

  6. My pipeline job "pipelinejob" is not triggered

  7. In Jenkins log i see the below:

Sep 12, 2019 2:42:45 PM INFO org.jenkinsci.plugins.github.webhook.subscriber.DefaultPushGHEventSubscriber$1 run
Poked simplejob

Nothing regarding my "pipelinejob".

Could you please point me to the right directions as to what to check next?

P.S. I have manually executed my "pipelinejob" successfully

Dan Bonachea
  • 2,408
  • 5
  • 16
  • 31
Dim Malt
  • 121
  • 1
  • 1
  • 4

4 Answers4

20

I wasted two days of work on this, as none of the previous solutions worked for me. :-(
Eventually I found the solution on another forum:
The problem is that if you use a Jenkinsfile that is stored in GitHub, along with your project sources, then this trigger must be configured in the Jenkinsfile itself, not in the Jenkins or project configuration.
So add a triggers {} block like this to your Jenkinsfile:

pipeline {
  agent any
  triggers {
    githubPush()
  }
  stages {
...
  }
}

Then...

  • Push your Jenkinsfile into GitHub
  • Run one build manually, to let Jenkins know about your will to use this trigger.
    You'll notice that the "GitHub hook trigger for GITScm polling" checkbox will be checked at last!
  • Restart Jenkins.

The next push should trigger an automated build at last!

  • 1
    useful answer, although we don't need to build manually after adding trigger in jenkins file, neither do we need to restart (at least in my case) – Ajay Sharma Dec 01 '20 at 11:04
  • I had a failing job so I had to revert to a simple Jenkinsfile to get the job to pass first, don't know if that is the key - a passing job? – thoni56 Apr 27 '21 at 22:27
  • I now understand why I had to build manually once: It's my project branch source which was configured incorrectly: It was setup as a git branch source. After changing it to a github branch source, things started to behave much better :-) – Jean-François Larvoire Apr 30 '21 at 08:03
  • saved my day .. worked without need to any restart – Nurhun Jun 27 '21 at 00:22
  • Is the `githubPush` trigger documented anywhere? I can't seem to find it! – trebor Aug 12 '23 at 20:37
7

On the left side-pane of your pipeline job, click GitHub Hook log. If it says 'Polling has not run yet', you will need to manually trigger the pipeline job once before Jenkins registers it to poke on receiving hooks.

Henceforth, the job should automatically trigger on GitHub push events.

Dibakar Aditya
  • 3,893
  • 1
  • 14
  • 25
  • Thank you for your comment Dibakar. I have already built the job twice, and in the GitHub log it still says "Polling has not run yet". I have attached a pic – Dim Malt Sep 13 '19 at 07:51
  • @DimMalt Thanks for checking this Dim. What does the Jenkins system log (jenkins/log/all) say now after you push new changes to the repository? Does it indicate that hooks were received from GitHub? If you created the job by copying another job, also try deleting the problematic job and recreate it from the scratch (without copying from another job). – Dibakar Aditya Sep 13 '19 at 08:41
  • Also please verify that `.git` is not missing from the end of the repository URL where you have defined your SCM and the branches to build is specified in the format `/refs/heads/`. – Dibakar Aditya Sep 13 '19 at 08:49
  • Please see 8) in my original post. The hook from Github is received by Jenkins, but Jenkins only pokes my "simplejob" and not my "pipelinejob". Regarding your second comment, i will also add `.git` but regarding the branches to build, in a pipeline job there is no option for that. – Dim Malt Sep 13 '19 at 08:50
  • It must be under **Pipeline script from SCM**. – Dibakar Aditya Sep 13 '19 at 09:08
  • could you please ellaborate? – Dim Malt Sep 13 '19 at 09:59
  • You can find this setting in your job configuration page under **Pipeline** > **Definition** > **Pipeline script from SCM**. – Dibakar Aditya Sep 13 '19 at 10:51
  • Thanks again for your answer Dibakar. But what if i want to execute a pipeline script that is written inside the job in Jenkins, and not in a jenkinsfile in github/gitlab etc? – Dim Malt Sep 17 '19 at 08:35
  • @DimMalt I have the same issue. Have you found a solution? – Konstantin B. Mar 05 '20 at 14:21
  • @KonstantinB. I have the same issue. Have you guys found any solution? – Avtar Nanrey Jun 10 '20 at 20:27
3

I found an answer to this question with scripted pipeline file. We need to declare the Github push event trigger in Jenkins file as follows.

properties([pipelineTriggers([githubPush()])])

node {
        git url: 'https://github.com/sebin-vincent/Treasure_Hunt.git',branch: 'master'
        stage ('Compile Stage') {

            echo "compiling"
            echo "compilation completed"
        }

        stage ('Testing Stage') {

            echo "testing completed"
            echo "testing completed"
        }
        stage("Deploy") {

                echo "deployment completed"
                        }
            }
}

The declaration should be in the first line.

git url: The URL on which pipeline should be triggered.

branch: The branch on which pipeline should be triggered. When you specify the branch as master and make changes to other branches like develop or QA, that won't trigger the pipeline.

Hope this could help someone who comes here for an answer for the same problem with Jenkins scripted pipeline :-(.

sebin vincent
  • 330
  • 3
  • 12
  • 1
    Forgot to mention that, we need to warm the Jenkins by manually building it for the first time only after which it can perform the automatic triggering. – sebin vincent Oct 08 '20 at 04:56
-1

The thing is whenever you create a pipeline job for git push which is to be triggered by github-webhook, first you need to build the pipeline job manually for one time. If it builds successfully, then Jenkins registers it to poke on receiving hooks. And from the next git push, your pipeline job will trigger automatically.

Note: Also make sure that the pipeline job built manually for the first time should be built successfully, otherwise Jenkins will not poke it. If it fails to build, you can never trigger the job again.