Publish Jenkins Job build status to Gitlab commit from Jenkins - 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.

Related

Triggering a Jenkins job from another Jenkins job, without remote URL Curl

I'm working on building a Jenkins job that will trigger a pipeline to build a new AMI in one group, when a Golden AMI in another group becomes available. I need a way for a Jenkins job to initiate another Jenkins job without using the remote build URL method, because in the environment I'm working in it's not feasible to keep them all updated.
I've seen some things about plug-ins, but I'm a little fuzzy about which should be used here. Is there one that's recommended? Or is there a way to script this without the remote URL?
You can use build job to start an existing pipeline in Jenkins.
It's part of the Build Step plugin.
A simple example:
pipeline {
agent {
node { label 'nuc3' }
}
stages {
stage('Run External Jobs') {
steps {
build job: 'FOLDER_TEST_JOBS/test_job1_located_under_folder', wait: false
sleep(60)
build job: 'test_job_without_folder_path', wait: false
}
}
}
}

RE: Updating Choice Parameter in Deploy Job from the post actions of Build Job without building it

I have two pipeline jobs (one for Build and another for Deployment) in jenkins and deployment job builds on one choice parameter which needs values(tags) from docker hub.
What we want to do is, To just update the choice field of deploy job from the build job post actions with out building it. Is that possible anyway?
I don't think that's possible at the moment. You need to run a build to update the parameters.
There are some tickets for this on the Jenkins Jira: JENKINS-52939 JENKINS-50365. The latter is marked as "Fixed but unreleased", but it doesn't appear to be actually fixed yet.
One comment mentions a workaround. You can put this in your pipeline to update the parameters and then abort the build:
stages {
stage("parameterizing") {
steps {
script {
if ("${params.Invoke_Parameters}" == "Yes") {
currentBuild.result = 'ABORTED'
error('DRY RUN COMPLETED. JOB PARAMETERIZED.')
}
}
}
}
}

GitLab pipelines for merge requests in Jenkins

I'm working on a CI integration between GitLab and Jenkins where I want to do specific validation depending on the target branch of an opened merge request (MR).
To be more specific, we have a repository containing Flyway database migrations and we'd like to validate inside a Docker container whether the migrations can be applied successfully against different environments. The target environment can be deduced by the target branch of a merge request.
Now, my problem is this. I'd like to reject MRs that don't build successfully, but I couldn't find way of rejecting MRs, only commits.
The way our integration is set up is that Jenkins receives webhook notifications for both commit and MR events. So, when someone pushes a new commit to an MR, two Jenkins builds will be triggered — one for the whole MR and one for the commit. The MR build will fail, but the commit build will succeed and due to race conditions in how the builds are run, the MR pipeline in GitLab will be marked as successful although it shouldn't be.
Is there a way to mark merge requests as failed in a Jenkins-GitLab integration?
I believe this feature is called pipelines for merge requests in GitLab's built-in CI/CD support, but can it be simulated with Jenkins?
Here's a sketch of the Jenkinsfile I'm using:
def isMergeRequest() {
gitlabActionType == 'MERGE' && gitlabTargetBranch != gitlabSourceBranch
}
pipeline {
agent any
stages {
stage('Validate') {
when {
expression {
isMergeRequest()
}
}
steps {
gitlabCommitStatus('Validate') {
sh "jenkins/validate_mr.py $gitlabTargetBranch $gitlabSourceBranch"
}
}
}
}
}

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.

Github Pull Request Builder not working with DSL plugin

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.

Resources