How to use SSH keys in freestyle steps

Running commands remotely from Codefresh Pipeline

You can easily connect to external servers in Codefresh pipelines and run commands with them via SSH.

First, you need to create or find a Docker image with the SSH client. A good choice is https://hub.docker.com/r/praqma/network-multitool as it has several other networking tools inside.

Then create a freestyle step in your pipeline like this:

 ssh:
   title: "Executing command over SSH"
   type: "freestyle"
   image: "praqma/network-multitool"
   commands:
     - mkdir /root/.ssh
     - echo ${{SSH_KEY}} | base64 -d > /root/.ssh/id_rsa ## Value of ${{SSH_KEY}} is base64 encoded
     - chmod 600 ~/.ssh/id_rsa
     - eval $(ssh-agent -s)
     - ssh-add ~/.ssh/id_rsa
     - ssh -o "StrictHostKeyChecking no"  ${{MY_USER}}@${{MY_HOST}}
     - ssh ${{MY_USER}}@${{MY_HOST}} 'uptime'

The pipeline expects some variables called SSH_KEY, MY_USER, MY_HOST that you can enter directly in the pipeline or fetch from shared configuration.

Replace uptime, with your own command that you want executed on the remote host.