Github Pull Request Builder not working with DSL plugin - jenkins

I have hosted a DummyProject2 on github and I want to setup a local CI for this using Jenkins. I have setup webhook triggers for everything on github. This is my seed job DSL script.
job('upstreamJob') {
scm {
git {
remote {
github('codernavi18/DummyProject2')
refspec('+refs/pull/*:refs/remotes/origin/pr/*')
}
}
}
steps {
shell('echo "This build is triggered from pull request"')
}
triggers {
githubPullRequest {
useGitHubHooks()
permitAll()
autoCloseFailedPullRequests()
displayBuildErrorsOnDownstreamBuilds()
}
}
}
When I raise a pull request on the project, my job upstreamJob is not getting triggered. What's wrong with my code?
PS : If i used a different plugin Github Integration Plugin then my builds are getting triggered properly based on the pull requests. This means my webhooks are fine and connection is also not the issue.

Related

Jenkins Pipeline not providing Bitbucket Server environment variables

How can I configure my Jenkins Pipeline project to provide the CHANGE_* variables related to a Bitbucket Server commit? The project's Pipeline definition is Pipeline script from SCM (Bitbucket Server Integration).
I have checked Bitbucket Server trigger build after push made available from the Bitbucket Server Integration Jenkins plugin and the build does get triggered, but the variables related to the commit/change message, author, author email, etc. are all missing.
pipeline {
agent any
stages {
stage("Hello variables") {
steps {
sh 'printenv'
}
}
}
}
The only Bitbucket related env variables are GIT_BRANCH, GIT_COMMIT, and GIT_URL.
The Bitbucket webhook (trigger) plugin does not provide a json payload.
If you want to get Bitbucket trigger json payload (and query it inside the the pipeline) you'll need to use the Generic Webhook Trigger

Jenkins Pipeline Multibranch doesn't run post steps if there is merge conflicts

I have a multibranch pipeline with the following behaviors:
And the following Jenkinsfile:
pipeline {
agent {
label 'apple'
}
stages {
stage('Lint') {
when {
changeRequest()
}
steps {
sh 'fastlane lint'
}
}
}
post {
success {
reportSuccess()
}
failure {
reportFailure()
}
}
}
I use a slave to run the actual build, but the master still needs to checkout the code to get the Jenkinsfile. For that, it seems to use the same behaviors as the one defined in the job even though it really only needs the Jenkinsfile.
My problem is that I want to discover pull requests by merging the pull request with the current target branch revision, but when there is a merge conflict the build will fail before the Jenkinsfile is executed. This prevents any kind of reporting done in post steps.
Is there a way to have the initial checkout not merge the target branch, but still have it merged when actually running the Jenkinsfile on a slave?
You may want to check out using "Current Pull Request revision" strategy, and then on a successful build issue a git merge command.

How to add webhooks in gitlab for multibranch pipeline jenkins

I want to trigger multi-branch pipeline for every push, can anyone please let me know can how can we configure web-hooks in gitlab for multi-branch pipeline.
If you were wondering where the trigger setting is in Multibranch pipeline job settings, this will answers it:
Unlike other job types, there is no 'Trigger' setting required for a Multibranch job configuration; just create a webhook in GitLab for push requests which points to the project's webhook URL.
Source: https://github.com/jenkinsci/gitlab-plugin#webhook-url
You can also provide Gitlab triggers within the Jenkinsfile. You can see examples within the link provided above. This is how I got it work:
pipeline {
agent {
node {
...
}
}
options {
gitLabConnection('GitLab')
}
triggers {
gitlab(
triggerOnPush: true,
triggerOnMergeRequest: true,
branchFilterType: 'All',
addVoteOnMergeRequest: true)
}
stages {
...
}
}
Then in your Gitlab Project go to Settings -> Integrations and type in the Jenkins Job project url in 'URL'. URL should take either form:
http://JENKINS_URL/project/PROJECT_NAME
http://JENKINS_URL/project/FOLDER/PROJECT_NAME
Notice that the url does not contain "job" within it and instead uses "project".
Make sure under Triggers, you have "Push Events" checked as well if you want the job to trigger whenever someone pushes a commit.
Finally, run a build against your Jenkinsfile first before testing the webhook so Jenkins will pick-up the trigger settings for Gitlab.

Publish Jenkins Job build status to Gitlab commit from Jenkins

I'm new to jenkins. I'm using Jenkins 2.77 and GitLab Enterprise Edition 9.5.5. I see a strange issue happening.
I've created 3 Jenkins freestyle jobs and have built a pipeline view for those jobs [1 ~> 2 ~> 3]. I am able to trigger builds with Gitlab webhook and publish the status of job 3 to Gitlab without any issues . All these were done manually.
I have to create these 3 jobs for about 100 projects. So I chose to generate these jobs via jenkins job DSL. I am able to generate the jobs and build a pipeline view. Also triggering the builds (for a particular project) from Gitlab webhook works fine. In the pipeline view I am able to see the builds too. But unfortunately due to some reason, I'm not getting feedback to Gitlab.
The strange thing I observed is, if open the "job 3" in jenkins and just click save without even changing anything, it auto-magically sends the feedback from the next builds.
I've no clue what is going on.
DSL of job 3:
job("SyntaxCheck") {
logRotator {
numToKeep(10)
artifactNumToKeep(10)
}
parameters {
stringParam('jobType', '')
}
scm {
git {
remote {
url('gitlab url to checkout the project')
branch('master')
credentials(*********)
extensions {
submoduleOptions {
recursive(true)
}
}
}
}
}
steps {
shell(puppetparser)
}
publishers {
gitLabCommitStatusPublisher {
name('syntaxcheck')
markUnstableAsSuccess(false)
}
}
}
Suggestions or workarounds would be appreciated.

Pushing to GitLab repo does not trigger the Jenkins build

gitlab plugin Version: 1.4.2
jenkins Version: 2.7.4
gitlab Version: GitLab Community Edition 8.11.4
I have followed the plugin documentation and setup the webhook accordingly
(https://github.com/jenkinsci/gitlab-plugin).
Added gitlab repo to the jenkins job, the connection test succeeds.
Building the jenkins job manually also succeeds (The code is fetched
from the repo correctly so no issues there)
Added the webhook for jenkins. Testing the webhook also succeeds
(returns HTTP200). But on the jenkins side. nothing happens as a
result of the test even after it was performed after a change to the
repo (the jenkins log and gitlab plugin log show no activity)
When I try to test the whole setup. I make a new push to the gitlab
repo to see if it triggers a new build on jenkins. But nothing
happens. Can anybody help me out with this? I am not sure what is
wrong here as both the test hook and test gitlab connection show
success.
Thankyou in advance.
Naveed
In Jenkins you install and configure (global and job) Gitlab Hook Plugin
in your webhook can you make this :
URL : http://your-jenkins-server/gitlab/notify_commit or http://your-jenkins-server/gitlab/build_now.
Trigger : you check Push Events
and try again
To trigger a specific job the URL is:
http://your-jenkins-server/gitlab/build_now/job_name
job_name is the name of the job created in jenkins
I followed the instructions here and everything worked quite well: https://github.com/jenkinsci/gitlab-plugin/wiki/Setup-Example. It is possible to give back the results of the jenkins job to GitLab pipelines.
You can also push back the results using the jenkins pipeline:
node {
gitlabBuilds(builds: ['Build', 'Test', 'QA']) {
stage('Build') {
gitlabCommitStatus(name: 'Build') {
sh "your execution"
}
}
// The rest of the stages here...
}
}

Resources