16
votes

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.

4
What version of Gitlab-plugin do you have in Jenkins? With jenkins version 2.1.60, gitlab plugin 1.5.11 and gitlab version 11.6.3-ce your DSL creates the multibranch job and the branch is built when pushing. Try to create one of the repos with Open Blue Ocean plugin and see what happens. By the way check stackoverflow.com/questions/50914104/…. It seems merge requests are not supported by the plugin yet.Carlos Cavero
There is an open MR to allow MR for multibranch pipelines github.com/jenkinsci/gitlab-plugin/pull/857Carlos Cavero
Could you share also your GitLab webhook?Carlos Cavero

4 Answers

3
votes

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.

3
votes

Possibly you have configured Basic Branch Build Strategies to only include specific branches e.g. using Exact Name: master which would skip the branch ci from your example.

Ensure that your Jenkins branch build configuration covers the branches that you are testing for. Also make sure that Suppress automatic SCM triggering option is not set.

Do note that settings on Organization or Folder level will influence the specific Project and Job settings, unless they are specifically overridden on lower level.

3
votes

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!

1
votes

We were facing similar (not the same) issues with gitlab and Jenkins and the problem was related with the credentials.

In Jenkins, we created a new global Access token for GitLab (Jenkins Configuration -> Credentials -> System -> New Global access -> Define a gitlab token). That generates a Token that we added it to the Webhook, now the hook being something like:

http://[Gitlab User]:[token ID]@Jenkins Address

I hope it helps