Gitlab webhook 不会触发对 jenkins 的构建 [英] Gitlab webhook does not trigger a build on jenkins

查看:159
本文介绍了Gitlab webhook 不会触发对 jenkins 的构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组使用以下 groovy 脚本生成的多分支管道作业:

<代码>['repo1','repo2',].each { 服务 ->多分支管道作业(服务){显示名称(服务)分支来源{混帐{远程(git@gitlab.com:whatever/${service}.git")credentialsId('gitlab-ssh-key')}}孤儿项目策略 {丢弃旧物品 {保留天数(0)numToKeep(30)}}触发{定期(5)}}}

在每个 repo 中都有一个 Jenkinsfile,如下所示:

#!/usr/bin/env groovy特性([gitLabConnection('ci@gitlab.com'),管道触发器([[$class : 'GitLabPushTrigger',triggerOnPush : 真,triggerOnMergeRequest:真,]]),disableConcurrentBuilds(),覆盖索引触发器(假)])节点{def sbtHome = 工具名称:'sbt-0.13.15',类型:'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'阶段(结帐"){结帐单片机}阶段(构建"){sh "'${sbtHome}/bin/sbt' 干净编译"}阶段('测试'){sh "'${sbtHome}/bin/sbt' 测试"}if (env.BRANCH_NAME == '开发' || env.BRANCH_NAME == 'master') {阶段(发布"){sh "'${sbtHome}/bin/sbt' 发布"}}}

一切正常.seeder 项目从第一个脚本生成所有文件夹,并且正确构建了给定 repo 的所有分支.

不幸的是,在向 gitlab 提交 + 推送后,我无法触发任何分支的构建.

我已经正确配置了 jenkins - 我的意思是 gitlab 插件,有一个连接并且一切正常.

我还在 gitlab 端添加了一个 webhook,它也可以正常运行.发送测试推送后,我收到来自 jenkins 的 200 OK 并且我确实在日志中看到扫描分支已经开始并正确检测到更改.不幸的是,更改分支的构建没有开始.这是分支扫描日志的摘录:

 检查分支 ci找到Jenkinsfile"符合标准检测到的变化:ci (a7b9ae2f930b0b10d52bb42f1ecf96a68bba4a30 → 39a4c1a65051d5e90079feec14ad22455a77c58e)没有为分支安排构建:ci

我 100% 确定这不是我的 jenkins 实例和 gitlab 帐户之间的通信问题.我看到在推送到 gitlab 后触发了 webhook,我看到请求正在发送并且分支扫描正在运行.也检测到更改,但 为什么工作没有开始? 我还阅读了

希望对你有帮助!

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("git@gitlab.com: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('ci@gitlab.com'),
      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.

解决方案

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:

Hope this can help you!

这篇关于Gitlab webhook 不会触发对 jenkins 的构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆