Connect remote server via SSH within the container

I want to locally run a job to deploy my current project. For that I use environment variables with secrets to provide the SSH key of the remote server:

kind: pipeline
type: docker
name: default

steps: 
  - name: deploy
    image: node:10.15.3
    environment:
      SSH_KEY:
        from_secret: ssh_key
    commands:
    - npm install --silent
    - npm install -g @angular/cli --silent
    - ng build --prod
    - echo "$SSH_KEY" > key
    - cd dist
    - scp -i /drone/src/key -P [my_ssh_port] -r * [my_user]@[host]

I have tested the env var and it’s correctly creating a key with the correct content within the container.
The problem comes with the scp command, it outputs:

[deploy:25] Host key verification failed.
[deploy:26] lost connection

I have run the same command in my local computer and it successfully connects to the server and copies the files, but for some reason from within the container that doesn’t happen.

I run the file with the following command:

sudo drone exec --secret-file [path_to_secret]

Is there something wrong in the file?

Here is an example script that we used to setup a private ssh key from a secret:

If you are certain the key is being successfully written to a file and is not malformed in any way, as a next step, I recommend posting to Stack Overflow so that your question can be directed to individuals that might have experience with these commands and their error messages.

1 Like

Thanks @ashwilliams1, I was missing those important steps. By the way a lot of trouble came because I am not using the standard SSH port, so I have to keep in mind when configuring the known host:

ssh-keyscan -p [ssh_port] -H [host] >> ~/.ssh/known_hosts