Host key verification failed in github actions

270 Views Asked by At

I have a github actions setting that calls a shell script

  deploy:
    name: Deploy to ECR
    runs-on: ubuntu-latest
    steps:
      - name: Release
        run: |
          chmod +x ./release.sh
          ./release.sh

The contents of .release.sh are following. What it does is connects to a server via ssh and runs few commands:

#!/bin/bash

# SSH connection details
SSH_USER="ubuntu"
SSH_HOST="13.234.201.241"
SSH_PORT="22"
SSH_PRIVATE_KEY="${{ secrets.SSH_PRIVATE_KEY }}"  # Assuming the secret name is SSH_PRIVATE_KEY

    
# Create the SSH private key file
echo "$SSH_PRIVATE_KEY" > ./ssh_private_key
chmod 600 ./ssh_private_key

# SSH command to delete existing Docker container
ssh -i ./ssh_private_key -p "${SSH_PORT}" "${SSH_USER}@${SSH_HOST}" "docker rm -f ${CONTAINER_NAME}"

# SSH command with port forwarding
ssh -i ./ssh_private_key -p "${SSH_PORT}" -L "${LOCAL_PORT}:localhost:${LOCAL_PORT}" "${SSH_USER}@${SSH_HOST}" << EOF
  aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 714116641665.dkr.ecr.us-east-1.amazonaws.com
EOF

# Remove the temporary SSH private key file
rm ./ssh_private_key

However I am not able to connect to the remote machine, although github actions runs successfully, the logs indicate it failed to connect to the ssh instance. The logs are

## FOLLOWING IS LOG Under the 'Release' Step:
Run chmod +x ./release.sh
  chmod +x ./release.sh
  ./release.sh
  shell: /usr/bin/bash -e {0}
  env:
    IMAGE: ghcr.io/$(echo $GITHUB_REPOSITORY | tr '[A-Z]' '[a-z]')/summarizer
    ECR_REPO_NAME: 714116641665.dkr.ecr.us-east-1.amazonaws.com/ml-libraries
./release.sh: line 7: ${{ secrets.SSH_PRIVATE_KEY }}: bad substitution
Host key verification failed.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Host key verification failed.

Although an error (or an warning?) github actions shows this step as executed successfully. How to resolve this issue of not being able to connect to the ssh machine (it clearly didn't as I can see).

I sense the error can be due to how I define my secrets.SSH_PRIVATE_KEY, but it is correct, I simply copy pasted the related .pem file

Note that this worked correctly in my local. Would be grateful for any help.

1

There are 1 best solutions below

0
On BEST ANSWER

Answer from @Azeem solved the issue, thanks Azeem