Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to checkout ssh remote in github organization Jenkins workflow and use ssh credentials in Jenkinsfile

I have a Github Organisation item in jenkins. On the root of my repository, my Jenkinsfile looks something like this:

node {

  def jenkinsCredsId = 'xxxxxxxx-yyyy-zzzz-aaaa-bbbbbbbbbbbb'

  stage 'Checkout'
  checkout scm
  // I also tried the following:
  // checkout scm: [$class: 'GitSCM', source: 'ssh://[email protected]:MY_ORGANISATION/jenkins-testing-temp.git', clean: true, credentialsId: jenkinsCredsId]

  stage 'Build'
  // generate some artefact (dist.zip)

  stage 'Release'

  sshagent([jenkinsCredsId]) {
    sh '''
    git remote -v // show remotes
    ssh-add -l // show currently loaded ssh keys fingerprints

    git fetch --all --tags // IT FAILS HERE

    CURRENT_BUILD_TAG="some_build/${BUILD_NUMBER}"
    git tag ${CURRENT_BUILD_TAG}

    git push --tags

    github-release release \
    --security-token ${GITHUB_RELEASE_TOKEN} \
    --user MY_ORGANIZATION \
    --repo MY_REPO \
    --tag ${CURRENT_BUILD_TAG} \
    --name ${CURRENT_BUILD_TAG}

    github-release upload \
    --security-token ${GITHUB_RELEASE_TOKEN} \
    --user MY_ORGANIZATION \
    --repo MY_REPO \
    --tag ${CURRENT_BUILD_TAG} \
    --name ${CURRENT_BUILD_TAG} \
    --file dist.zip
    '''
  }

There's a few lines for testing repository access in here and it's currently failing on the git fetch part with the following error:

fatal: could not read Username for 'https://github.com': No such device or address

The git remote -v command from the above Jenkinsfile outputs something like origin https://github.com/MY_ORGANIZATION/MY_REPO.

My Github Organization git configuration looks like this: repository-sources-github-organization-jenkins-config

I found a few related questions:

  • how-to-use-ssh-keys-with-jenkins-workflow-plugin
  • tag-a-repo-from-a-jenkins-workflow-script
like image 556
GabLeRoux Avatar asked Nov 15 '16 19:11

GabLeRoux


People also ask

How do I manage ssh keys for Git within Jenkins?

Add SSH Key inside JenkinsIn the dropdown, select 'SSH username with private key' and then give a name for it. Copy the private key from the Jenkins server. Now you can clone any git repo in this Jenkins instance. You do not need to provide the credentials while configuring the job in Jenkins.

How does Jenkins connect to remote server using ssh?

Configuring the remote-host to be connected to via SSH:Navigate to Jenkins Dashboard> Manage Jenkins > Configure System. Add this remote host with the same name as mentioned in the docker-compose file and 22 as the default SSH port. Use the SSH credentials configured in the previous step and check the connection.


1 Answers

It turns out that the Github Organization item only uses https credentials for the Scan credentials (like the picture above).

The solution was to hit Advanced button, and to actually select a ssh credential in the Checkout credentials dropdown instead of the default - Same as scan credentials -.

advanced-view-checkout-credentials

Note that credentials with stars are user/password entries, that's how I found the problem.

This way, the checkout scm will use ssh instead and the sshagent([jenkinsCredsId]) { block will work as expected, letting you create tags, fetch and push according to your rights. :)

like image 121
GabLeRoux Avatar answered Sep 23 '22 19:09

GabLeRoux