Search code examples
githubjenkinsgradlerelease

Jenkins release pipeline for Github


I have a Jenkins pipeline job to make a release. It uses the Jenkin's Github plugin to checkout the project and make a build.

My simplified DSL is:

multibranchPipelineJob('Release') {
    ...
    branchSources {
        branchSource {
            source {
                github {
                    id('AAA')
                    repoOwner('BBB')
                    repository('CCC')
                    credentialsId('github-credentials')
                    repositoryUrl('https://github.com/BBB/CCC')
                    configuredByUrl(false)
                }
            }
          ...
        }
    }
    ...
}

and my simplified 'Jenkinsfile' is like:

pipeline {
    agent any
    stages {
        stage('Build & Release') {
            steps {
                sh "./gradlew clean build release"
            }
        }
    }
}

But, when it tries to execute the release task, it fails with the following exception.

Caused by: org.eclipse.jgit.errors.TransportException: https://github.com/BBB/CCC.git: Authentication is required but no CredentialsProvider has been registered
    at org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:531)
    at org.eclipse.jgit.transport.TransportHttp.openPush(TransportHttp.java:434)
    at org.eclipse.jgit.transport.PushProcess.execute(PushProcess.java:127)
    at org.eclipse.jgit.transport.Transport.push(Transport.java:1335)
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:137)

My understanding is that when release task is run, it tries to connect using SSH to Github, but I haven't setup one as we don't want to maintain a 'user' for Jenkins on Github. How can I resolve this without setting up SSH keys on Github?


Solution

  • The credentials for Github App can be accessed in the Jenkinsfile as follows:

    steps {
      withCredentials([usernamePassword(credentialsId: 'github-app-id',
                       usernameVariable: 'GITHUB_APP',
                       passwordVariable: 'GITHUB_ACCESS_TOKEN')]) {
        sh 'export GRGIT_USER=$GITHUB_APP && export GRGIT_PASS=$GITHUB_ACCESS_TOKEN && ./gradlew final'
      }
    }