Ray
Ray

Reputation: 3201

How to pass Git SSH credentials to Gradle release plugin in Jenkins?

I’m trying to employ the Gradle release plugin on our client’s Jenkins (1.x, so no Jenkinsfile pipelines… 😟). I tested it on my box, and everything is well. However, when I invoke the build job, it fails with this output:

Task :foundation:checkUpdateNeeded FAILED
Running [git, remote, update] produced an error: [Permission denied (publickey).

I know that Jenkins has got a set of Git SSH Credentials, because the job starts by checking out a fresh copy from Git using these.

How can we make the release plugin use the credentials which are configured for the job during checkout?

Upvotes: 3

Views: 2962

Answers (3)

Ravi Natesh
Ravi Natesh

Reputation: 51

Use the below script in the Shell command before executing the Gradle task that runs the git command to update the repo.

Here GITHUB_SSH_KEY is an environment variable injected from credentials using the "SSH User Private Key" option.

cat "${GITHUB_SSH_KEY}" > ./build/sshkey
cd build
chmod 600 sshkey    
eval `ssh-agent -s`
ssh-add sshkey
cd ..

git config core.sshCommand "ssh -i ./build/sshkey -o 'IdentitiesOnly yes'"
git config --global user.email "[email protected]"
git config --global user.name "flastname"

Note: If the build folder doesn't exist, insert "mkdir build" on top of the script.

Upvotes: 0

Carlos Cavero
Carlos Cavero

Reputation: 3196

Gradle release does not allow to configure the git credentials. Even this question is unnecesary I will put two different possibilities to sort this out because I was struggling all the day with this. Why? Because I am not allowed to use SSH anymore in the company and we are moving to docker containers to distribute our CI pipelines:

1.) Put the SSH key under user jenkins ~/.ssh/id_rsa as it is explained here

2.) Use "Execute shell" before the gradle release to configure the remote:

enter image description here

Token must be configured as an environment variable. This to answer the initial question.

3.) More advance functionalities can be included with the use of pipelines. I put below the Jenkinsfile to execute gradle release (you can use also sshagent (credentials: ['credential']) and then you do not need the git stuff):

    // GITLAB_API_TOKEN
    withCredentials([string(credentialsId: 'nexususer', variable: 'nexusUsername'),
        string(credentialsId: 'nexuspassword', variable: 'nexusPassword'),
        string(credentialsId: 'nexussnapshoturl', variable: 'nexusSnapshotUrl'),
        string(credentialsId: 'nexusreleaseurl', variable: 'nexusReleaseUrl'),
        string(credentialsId: 'token', variable: 'GITLAB_API_TOKEN')]) {
        if (env.BRANCH_NAME == "master") {
            stage('Release') {
                gitlabCommitStatus(name: 'Release') {
                    // Run the gradle release
                    sh 'git config user.email "email"'
                    sh 'git config user.name "name"'
                    sh "git remote rm origin"
                    sh "git remote add origin https://username:${GITLAB_API_TOKEN}@yourrepo"
                    sh "gradle clean release -Prelease.useAutomaticVersion=true"
                }
            }
        }
    }

Upvotes: 3

minas
minas

Reputation: 230

Assuming it's a freestyle job and you're using private key to authenticate:

  1. Check option Use secret text(s) or file(s). Select git credentials and enter names for environment variables that you need to import.
  2. In Gradle build part import your key environment variable (and others if needed) as a project property: -Pkeylocation=$KEY_VARIABLE_NAME

Upvotes: 0

Related Questions