Github action use SSH to access remote host and execute a script

In this article, we will use 6 steps to illustrate how to use Github action to access a remote server and execute the shell script. So that’s getting started:

step 1. Generate keygen in local machine

In your local machine, execute the following command in the terminal and generate your ssh-keygen.

ssh-keygen -f ~/.ssh/yourhost -t ed25519 -b 4096 -C ""

step 2. Copy our public key to the remote server’s authorize

# goto remote server

echo "your public keygen content" >> .ssh/authorized_keys

step 3. copy our Private Key and paste it into Github Secrets.

Go to “Settings” > “Secrets”, and click “New repository secret” button.

HOST: your host address KEY_ED25519: past your private keygen content PORT: your ssh port, default is 22 USERNAME : your ssh login username

step 4. create github action workflows ymal

Go to the “Actions” tab, and click “New workflow” button

past fellow code

name: remote ssh command
on: [push]

    name: Build
    runs-on: ubuntu-latest
    - name: executing remote ssh commands using ssh key
      uses: appleboy/ssh-action@master
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        key: ${{ secrets.KEY_ED25519 }}
        port: ${{ secrets.PORT }}
        script: sh

step 5. create your script

Go to your server and create the shell script file

Here, we create a bash that will generate a hellofile and include helloworld.

echo 'helloworld' > hellofile.txt

Step 6. pull your workflow and re-push

Pull your workflow and try to push ~

Finally, go to “Actions” and check workflow runs with the green line. If yes, the file should be created on the remote server.

Congradurations~ you already learn how to use github action.