trigger open source jenkins job for a github merge - jenkins

This is our tool set:
jenkins: Open source Jenkins running on AWS server - 2.107.2
Github: 2.13.3 version
Requirement:
We need to create a GitHub hook in such a way that, if anyone merges the branch (create pull request and merge it), then it should trigger the Jenkins job.We do not use multibranch pipeline, we use just pipeline jobs i.e we use jenkisnfile to run our jobs.
so, i have enabled the "PullRequestEvent" in github webhooks. Then i have enabled scm polling in Jenkins job for that particular job.But, the issue is the job is getting triggered for a commit to the xyz branch and also if we merge the pull request. But, the expected behavior is, the job should be triggered only for the merge.
polling log :
This page captures the polling log that triggered this build.
Started on Jul 17, 2018 2:04:00 PM
Using strategy: Default
[poll] Last Built Revision: Revision xyzz (origin/development)
> /app/digital/tools/git/bin/git --version # timeout=10
using GIT_ASKPASS to set credentials Githubread id generated on 20th Jan 2017
> /app/digital/tools/git/bin/git ls-remote -h https://github.xyz.net /abc/Test-SourceCode-Jenkins # timeout=10
Found 3 remote heads on https://github.xyz.net/abc/Test-SourceCode-Jenkins
[poll] Latest remote head revision on refs/heads/development is: xyzz
Done. Took 1.8 sec
Changes found
jenkins configuration:

Related

How to poll scm using Jenkins for release tags

I need a help to poll scm using Jenkins for release tags. Webhook trigger is not working as the Jenkins resides behind the firewall.
Jenkins build has to be triggered if there is a release/release tag is committed/pushed under https://github.com/<user>/<repo>/releases
I have configured the below under Source Code Management tab in Jenkins.
Git ->
Under Repositories, given Repository URL and Credentials. Also under "Advanced" for "Refspec" configured the values as:
+refs/tags/*:refs/remotes/origin/tags/*
Also for Branches to build -> /tags/
Configured SCM poll to run for every 5 minutes. Under "Git Polling Log", its polling every 5 minutes and bringing the release details based on the tag. But the Jenkins job is not triggered.Can anyone please help me to address this issue?
From Git Polling Log:
Started on 13 Oct, 2020 4:42:00 PM
Polling SCM changes on master
Using strategy: Default
[poll] Last Built Revision: Revision 2f3a23888bdce0b4ac63044c00f71adedc0aa175 (origin/tags/fb-bin1, origin/tags/15thRelease, origin/tags/9thRelease, origin/tags/17thRelease, origin/tags/14thRelease, origin/tags/11thRelease, origin/tags/20thRelease, origin/tags/8thRelease, origin/tags/13thRelease, origin/tags/10thRelease, origin/tags/21stRelease, origin/tags/19thRelease, origin/tags/12thRelease, origin/tags/16thRelease, refs/tags/fb-bin1, refs/tags/15thRelease, refs/tags/13thRelease, refs/tags/8thRelease, refs/tags/19thRelease, refs/tags/16thRelease, refs/tags/12thRelease, refs/tags/17thRelease, refs/tags/9thRelease, refs/tags/21stRelease, refs/tags/11thRelease, refs/tags/10thRelease, refs/tags/20thRelease, refs/tags/14thRelease)
using credential 7cbf657e-010a-40fd-ad76-0813e675c188
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repositories
> git config remote.origin.url https://github.com/<user>/repo.git # timeout=10
Fetching upstream changes from https://github.com/<user>/repo.git
> git --version # timeout=10
using GIT_SSH to set credentials
> git fetch --tags --progress https://github.com/<user>/repo.git +refs/tags/*:refs/remotes/origin/tags/* # timeout=10
Polling for changes in
Seen branch in repository origin/bin
Seen branch in repository origin/master
Seen branch in repository origin/tags/10thRelease
Seen branch in repository origin/tags/11thRelease
Seen branch in repository origin/tags/12thRelease
Seen branch in repository origin/tags/13thRelease
Seen branch in repository origin/tags/14thRelease
Seen branch in repository origin/tags/15thRelease
Seen branch in repository origin/tags/16thRelease
Seen branch in repository origin/tags/17thRelease
Seen branch in repository origin/tags/19thRelease
Seen branch in repository origin/tags/20thRelease
Seen branch in repository origin/tags/21stRelease
Seen branch in repository origin/tags/22ndRelease
Seen branch in repository origin/tags/23rdRelease
Seen branch in repository origin/tags/24thRelease
Seen branch in repository origin/tags/2ndLatest
Seen branch in repository origin/tags/3rdRelease
Seen branch in repository origin/tags/4thRelease
Seen branch in repository origin/tags/5thRelease
Seen branch in repository origin/tags/6thRelease
Seen branch in repository origin/tags/7thRelease
Seen branch in repository origin/tags/8thRelease
Seen branch in repository origin/tags/9thRelease
Seen branch in repository origin/tags/Latest
Seen branch in repository origin/tags/fb-bin1
Seen 26 remote branches
> git show-ref --tags -d # timeout=10
Done. Took 1.6 sec
No changes
Thanks in advance!

Multibranch job scan fails to discover remote GitHub repo tags

I'm using a Multibranch pipeline job to discover branches/tags/PRs and execute certain jobs. I got repos on GitHub and scan is able to discover all but not tags. I get below error. Also, when the Discover tags option is disabled in multibranch job configuration, I don't see this error and I miss the build when tags are created.
I tried to create multiple new repos but it did not help.
Jenkins version: 2.150.1
Getting remote tags...
ERROR: [Sun Jan 06 16:00:21 UTC 2019] Could not fetch branches from source 3f765a8f-ee7f-4c6d-a655-f9ca3b2b25d3
org.kohsuke.github.GHException: Failed to retrieve https://repourl/branch/git/refs/tags
at org.kohsuke.github.Requester$PagingIterator.fetch(Requester.java:529)
at org.kohsuke.github.Requester$PagingIterator.hasNext(Requester.java:494)
at org.kohsuke.github.PagedIterator.fetch(PagedIterator.java:44)
at org.kohsuke.github.PagedIterator.hasNext(PagedIterator.java:32)
at org.jenkinsci.plugins.github_branch_source.GitHubSCMSource$LazyTags$1$1.hasNext(GitHubSCMSource.java:2222)
at org.jenkinsci.plugins.github_branch_source.GitHubSCMSource.retrieve(GitHubSCMSource.java:1016)
at jenkins.scm.api.SCMSource._retrieve(SCMSource.java:374)
at jenkins.scm.api.SCMSource.fetch(SCMSource.java:284)
at jenkins.branch.MultiBranchProject.computeChildren(MultiBranchProject.java:634)
at com.cloudbees.hudson.plugins.folder.computed.ComputedFolder.updateChildren(ComputedFolder.java:277)
at com.cloudbees.hudson.plugins.folder.computed.FolderComputation.run(FolderComputation.java:165)
at jenkins.branch.MultiBranchProject$BranchIndexing.run(MultiBranchProject.java:1025)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
[Sun Jan 06 16:00:21 UTC 2019] Finished branch indexing. Indexing took 0.42 sec
FATAL: Failed to recompute children of Pipelines ยป bl-calibration-orchestrator-v1
org.kohsuke.github.GHException: Failed to retrieve https://repourl/branch/git/refs/tags
at org.kohsuke.github.Requester$PagingIterator.fetch(Requester.java:529)
at org.kohsuke.github.Requester$PagingIterator.hasNext(Requester.java:494)
at org.kohsuke.github.PagedIterator.fetch(PagedIterator.java:44)
at org.kohsuke.github.PagedIterator.hasNext(PagedIterator.java:32)
at org.jenkinsci.plugins.github_branch_source.GitHubSCMSource$LazyTags$1$1.hasNext(GitHubSCMSource.java:2222)
at org.jenkinsci.plugins.github_branch_source.GitHubSCMSource.retrieve(GitHubSCMSource.java:1016)
at jenkins.scm.api.SCMSource._retrieve(SCMSource.java:374)
at jenkins.scm.api.SCMSource.fetch(SCMSource.java:284)
at jenkins.branch.MultiBranchProject.computeChildren(MultiBranchProject.java:634)
at com.cloudbees.hudson.plugins.folder.computed.ComputedFolder.updateChildren(ComputedFolder.java:277)
at com.cloudbees.hudson.plugins.folder.computed.FolderComputation.run(FolderComputation.java:165)
at jenkins.branch.MultiBranchProject$BranchIndexing.run(MultiBranchProject.java:1025)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
That looks like a recent Jenkins issue (JENKINS-52397) which is still pending:
Org Scan blows up when repository has no tags
Given a GitHub Organization Folder that has the "Discover Tags" behavior; the scan blows up on every repository that doesn't have any tags.
Workaround of adding a single tag confirmed to work
This is linked to the JENKINS/GitHub Branch Source Plugin, and is still seen in Jenkins 2.152.
From the release notes of the Jenkins GitHub Branch Source Plugin:
Version 2.4.2
Release date: 2019-01-16
* JENKINS-52397: Org Scan blows up when repository has no tags #191
* INFRA-1934: Stop publishing to jenkinsci/jenkins repo on Docker Hub
ref: https://github.com/jenkinsci/github-branch-source-plugi/blob/b26aba6136024d4dfaafb9e2c36317128ceb82dd/CHANGELOG.md
It might depend on various factors if you received those plugin already. A Jenkins version of 2.160 (2019-01-16) or higher might or might not be a safe indicator for the fixed version of the plugin being at least available at setup time.

get git commit SHA within Jenkins pipeline with JGit

I have a Jenkins with the Github organization plugin that scans my organization and build all the branches/PRs of all the repositories within the organization that have a Jenkinsfile.
It works great, but I would like to retrieve for each build the commit SHA, in order to tag Docker images with both the branch name and the commit SHA.
Getting the branch name works great with env.BRANCH_NAME, however I cannot find any way to get the commit SHA.
The catch is that we are using JGit, so I cannot use git log to retrieve it.
I tried having a look at what is contained in the ENV using sh 'printenv', but there's nothing of any use there.
I also tried the following:
def checkoutResults = checkout scm
echo 'checkout results: ' + checkoutResults
but this yields the following result:
checkout results: [:]
Even though I cannot get the revision from my pipeline, Jenkins is getting it alright, as I can see in the logs:
...
Obtained Jenkinsfile from 98062e5f651ca698f4303c3bb8d20665ce491294
...
Checking out Revision 98062e5f651ca698f4303c3bb8d20665ce491294 (docker)
I am running the following versions:
Jenkins 2.73.3
Git plugin plugin 3.3.0
Pipeline: SCM Step plugin 2.6
Would appreciate any help in retrieving the commit SHA / revision in this particular situation.
The git plugin did not include the fix until the 3.3.2 release. You'll need to update to at least git plugin 3.3.2.
The current git plugin release as of 23 Nov 2017 is 3.6.4. It includes significant additions and changes for multi-branch pipelines.
There is also a known bug in the reporting of NAME and EMAIL values which has a regression test that confirms the bug affects all implementations (git and jgit). You can use that regression test as an example of using those values if needed.
If you cannot update from git plugin 3.3.0 to 3.3.2, you may be able to use the JGit classes from within the pipeline script to perform the same type of query as was offered with command line git in another answer to the question. I have never done it, but I believe it is possible.
I created a small groovy function
def getCommitSha(){
return sh(returnStdout: true, script: 'git rev-parse HEAD')
}
you can add it to your pipeline , or to your shared library if you are using one ( if not it's a good time to start ... :-) )

Jenkins gerrit trigger not fetching my change while building

I have configured jenkins with gerrit trigger plugin to validate every commit we push to gerrit.
I am expecting this trigger to include my latest change with original repo and make a build.
But, it is cloning only repo project and compiling without my change.
Below is my configuration settings for gerrit trigger in jenkins.
Refspec: $GERRIT_REFSPEC
Branches to build: $GERRIT_BRANCH
Build trigger: Gerrit event
Trigger on: patch set created
Gerrit project: added project and branch
Below is the build output message
Triggered by Gerrit: http://ci-test1/22
Building on master in workspace /var/lib/jenkins/jobs/Build_Adserver_4.7/workspace
Checkout:workspace / /var/lib/jenkins/jobs/Build_Adserver_4.7/workspace - hudson.remoting.LocalChannel#733aee56
Using strategy: Default
Last Built Revision: Revision 701a75ef38aa191ac1b806c48e6b3451671888f6 (ads/4.7)
Fetching changes from 1 remote Git repository
Fetching upstream changes from abc
Commencing build of Revision 701a75ef38aa191ac1b806c48e6b3451671888f6 (ads/4.7)
Checking out Revision 701a75ef38aa191ac1b806c48e6b3451671888f6 (ads/4.7)
[workspace] $ /bin/sh -xe /tmp/hudson1375188638196718521.sh
+ echo 'Started Build'
Started Build
+ echo ..................
..................
+ echo 'Build Finished'
Build Finished
Finished: SUCCESS
Here 701a75ef38aa191ac1b806c48e6b3451671888f6 is HEAD of repo branch and 8cbda558adcad4fb7eb714e0b3fb98a6fbf5811c is the SHA-id of my latest change trigged the build.
I verified from jenkins workspace also, it doesn't include my change.
sorry if I am missing any information to mention. Please let me know
please help me if I am missing anything here.
Using Jenkins 1.532.2 Git Client Plugin 1.6.2 Git Plugin 2.0.1 Git Trigger 2.11.0
Here are the steps for configuring the Gerrit Trigger (from memory, hopefully all works fine):
Install the plugin(s) "Gerrit Trigger", "Git Plugin" and "Git Client Plugin"
In the main jenkins config (HOME->Manage Jenkins), click on Gerrit Trigger.
Create the server and configure it. Use "Test Connection" to be sure it works.
At the end, under "Control" press "start" (No idea what that does or if it's actually needed, but I did that).
Go to your project's config (MYPROJECT->Configure)
Check "Gerrit event" under "Build Triggers"
In the newly added menu, select your server, your triggers, etc.
For Gerrit Project I used "Plain" with "MYPROJECT" as pattern
For Branch, I used "Path" and "**" as pattern (builds all branches)
Under "Source Code Management" (up from triggers in my UI), click on "Git"
Set the Repository URL, here $USER matches for me, but otherwise write the correct user $GERRIT_SCHEME://$USER#$GERRIT_HOST:$GERRIT_PORT/$GERRIT_PROJECT
Specify a branch: $GERRIT_BRANCH
Under "Repositories" on the right, click Advanced, for "Refspec" enter $GERRIT_REFSPEC
Click Add right below, and select "Strategy for choosing what to build"
Select "Gerrit Trigger"
Not very intuitive but it should work. I suggest making sure the correct SHA1 Ids are being built.
If run into Error stderr: fatal: Couldn't find remote ref $GERRIT_REFSPEC
You have to change the Choosing Strategy to Gerrit Trigger
Go to the configuration page of your job and then click on the 2nd Advanced button under the git section. Almost at the bottom there is a Choosing Strategy that you will need to change to Gerrit Trigger
This will cause Git to fetch the correct version for your build
Fixing small issues in Lewis answer, change the values to the following to ensure the latest SHA1 is built.
branch: $GERRIT_REFSPEC
REFSPEC: $GERRIT_REFSPEC:$GERRIT_REFSPEC
I am using Jenkins 2.15 and faced the Issue and got resolved with following settings.
In Git Advances add Refspec : $GERRIT_REFSPEC
Branches to build : $GERRIT_BRANCH.
In Addititional behaviors section select Strategy for choosing what to build and select gerrit Trigger.

Jenkins failure during trigger

When executing a job I made I got an error before the execute shell had been executed (so this error must come from Jenkins or Git).
Started by user anonymous
Building in workspace /var/lib/jenkins/workspace/Deploy example.com master branch
Checkout:Deploy example.com master branch / /var/lib/jenkins/workspace/Deploy example.com master branch - hudson.remoting.LocalChannel#51a745fc
Using strategy: Default
Last Built Revision: Revision 133257b4b34f8c0a90bf5ce11c634ebc9587d20d (origin/master)
Fetching changes from 1 remote Git repository
Fetching upstream changes from https://joachimroeleveld:fod4xvo0#bitbucket.org/joachimroeleveld/exofes_profiler.git
Commencing build of Revision 133257b4b34f8c0a90bf5ce11c634ebc9587d20d (origin/master)
Checking out Revision 133257b4b34f8c0a90bf5ce11c634ebc9587d20d (origin/master)
Triggering default
default completed with result FAILURE
Finished: FAILURE
What does ' default' mean in this case? Clicking on it results in a page with the links 'Workspace' and 'Recent changes'. Does this error come from a misconfiguration in Git (I use BitBucket for my remote repo's) or Jenkins?
You might be using multi-configuration job.
If this is the case try to configure "a free-style software project" first. The log will look like the following:
Started by an SCM change
Checkout:...
Using strategy: Default
Last Built Revision: Revision ...
Fetching changes from 1 remote Git repository
Fetching upstream changes from ...
Commencing build of Revision ...
Checking out Revision ...
[job-name] $ /bin/sh -xe /tmp/hudson7227596484536996852.sh

Resources