|
| 1 | +--- |
| 2 | +title:"How to use SSH keys in freestyle steps" |
| 3 | +description:"Running commands remotely from Codefresh Pipeline" |
| 4 | +group:troubleshooting |
| 5 | +sub_group:common-issues |
| 6 | +toc:true |
| 7 | +--- |
| 8 | + |
| 9 | +You can easily connect to external servers in Codefresh pipelines and run commands with them via SSH. |
| 10 | + |
| 11 | +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](https://hub.docker.com/r/praqma/network-multitool) as it has several other networking tools inside. |
| 12 | + |
| 13 | +Then create a freestyle step in your pipeline like this: |
| 14 | + |
| 15 | + |
| 16 | +{% highlight yaml %} |
| 17 | +{% raw %} |
| 18 | + ssh: |
| 19 | + title: "Executing command over SSH" |
| 20 | + type: "freestyle" |
| 21 | + image: "praqma/network-multitool" |
| 22 | + commands: |
| 23 | + - mkdir /root/.ssh |
| 24 | + - echo ${{SSH_KEY}} | base64 -d > /root/.ssh/id_rsa ## Value of ${{SSH_KEY}} is base64 encoded |
| 25 | + - chmod 600~/.ssh/id_rsa |
| 26 | + - eval $(ssh-agent -s) |
| 27 | + - ssh-add~/.ssh/id_rsa |
| 28 | + - ssh -o "StrictHostKeyChecking no" user@host |
| 29 | + - ssh user@host 'command' |
| 30 | +{% endraw %} |
| 31 | +{% endhighlight %} |
| 32 | + |
| 33 | + |
| 34 | +The pipeline expects a[variable]({{site.baseurl}}/docs/codefresh-yaml/variables/) called “SSH” that you can enter directly or fetch from[shared configuration]({{site.baseurl}}/docs/configure-ci-cd-pipeline/shared-configuration/). |
| 35 | + |
| 36 | +Replace user, host, and command with your own values. |
| 37 | + |
| 38 | +##What to read next |
| 39 | + |
| 40 | +*[How to use SSH keys with GIT]({{site.baseurl}}/docs/docs/integrations/git-providers/#ssh-keys) |
| 41 | +*[How to deploy with SCP]({{site.baseurl}}/docs/docs/yaml-examples/examples/deploy-to-tomcat-via-scp/) |
| 42 | +*[How to deploy with FTP]({{site.baseurl}}/docs/docs/yaml-examples/examples/transferring-php-ftp/) |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |