Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

jenkins - Is there a way to get Gitlab MR "label" values inside a "scripted pipeline" Jenkinsfile?

Gitlab merge requests have label values that can be added and removed both programmatically and manually (by reviewers).

Gitlab Labels

Gitlab has a Jenkins Integration setup that sends a JSON blob to Jenkins which contains the labels:

  "labels": [
    {
      "id": 2132,
      "title": "blocked",
      "color": "#ffbb00",
      "project_id": 29634,
      "created_at": "2019-11-01 14:32:22 UTC",
      "updated_at": "2020-05-12 15:52:22 UTC",
      "template": false,
      "description": "MR cannot be merged; rationale should be described in-line.",
      "type": "ProjectLabel",
      "group_id": null
    },
    {
      "id": 1909,
      "title": "ready for review",
      "color": "#5843AD",
      "project_id": 29634,
      "created_at": "2019-08-19 23:59:00 UTC",
      "updated_at": "2019-08-19 23:59:00 UTC",
      "template": false,
      "description": "",
      "type": "ProjectLabel",
      "group_id": null
    }
  ],

The Jenkins Gitlab plugin intercepts that JSON blob and kicks off a test.

I am guessing that this JSON blob is written out to a data structure that I can access inside the scripted pipeline Jenkinsfile, but I can't find any documentation on how to extract values from it. I'd like to be able to check the labels that are present and then take different test actions based on the assigned labels.

How do I extract Gitlab JSON data inside of a scripted pipeline Jenkinsfile?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We have to be careful here, there are two possibilities, when scripted pipelines are used. TL:DR; for multibranch pipeline jobs, you do have environment variables, for normal pipeline jobs not.

The first way, within normal pipeline jobs is utilizing the jenkins gitlab plugin. it is not possible to get the labels with this plugin, there is a pull request open for 2 years without interaction so far.

The second way, if you are using multibranch pipelines, you are most likely utilizing the gitlab branchsource plugin. Which offers way more environmentvariables as decribed here.

Specifically interesting for you should be the following

  • GITLAB_LABELS_COUNT
  • GITLAB_LABEL_ID_#
  • GITLAB_LABEL_TITLE_#
  • GITLAB_LABEL_COLOR_#
  • GITLAB_LABEL_PROJECT_ID_#
  • GITLAB_LABEL_CREATED_AT_#
  • GITLAB_LABEL_UPDATED_AT_#
  • GITLAB_LABEL_TEMPLATE_#
  • GITLAB_LABEL_DESCRIPTION_#
  • GITLAB_LABEL_TYPE_#
  • GITLAB_LABEL_GROUP_ID_#

Where GITLAB_LABELS_COUNT will return you the ammount and via the index you can parse through all of them.

eg. i create a merge request with the labels Doing and To Do and received following env variables

GITLAB_LABEL_TITLE_1=To Do
GITLAB_LABEL_TITLE_0=Doing
GITLAB_LABEL_GROUP_ID_0=<group id>
GITLAB_LABEL_GROUP_ID_1=<group id>
GITLAB_LABEL_ID_0=<label id 0>
GITLAB_LABEL_ID_1=<label id 2>
GITLAB_LABELS_COUNT=2
GITLAB_LABEL_TEMPLATE_1=false
GITLAB_LABEL_TEMPLATE_0=false
GITLAB_LABEL_COLOR_0=#5CB85C
GITLAB_LABEL_COLOR_1=#F0AD4E
# i skipped the Date fields

mr with labels


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...