patternMinor
Using Jenkins gitlab token to make an API call
Viewed 0 times
jenkinsgitlabmaketokencallusingapi
Problem
From inside a Jenkins pipeline, I want to create a merge request on gitlab
My problem is with the private_token, I don't want to put it in clear in the Jenkinsfile.
I didn't found a way to get it from Jenkins credentials, is there one ?
def toJson = {
input ->
groovy.json.JsonOutput.toJson(input)
}
def body = [
private_token: "...",
source_branch: "...",
target_branch: "...",
title: "..."
]
httpRequest httpMode: 'POST', requestBody: toJson(body), contentType: 'APPLICATION_JSON', url: 'https://my.gitlab.domain/api/v4/projects/1234/merge_requests'My problem is with the private_token, I don't want to put it in clear in the Jenkinsfile.
I didn't found a way to get it from Jenkins credentials, is there one ?
Solution
Try something similar to the following:
or
See https://jenkins.io/doc/pipeline/steps/credentials-binding/
and https://github.com/jenkinsci/gitlab-plugin/issues/536 for more information.
environment {
GITLAB_API_TOKEN = credentials('gitlab_api_token').
}or
withCredentials([[
$class: 'com.dabsquared.gitlabjenkins.connection.GitLabApiTokenImpl',
credentialsId: 'gitlab_api_token',
variable: 'GITLAB_API_TOKEN'
]])See https://jenkins.io/doc/pipeline/steps/credentials-binding/
and https://github.com/jenkinsci/gitlab-plugin/issues/536 for more information.
Code Snippets
environment {
GITLAB_API_TOKEN = credentials('gitlab_api_token').
}withCredentials([[
$class: 'com.dabsquared.gitlabjenkins.connection.GitLabApiTokenImpl',
credentialsId: 'gitlab_api_token',
variable: 'GITLAB_API_TOKEN'
]])Context
StackExchange DevOps Q#11117, answer score: 5
Revisions (0)
No revisions yet.