Andrea Matera
Andrea Matera

Reputation: 63

SSH Git access using Gradle Release Plugin

Using Jenkins Pipeline I changed the Repository URL from http to ssh git access. After doing that the job is not working anymore (before that all worked correctly).

Down below the logs:

:xxxxxx:checkUpdateNeeded
Running [git, remote, update] produced an error: [Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
error: Could not fetch origin]
:xxxxxx:checkUpdateNeeded FAILED
:release FAILED
Release process failed, reverting back any changes made by Release Plugin.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':checkUpdateNeeded'.
> Failed to run [git remote update] - [Fetching origin
  ][Permission denied (publickey).
  fatal: Could not read from remote repository.

  Please make sure you have the correct access rights
  and the repository exists.
  error: Could not fetch origin
  ]

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

The SSH RSA Key is correctly working because: - I configured correctly on our Bitbucket server in order to Read/Write on that repo - I added the key into ssh-agent - I can clone and commit directly from the server where the jenkins job is executed.

This is the gradle build file section:

....
release {
    versionPropertyFile="${rootDir}/gradle.properties"
    failOnCommitNeeded=false
    git{
        requireBranch="releases/.*|master"
    }
    tagTemplate = 'T-'+new Date().format('yy.MM')+'-${version}'
}


task publishRelease(type: GradleBuild) {
    tasks = ['publishMavenJavaPublicationToReleaseRepository']
    startParameter.projectProperties = [nexusUser: nexusUser, nexusPassword: nexusPassword]
}
....

Upvotes: 5

Views: 2798

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"

Upvotes: 0

pwojnowski
pwojnowski

Reputation: 392

Wrap the call to grade with ssh agent:

sshagent(credentials: ['id-of-private-key-defined-in-jenkins']) {
   withGradle {
     sh 'gradle release -Prelease.useAutomaticVersion=true'
   }
}

This will make the private key available for Git calls underneath.

Upvotes: 0

VonC
VonC

Reputation: 1329002

I can clone and commit directly from the server where the jenkins job is executed.

Then Jenkins should too, provided:

  • it is executed with the same user
  • and the SSH key is the default one ~/.ssh/id_rsa.

If any of those two conditions is not met, you need to specify the exact path of the private key, using the Jenkins SSH Credentials Plugin.

Upvotes: 2

Related Questions