DEV Community

Cover image for How to run GitHub Actions locally
Elisabeth Leonhardt
Elisabeth Leonhardt

Posted on

How to run GitHub Actions locally

This week, I made an update in our codebase that required a few changes in tests. I fixed them, tested locally and everything was fine. Proud of my accomplishment, I opened a PR, only to see that the tests started failing when running inside the GitHub Action.

I could have committed a bunch of log messages to the failing tests to find the cause of the issue but that seemed too tedious and I didn't want my coworkers to be able to see all my console.log('do we get here?') either. So, to maintain the appearance that I am a capable developer, I configured GitHub Actions locally and here is how:

  1. I installed act https://nektosact.com/ - I used Brew
  2. You need Docker, so the actions can run inside a container. I had Rancher already installed
  3. (optional) install the "GitHub Local Actions" extension if you are using VSCode or Cursor and prefer a UI

I was using cursor and after installing everything, I could see the required components were installed and running and the extension did already detect my workflows:

Components available

Workflows for GitHub Actions

When starting the GitHub Actions with the extension, I got the following error: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Error message when starting the workflow locally

To solve this, I just copied the command that the extension creates and added a DOCKER_HOST env variable in front, like so:

DOCKER_HOST=$(docker context inspect --format '{{.Endpoints.docker.Host}}') act --workflows ".github/workflows/backend.yaml" --secret-file "" --var-file "" --input-file "" --eventpath ""

To run just one simple job and not the whole workflow (maybe you don't need to lint right now) you just add a --job option:

DOCKER_HOST=$(docker context inspect --format '{{.Endpoints.docker.Host}}') act --workflows ".github/workflows/backend.yaml" --job "test" --secret-file "" --var-file "" --input-file "" --eventpath ""

The output was quite large in my case, so I redirected everything into log.txt, asked Cursor to go over it for me and find the critical error messages

DOCKER_HOST=$(docker context inspect --format '{{.Endpoints.docker.Host}}') act --workflows ".github/workflows/backend.yaml" --job "test" --secret-file "" --var-file "" --input-file "" --eventpath "" > log.txt

This way, I found the issue quickly and looked like a hacker in the process. Hope this helps you too.

Top comments (2)

Collapse
 
rbeier profile image
Robin

Didn't know this was even possible. Thank you!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.