jenkinsci / gitlab-plugin

A Jenkins plugin for interfacing with GitLab
https://plugins.jenkins.io/gitlab-plugin/
GNU General Public License v2.0
1.44k stars 613 forks source link

Example configuration for building MRs when Pipeline definition comes from SCM #914

Open beikov opened 5 years ago

beikov commented 5 years ago

Hey there, I was previously very happy with a freestyle job configuration that built every MR and published the results back to Gitlab. Now I switched to using a Jenkins Pipeline script and MR changes do not trigger a Jenkins build anymore. If I go to Gitlab > Settings > Integrations > Jenkins CI and "Test and save settings", jobs for all open MRs are triggered, but no jobs are started when I do a change to any of the MRs. I see that the webhook is called, it just doesn't start the job.

I read a bit about problems with Jenkinsfiles in the SCM and other issues, but I couldn't figure out the definitive configuration that I need to get this to work.

I'm using

Here is the config I'm using

jenkins-gitlab-config jenkins-gitlab-git-config

Please help me find out what is necessary to be able to build every MR and also every commit pushed to master. The same configuration worked fine for freestyle jobs but not just doesn't trigger a job when changing anything on the MR.

Here is a log entry that is produced when the webhook ist triggered due to a change on an MR

Apr 02, 2019 7:42:08 AM INFO com.dabsquared.gitlabjenkins.webhook.GitLabWebHook getDynamic
WebHook called with url: /project/clevercure-pipeline
Apr 02, 2019 7:42:08 AM FINE com.dabsquared.gitlabjenkins.webhook.build.MergeRequestBuildAction
MergeRequest: {
  "object_kind" : "merge_request",
  "user" : {
    "name" : "Christian Beikov",
    "username" : "..",
    "avatar_url" : ".."
  },
  "project" : {
    "id" : 17,
    "name" : "..",
    "description" : "",
    "web_url" : "..",
    "avatar_url" : null,
    "git_ssh_url" : "..",
    "git_http_url" : "..",
    "namespace" : "..",
    "visibility_level" : 0,
    "path_with_namespace" : "..",
    "default_branch" : "master",
    "ci_config_path" : null,
    "homepage" : "..",
    "url" : "..",
    "ssh_url" : "..",
    "http_url" : ".."
  },
  "object_attributes" : {
    "assignee_id" : null,
    "author_id" : 21,
    "created_at" : "2019-04-01 08:37:56 UTC",
    "description" : "Closes SRM-4936",
    "head_pipeline_id" : 3611,
    "id" : 977,
    "iid" : 934,
    "last_edited_at" : "2019-04-02 07:42:07 UTC",
    "last_edited_by_id" : 8,
    "merge_commit_sha" : null,
    "merge_error" : null,
    "merge_params" : {
      "force_remove_source_branch" : "1"
    },
    "merge_status" : "can_be_merged",
    "merge_user_id" : null,
    "merge_when_pipeline_succeeds" : false,
    "milestone_id" : null,
    "source_branch" : "SRM-4936_Quickfilter",
    "source_project_id" : 45,
    "state" : "opened",
    "target_branch" : "master",
    "target_project_id" : 17,
    "time_estimate" : 0,
    "title" : "SRM-4936 \"Quickfilter\" test",
    "updated_at" : "2019-04-02 07:42:07 UTC",
    "updated_by_id" : 8,
    "url" : "..",
    "source" : {
      "id" : 45,
      "name" : "..",
      "description" : "",
      "web_url" : "..",
      "avatar_url" : null,
      "git_ssh_url" : "..",
      "git_http_url" : "..",
      "namespace" : "..",
      "visibility_level" : 0,
      "path_with_namespace" : "..",
      "default_branch" : "master",
      "ci_config_path" : null,
      "homepage" : "..",
      "url" : "..",
      "ssh_url" : "..",
      "http_url" : ".."
    },
    "target" : {
      "id" : 17,
      "name" : "..",
      "description" : "",
      "web_url" : "..",
      "avatar_url" : null,
      "git_ssh_url" : "..",
      "git_http_url" : "..",
      "namespace" : "..",
      "visibility_level" : 0,
      "path_with_namespace" : "..",
      "default_branch" : "master",
      "ci_config_path" : null,
      "homepage" : "..",
      "url" : "..",
      "ssh_url" : "..",
      "http_url" : ".."
    },
    "last_commit" : {
      "id" : "7e4fa53dc1f39ebaf8a17e091ef650ac7202689a",
      "message" : "Update pom.xml",
      "timestamp" : "2019-04-01T16:57:27+00:00",
      "url" : "..",
      "author" : {
        "name" : "Christian Beikov",
        "email" : "c.beikov@curecomp.com"
      }
    },
    "work_in_progress" : false,
    "total_time_spent" : 0,
    "human_total_time_spent" : null,
    "human_time_estimate" : null,
    "action" : "update"
  },
  "labels" : [ ],
  "changes" : {
    "last_edited_at" : {
      "previous" : "2019-04-01 17:55:19 UTC",
      "current" : "2019-04-02 07:42:07 UTC"
    },
    "title" : {
      "previous" : "SRM-4936 \"Quickfilter\"",
      "current" : "SRM-4936 \"Quickfilter\" test"
    },
    "updated_at" : {
      "previous" : "2019-04-01 17:55:19 UTC",
      "current" : "2019-04-02 07:42:07 UTC"
    }
  },
  "repository" : {
    "name" : "..",
    "url" : "..",
    "description" : "",
    "homepage" : ".."
  }
}
bjakubiak commented 5 years ago

It looks like running Jenkinsfile from SCM overwrites trigger settings, so they are gone after you run the job once. One way to go is to define pipeline code inside the job (maybe with DSL) which then starts standard Jenkinsfile, like there: https://blog.ljdelight.com/gitlab-trigger-jenkins-builds-on-merge-request/