Friday, July 29, 2022

[SOLVED] GitHub Actions: How to run remote SSH commands using a self hosted runner

Issue

Since I need to use a self-hosted runner, the option to use existing marketplace SSH Actions is not viable because they tend to docker and build an image on GitHub Actions which fails because of a lack of permissions for an unknown user. Existing GitHub SSH Actions such as appleboy fail due to reliance on Docker for me.

Please note: Appleboy & other similar actions work great when using GitHub Actions Runners.


Solution

Since it's a self-hosted runner i.e. I have access to it all the time. So using an SSH profile and then using native-run command works pretty well.

To create an SSH profile & use it in GitHub Actions:

  1. Create (if doesn't exist already) a "config" file in "~/.ssh/".

  2. Add a profile in "~/.ssh/config". For example-

    Host dev HostName qa.bluerelay.com User ec2-user Port 22 IdentityFile /home/ec2-user/mykey/something.pem

  3. Now to test it, on self-hosted runner run:

    ssh dev ls

This should run the ls command inside the dev server. 4. Now since the SSH profile is set up, you can easily run remote SSH commands using GitHub Actions. For example-

name: Test Workflow

on:
  push:
    branches: ["main"]

jobs:
  build:
    runs-on: self-hosted

steps:
  - uses: actions/checkout@v2

    - name: Download Artifacts into Dev 
    run: |
      ssh dev 'cd GADeploy && pwd && sudo wget https://somelink.amazon.com/web.zip'

  - name: Deploy Web Artifact to Dev 
    run: |
      ssh dev 'cd GADeploy && sudo ./deploy-web.sh web.zip'

This works like a charm!!



Answered By - Onkar Singh
Answer Checked By - Timothy Miller (WPSolving Admin)