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
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
- name: clone
- echo "$SSH_KEY" > $HOME/.ssh/id_rsa
- chmod 600 $HOME/.ssh/known_hosts
- ssh-keyscan -H firstname.lastname@example.org > ~/.ssh/known_hosts 2> /dev/null
- git clone ssh://email@example.com:22/USERNAME/REPOSITORY.git .
- git checkout $DRONE_COMMIT