Drone CI - How to clone via SSH
If you need a full Example of how to make a SSH Pipeline check out my other Post!
Normally Drone CI clones the Repositories via http/https.
To be able to clone via SSH you need to first pass a SSH Private Key for the Cloning as an Environment Variable.
In this example I already added the Private Key in Drone as a Secret with the Name ssh_key
Make sure the Public Key is known to the Git Server.
Then I disable the default Cloning Step and create a new One with the Commands to clone via SSH:
- Call the Drone Secret with the Private Key with from_secret and store it inside the Environment Variable SSH_KEY
- Write the Content of SSH_KEY to a new SSH Key File ‘id_rsa’
- Set File Permissions to the Key File so that the SSH Client won’t output any Errors when it tries to use it.
- Add the Git Server to the known Hosts before trying to access it with SSH
- Clone your Repository to your current directory
- Checkout the Branch
clone:
disable: true
steps:
- name: clone
environment:
SSH_KEY:
from_secret: ssh_key
commands:
- echo "$SSH_KEY" > $HOME/.ssh/id_rsa
- chmod 600 $HOME/.ssh/known_hosts
- ssh-keyscan -H git@your.gitserver.com > ~/.ssh/known_hosts 2> /dev/null
- git clone ssh://git@your.gitserver.com:22/USERNAME/REPOSITORY.git .
- git checkout $DRONE_COMMIT