Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab webhook does not trigger a build on jenkins

I've a group of multibranch pipeline jobs generated with the following piece groovy script:

[
      'repo1',
      'repo2',
].each { service ->

  multibranchPipelineJob(service) {

    displayName(service)

    branchSources {
      git {
        remote("[email protected]:whatever/${service}.git")
        credentialsId('gitlab-ssh-key')
      }
    }

    orphanedItemStrategy {
      discardOldItems {
        daysToKeep(0)
        numToKeep(30)
      }
    }

    triggers {
      periodic(5)
    }

  }
}

and in each repo a Jenkinsfile that looks as follows:

#!/usr/bin/env groovy

properties([
      gitLabConnection('[email protected]'),
      pipelineTriggers([
            [
                  $class               : 'GitLabPushTrigger',
                  triggerOnPush        : true,
                  triggerOnMergeRequest: true,
            ]
      ]),
      disableConcurrentBuilds(),
      overrideIndexTriggers(false)
])

node {

  def sbtHome = tool name: 'sbt-0.13.15', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder\$SbtInstallation'

  stage('Checkout') {
    checkout scm
  }

  stage('Build') {
    sh "'${sbtHome}/bin/sbt' clean compile"
  }

  stage('Test') {
    sh "'${sbtHome}/bin/sbt' test"
  }

  if (env.BRANCH_NAME == 'develop' || env.BRANCH_NAME == 'master') {
    stage('Publish') {
      sh "'${sbtHome}/bin/sbt' publish"
    }
  }
}

It all works correctly. The seeder project generates all the folders from the first script and all the branches for given repo are built correctly.

Unfortunately I've problems with triggering a build for any branch after commit + push has been made to gitlab.

I've jenkins configured correctly - I mean the gitlab plugin, there is a connection and it all works well.

I've also added a webhook on the gitlab side and it also runs correctly. After a test push is sent I receive 200 OK from jenkins and I do see in logs that scanning the branches has started and detected the changes correctly. Unfortunately the build for the changed branch does not start. Here's an extract from branch scan log:

  Checking branch ci
      ‘Jenkinsfile’ found
    Met criteria
Changes detected: ci (a7b9ae2f930b0b10d52bb42f1ecf96a68bba4a30 → 39a4c1a65051d5e90079feec14ad22455a77c58e)
Did not schedule build for branch: ci

I'm 100% sure that this not a problem with communication between my jenkins instance and gitlab account. I see the webhook being triggered after push to gitlab, I see the request being send and branch scan being run. Changes are also detected but why on earth the job isn't started? I've also read the docs thoroughly and have it all configured correctly.

Jenkins version: 2.150.3
Gitlab version: 11.8.1-ee

EDIT

It seems that after upgrading jenkins to v.2.164.1 it all started working correctly.

like image 420
Opal Avatar asked Mar 02 '19 09:03

Opal


People also ask

How do I use GitLab Webhooks with Jenkins?

Step 1: Go the the “Settings” of your Jenkins project. Step 2: Go to the “Build Triggers” section. Step 3: Under the “Build when a change is pushed to Gitlab” checkbox, click the “advanced” button. Step 4: Click the “Generate” button under the “Secret Token” field.

How do you trigger Jenkins build from GitLab?

Go to Manage Jenkins -> Configure System and scroll down to the 'GitLab' section. Write a custom connection name, enter the GitLab server URL in the 'GitLab host URL' field and click Add -> Jenkins button. Then, fill required fields as shown below with the Jenkins Access API Token which we created in GitLab before.

How do I trigger a build automatically in Jenkins?

Follow the steps as mentioned below to trigger a Jenkins job automatically based on GitHub's webhook configurations: Step 1: Go to the Configuration page of the respective job and under the build trigger section, check the "GitHub hook trigger for GITScm polling" checkbox and click on the Save button.


2 Answers

I found this very useful Setup Example (Continuous Integration with Jenkins and GitLab) . Especially the part Source Code management:

We need to specify the name as “origin”, which will be used by the other sections. For the Refspec we need to input: +refs/heads/*:refs/remotes/origin/* +refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*

And also:

Branch Specifier we need origin/${gitlabSourceBranch} which will be filled in based on the web hook we’ll be setting up next.


Edit1

You could try the following for one multibranch pipeline:

  1. Select a branch, for example ci
  2. Select "View Configuration"
  3. Under "Build Triggers" select the checkbox "Build when a change is pushed to GitLab"
  4. Make some changes to the code and push to ci

Edit2

I could not find a suitable git-project to run and try to reproduce this behaviour. So if someone know a similar project and could share, please comment and I could do some more testing.

For Gitlab (requested a trial key, otherwise it will be a GitLab Community Edition):

sudo docker run --detach --hostname gitlab.example.com --publish 443:443 --publish 80:80 --publish 22:22 --name gitlab --restart always --volume /srv/gitlab/config:/etc/gitlab --volume /srv/gitlab/logs:/var/log/gitlab --volume /srv/gitlab/data:/var/opt/gitlab gitlab/gitlab-ee:11.8.1-ee.0

For Jenkins:

sudo docker run  -u root  --rm  -d  -p 8080:8080  -p 50000:50000  -v jenkins-data:/var/jenkins_home  -v /var/run/docker.sock:/var/run/docker.sock  jenkins/jenkins:2.150.3

Then "Integration" —> "Jenkins CI" in Gitlab as in this image:enter image description here

Hope this can help you!

like image 70
Anton Bärwald Avatar answered Nov 15 '22 19:11

Anton Bärwald


I believe you need to use includes() to specify pattern(s) identifying which branches will be included:

branchSources {
  git {
    remote("[email protected]:whatever/${service}.git")
    credentialsId('gitlab-ssh-key')
    includes('ci')
  }
}

You can specify a number of patterns, which can include wildcards. For example:

includes("master release/* feature/* bugfix/*")

There is also a corresponding excludes() for even finer-grained control.

like image 40
cody Avatar answered Nov 15 '22 19:11

cody