How to use Git Tokens in Jenkinsfile

Viewed 4421

I'm trying to use Jenkinsfile to clone git branches and merge them. Skeleton code is as below:

pipeline {
    agent { 
        label ""
    }
    stages {
        stage("Git") {
            steps {
                git( url: , credentialsId: )
            }
        }
    }
}

Everytime I run this I get the error

stderr: remote: Password authentication is not available for Git operations. remote: You must use a personal access token or SSH key.

Does anyone know how to use git tokens from within jenkinsfile? I'd very much appreciate any help. Thanks

3 Answers

This worked for me:

  withCredentials([string(credentialsId: "GITHUB_TOKEN", variable: 'GITHUB_TOKEN')]) {
    // An example for getting the statuses for a commit using GITHUB_TOKEN:
    // Use env.GITHUB_TOKEN here or just let shell expand it...
    def statusJson = sh returnStdout: true, 
         script: "curl --fail -s -X GET -H 'Accept: application/vnd.github.v3+json' -u " +
          ":\$GITHUB_TOKEN " +
 "https://api.github.com/repos/myowner/myrepo/commits/$gitCommit/statuses"    

  }
Related