How to clone a git repository with SSH using Jenkins credentials and pipeline?

Viewed 1821

I'm running Jenkins in a Docker and I want to clone a BitBucket repository in a temporary fashion, execute the script and wipe the clone out.

If I try to clone right now I'm having error of the private key obviously because it's not installed in the Docker. So I would like to use the Jenkins credentials to clone and wipeout. My scenario is something like this:

  • git somehowlogin with jenkinscredentials SSHprivatekeys

  • git clone ssh://git@stash.group.net/~myid/myrepo.git

  • git checkout echo ${env.BRANCH_NAME}

  • Execute some scripts within the repo

  • git somehow clean all temp files

Can you help me with this?

1 Answers

it looks like you still need some to work to configure access to git via ssh you can follow the below steps:

1- Create an SSH keypair.

# ssh-keygen

2- Add the public key as a deployment key for the repository on BitBucket A private key(id_rsa) saved to your Jenkins and a public key uploaded to Bitbucket. Bitbucket uses the key pair to authenticate anything the associated account can access. 3- upload the private key in Jenkins, navigate to Jenkins URL, and following the menu

Manage Jenkins –> configure credentials –> credentials –> system –>Add credentials.

enter image description here

Now add the public key to bit bucket,

Bitbucket –> settings –> security–>ssh keys –>add keys.

so now you can create a new job with your ssh credentials.

if you have a Jenkinsfile in your git repo you can use :

node () {
  
    stage('checkout .. run and delete ') {
          checkout scm
          sh './youscript.sh'
           deleteDir() // will remove git repo.

   } }
Related