Answers: <p>You can do this using the <a href="https://jenkins.io/doc/pipeline/steps/credentials-binding/" rel="nofollow noreferrer">Credential Binding Plugin</a>. Open an existing pipeline job, scroll down to the text area where you enter the pipeline code, and click the Pipeline Syntax link to open the Snippet Generator.</p>
  
  <p>From the Sample Step drop-down, select <code>withCredentials: Bind credentials to variables</code>. Under the Bindings section, select the "Add" button next to Credentials. Add a new credential of kind "Secret text". Enter the API token, give it an easy to remember ID (this is how we reference this secret) and a description.</p>
  
  <p>When the token is added, select it from the <strong>Credentials</strong> drop-down selection. Enter the variable you want to use to reference the API key. Using your example, I named it <code>private_token</code>. When finished, click the <strong>Generate Pipeline Script</strong> button to generate a sample withCredentials() function. Using this function, any reference to the API key will be hidden from the user when called as <code>private_token</code></p>
  
  <pre><code>withCredentials([string(credentialsId: 'Gitlab-API-Key', variable: 'private_token')]) 
  {
      echo("My private token is: ${private_token}")
  }
  </code></pre>
  
  <p><a href="https://i.sstatic.net/IBPW8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/IBPW8.png" alt="Screenshot for reference"></a></p>
  
Answers: CC BY-SA 4.0
Answers: 2022-01-21T10:31:19.547Z
Answers: That is exactly the approach that led me here. Doing that gives: `Credentials '366df523-22fd-4b84-9a29-da4b6506fde4' is of type 'GitLab API token' where 'org.jenkinsci.plugins.plaincredentials.StringCredentials' was expected`
Answers: Peter V. Mørch
Answers: CC BY-SA 4.0
Answers: 2020-03-20T13:57:41.150Z
Answers: 2020-03-20T13:57:41.150Z
Answers: Argyle
Answers: <p>What worked for me was</p>
  <ul>
  <li>Create access token in gitlab</li>
  <li>Create credential in Jenkins as username, password. Use a dummy username &amp; token as the password</li>
  </ul>
  <p>Use the below code block</p>
  <pre class="lang-java prettyprint-override"><code>withCredentials([gitUsernamePassword(credentialsId: 'your-jenkins-token-name', 
  gitToolName: 'Default')]) {  ... }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2023-07-20T12:00:14.843Z
Answers: 2023-07-20T15:37:41.987Z
Answers: 2023-07-20T15:37:41.987Z
Answers: Imran AK
Answers: <p>Try something similar to the following:</p>
  
  <pre><code>environment {
    GITLAB_API_TOKEN = credentials('gitlab_api_token').   
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>withCredentials([[
    $class: 'com.dabsquared.gitlabjenkins.connection.GitLabApiTokenImpl',
    credentialsId: 'gitlab_api_token',
    variable: 'GITLAB_API_TOKEN'
  ]])
  </code></pre>
  
  <p>See <a href="https://jenkins.io/doc/pipeline/steps/credentials-binding/" rel="noreferrer">https://jenkins.io/doc/pipeline/steps/credentials-binding/</a>
  and  <a href="https://github.com/jenkinsci/gitlab-plugin/issues/536" rel="noreferrer">https://github.com/jenkinsci/gitlab-plugin/issues/536</a> for more information.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-03-20T13:55:39.543Z
Answers: 2020-03-20T13:55:39.543Z
Answers: TheFiddlerWins
Body: <p>From inside a Jenkins pipeline, I want to create a merge request on gitlab</p>
  
  <pre><code>def toJson = {
      input -&gt;
      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'
  </code></pre>
  
  <p>My problem is with the private_token, I don't want to put it in clear in the Jenkinsfile.</p>
  
  <p>I didn't found a way to get it from Jenkins credentials, is there one ?</p>
  
Tags: <jenkins><gitlab>
Title: Using Jenkins gitlab token to make an API call
UserDisplayName: BlueMagma
ThreadText: # Using Jenkins gitlab token to make an API call
  
  (Asked by: username_0 on 2020-03-20)
  
  From inside a Jenkins pipeline, I want to create a merge request on gitlab
  
  ```
  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 ?
  
  **Tags:** jenkins, gitlab
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_4 on 2020-03-20. Score: 5 | Accepted answer
  
  Try something similar to the following:
  
  ```
  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.
  
  
  ---
  
  ### Answer by username_1 on 2020-03-20. Score: 1
  
  You can do this using the [Credential Binding Plugin](https://jenkins.io/doc/pipeline/steps/credentials-binding/). Open an existing pipeline job, scroll down to the text area where you enter the pipeline code, and click the Pipeline Syntax link to open the Snippet Generator.
  
  From the Sample Step drop-down, select `withCredentials: Bind credentials to variables`. Under the Bindings section, select the "Add" button next to Credentials. Add a new credential of kind "Secret text". Enter the API token, give it an easy to remember ID (this is how we reference this secret) and a description.
  
  When the token is added, select it from the **Credentials** drop-down selection. Enter the variable you want to use to reference the API key. Using your example, I named it `private_token`. When finished, click the **Generate Pipeline Script** button to generate a sample withCredentials() function. Using this function, any reference to the API key will be hidden from the user when called as `private_token`
  
  ```
  withCredentials([string(credentialsId: 'Gitlab-API-Key', variable: 'private_token')]) 
  {
      echo("My private token is: ${private_token}")
  }
  
  ```
  
  [![Screenshot for reference](https://i.sstatic.net/IBPW8.png)](https://i.sstatic.net/IBPW8.png)
  
  > **Comments:**
  > **username_2**: That is exactly the approach that led me here. Doing that gives: `Credentials '366df523-22fd-4b84-9a29-da4b6506fde4' is of type 'GitLab API token' where 'org.jenkinsci.plugins.plaincredentials.StringCredentials' was expected`  
  
  ---
  
  ### Answer by username_3 on 2023-07-20. Score: 0
  
  What worked for me was
  
  * Create access token in gitlab
  * Create credential in Jenkins as username, password. Use a dummy username & token as the password
  
  Use the below code block
  
  ```java
  withCredentials([gitUsernamePassword(credentialsId: 'your-jenkins-token-name', 
  gitToolName: 'Default')]) {  ... }
  
  ```
  
  
  ---
  
  