A, Overviews

Jenkins’ Gitlab plug-in can receive webhooks triggered by Gitlab warehouse code events to trigger the construction of Jenkins Job/Pipeline, and can synchronize the construction status to Gitlab.

Plugin Github: github.com/jenkinsci/g…

Using this plug-in, you can achieve the following effects:



Second, the installation

Jenkins -> System Administration -> Plugins Management -> Available plugins -> Search for “gitlab”

The plug-in download address: plugins. Jenkins. IO/gitlab c-plug…

Three, configuration,

1. Security authentication configuration when Gitlab sends Webhook to Jenkins

The Token of Jenkins Gitlab plug-in is required to encrypt and verify the security of WebHook when configuring Web Hook in Gitlab code repository

Method 1: Authenticate the Token globally

Jenkins create new user (grant Job/Build permission)

② Obtain the User ID and API Token of the new User

③ When Gitlab configures web hook to fill in URL, use http://USERID:APITOKEN @jenkins_URL /project/YOUR_JOB (Secret Token can be ignored).

Method 2 (Recommended) : Authentication Token for Jenkins Job

When configuring Webhook in Gitlab, simply fill in the Token in the figure

Method 3 (not recommended) : Do not use authentication

Manage Jenkins -> Configure System -> GitLab section -> uncheck Enable authentication for ‘/project’ end-point

2. Jenkins writes back the authentication and authorization configuration required when the build state is written to Gitlab

When Jenkins wrote the build status back to the corresponding code repository through the Gitlab API, he needed permission to create comments and change the status in the code repository

Step one

① Gitlab create a new user (it is suggested to name the user as far as possible to know the meaning by the name, such as Jenkins)

Log in to new users and get Access Tokens as long as API.

③ Create a credential of the GitLab API Token type in Jenkins and save the token obtained in the previous step in the API Token field

④ Configure the information about Gitlab Server in Jenkins Gitlab plug-in

Step 2

When configuring Jenkins Job for the code repository, add the user to which the Token belongs to members and grant Developers roles or roles with higher permissions

5. Configure Webhook in Jenkins Job

1. Configure Gitlab Buid Trigger

2. Configure the action of Gitlab writing back information after construction

  • Add “Pushlish Build status to Gitlab”

    You’ll then see build status ICONS in THE CI/CD–>Pipeline and Merge Request sections of the Gitlab repository

  • Add Add note with build status on Gitlab Merge Request

    Later, different comments will be added to Gitlab’s Merge Request based on the different build status

  • “Add Vote for Build status on Gitlab Merge Request”

    Different votes will then be displayed in Gitlab’s Merge Request depending on the build status

  • “Accept Gitlab merge Request on success”

    This action automatically agrees to accept the Merge Request on Gitla after a successful build

  • No post-build actions are added

    The Gitlab event will only trigger the Jenkins build and will not write back any information to the corresponding Code repository of Gitlab

Gitlab code repository configuration Webhook

See: GitLab – Configuring code repository event triggers webhooks

Get Webhook HTTP request information variable

For Webhook HTTP POST request information sent by Gitlab, the following variables can be obtained directly.

  • gitlabBranch
  • gitlabSourceBranch
  • gitlabActionType
  • gitlabUserName
  • gitlabUserEmail
  • gitlabSourceRepoHomepage
  • gitlabSourceRepoName
  • gitlabSourceNamespace
  • gitlabSourceRepoURL
  • gitlabSourceRepoSshUrl
  • gitlabSourceRepoHttpUrl
  • gitlabMergeRequestTitle
  • gitlabMergeRequestDescription
  • gitlabMergeRequestId
  • gitlabMergeRequestIid
  • gitlabMergeRequestState
  • gitlabMergedByUser
  • gitlabMergeRequestAssignee
  • gitlabMergeRequestLastCommit
  • gitlabMergeRequestTargetProjectId
  • gitlabTargetBranch
  • gitlabTargetRepoName
  • gitlabTargetNamespace
  • gitlabTargetRepoSshUrl
  • gitlabTargetRepoHttpUrl
  • gitlabBefore
  • gitlabAfter
  • gitlabTriggerPhrase