DEV Community

Cover image for How debugging GitHub Actions with SSH?
Davyd NRB
Davyd NRB

Posted on • Updated on

How debugging GitHub Actions with SSH?


UPDATE: 27.10.2020

Thanks Luchi, who create a GitHub Action debug-via-ssh now it easier to connect into an instance.


Often the best way to troubleshoot problems is to SSH into a job and inspect things like log files, running processes, and directory paths.

I really like a CircleCi ability to debugging with SSH, but seems Github Actions don't have this opportunity?

Actually no, let me show a workaround 🤪


Setup

The first step you need to create an account on the ngrok.com website

Then in the created .yml file will add a two steps:

# .github/workflows/my-build.yml
name: Debugging with SSH
on: push
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
     - uses: actions/checkout@v1

     - name: Try Build
       run: ./not-exist-file.sh it bloke build

     - name: Start SSH via Ngrok
       if: ${{ failure() }}
       run: curl -sL https://gist.githubusercontent.com/retyui/7115bb6acf151351a143ec8f96a7c561/raw/7099b9db76729dc5761da72aa8525f632d8875c9/debug-github-actions.sh | bash
       env:
        # After sign up on the https://ngrok.com/
        # You can find this token here: https://dashboard.ngrok.com/get-started/setup
        # NGROK_TOKEN: ${{ secrets.NGROK_TOKEN }}
        NGROK_TOKEN: 1cu2nQXxljrX4mNGxz4z9N1QSfm_3JDS7Ek4Rti6oTwov43Sx

        # This password you will use when authorizing via SSH 
        # USER_PASS: ${{ secrets.USER_PASS }}
        USER_PASS: murAmur

     - name: Don't kill instace
       if: ${{ failure() }}
       run: sleep 1h # Prevent to killing instance after failure
Enter fullscreen mode Exit fullscreen mode

NOTE: Don't paste tokens directly to .yml file
USE: Secrets variables


Result

So when the build failed will be runner SSH session and you will the next message in the console:

login command

Let login using USER_PASS: murAmur :

login via ssh
lscpu result


if you have any question I am glad to discuss them in the comments!

(c) MurAmur

Top comments (6)

Collapse
 
abcfy2 profile image
Feng Yu

Any way to debug runs-on: windows-latest?

Collapse
 
luchihoratiu profile image
Luchi

I've also needed a way to debug Windows GitHub Actions runners and ended up making github.com/marketplace/actions/deb...
Any feedback or code contributions are extremely welcomed!

Collapse
 
retyui profile image
Davyd NRB

Actually, I haven't investigated for work with ssh on Windows and I can't give advice

Collapse
 
kubukoz profile image
Jakub Kozłowski

Wow, that's a really smart idea. Thanks!

Collapse
 
felipetio profile image
Felipe Vieira • Edited

That's super helpful! Thanks, David! ✌️

Do you know a simple way to load the workflow ENV into that session?

Collapse
 
retyui profile image
Davyd NRB

Just login in Linux container and write next commands:

# set envs
export MY_ENV=123
export MY_ENV2="test test"

# test 
echo $MY_ENV
echo $MY_ENV2