0

I'm trying to integrate Webhook with gitlab and jenkins. I have done it via upstream downstream jobs using the URL. While trying to rece=reate the same via declarative pipeline, I'm in a stanstill

 pipeline {
  agent any
  stages {
    stage('fetchcodeFromGit') {
      steps {
        timeout(time: 30) {
          git(url: 'http:<<>>/JenkinsPipeline.git', branch: 'master', credentialsId: 'QualityAssurance', poll: true)
        }

      }
    }

Can anyone help with documents or any sample snippets?

ARJUN U
  • 63
  • 1
  • 10
  • Your approach is clone the git repository at regular intervals? This is some "outdated". I suggest you use webhooks : https://shadow-soft.com/jenkins-jobs-with-gitlab-webhooks/ – JRichardsz Jul 09 '18 at 14:10
  • @JRichardsz: I want to add a webhook. But I'm not able to figure out, how to add it in declrarative pipeline syntax. – ARJUN U Jul 10 '18 at 09:37
  • Check this : https://stackoverflow.com/a/46616144/3957754 in which they talk about declarative vs scripted pipeline. Im a scripted pipeline lover. If you change to scripted pipeline, you just need (1) install some webhook plugin (2) configure webhook in your git platform (3) create some job to be triggered and execute some commands java, python, nodejs, etc. I can help you with configurations – JRichardsz Jul 10 '18 at 14:23
  • I have the webhook plugin installed. I have been using it in upstream-downstream jobs. But I'm unable to find any syntax to integrate that with the pipeline scripts. In the blue ocean, I only get the polling option for Git. Any sample snippet or something is there for reference? – ARJUN U Jul 11 '18 at 05:51
  • If you are using pipeline scripts , It should not be complicated – JRichardsz Jul 11 '18 at 17:16
  • @JRichardsz : do you have any code samples? – ARJUN U Jul 23 '18 at 04:36
  • I don't have declarative pipeline examples. Sorry – JRichardsz Jul 23 '18 at 14:21

2 Answers2

1

If you choose pipeline script instead declarative pipeline, this post could be help you :

https://jrichardsz.github.io/devops/devops-with-git-and-jenkins-using-webhooks

Steps:

  • Configure required plugins in jenkins.
  • Jenkins user and password.
  • Create a jenkins job which will be triggered by your git provider. This job publish an http url ready to use. We will call webhook_url to this url.
  • Configure webhook_url in webhook section of your git provider of some repository.
  • Test this flow, pushing some change to your git repository or simulating it using comandline.
JRichardsz
  • 14,356
  • 6
  • 59
  • 94
1

You can use this snippet:

pipeline {

options {
    gitLabConnection('your-gitlab-conn')
}

triggers {
    gitlab(
      triggerOnPush: false,
      triggerOnMergeRequest: true, triggerOpenMergeRequestOnPush: "both",
      triggerOnNoteRequest: true,
      noteRegex: "Jenkins please retry a build",
      skipWorkInProgressMergeRequest: false,
      ciSkip: false,
      setBuildDescription: true,
      addNoteOnMergeRequest: true,
      addCiMessage: true,
      addVoteOnMergeRequest: true,
      acceptMergeRequestOnSuccess: false,
      branchFilterType: "All",
      secretToken: "NOTVERYSECRET")
}

stages {
    ...

more details here: https://github.com/jenkinsci/gitlab-plugin

Vano
  • 1,416
  • 1
  • 13
  • 26