I want to connect to a server via SSH with a public key when GitLab pipeline succeeds.
As I see, I need to generate a key with ssh-keygen on GitLab side and add it to server where I want to connect.
I can generate a key during the pipeline but as the public key is not added to the target server, it makes no sense.
I suppose it's a common scenario to connect from a CI build to a remote SSH with a key.
How can I make it work?
You can run ssh-keygen from wherever you want as long as you use the appropriate keys on the appropriate server.
Here is what you need:
SSH_PRIVATE_KEY
)~/.ssh/authorized_keys
fileIn order to do that last step, just add the following to your
.gitlab-ci.yml
in the script or before_script section of the job of interest:Then do your SSH connections and voilà !
EDIT: I couldn't remember where I had found this info the first time but here it is : https://docs.gitlab.com/ee/ci/ssh_keys/README.html