PROJECTS NOTES HOME

Run github actions locally with act

1 Routine that caused me to look for something better

  • Modify the github action
  • Push to git
  • Wait 2-3 min for ubuntu instance to build
  • Wait for a another 1 min for your action to run
  • See that it failed because of a typo in yml file
  • Modify the github action……
  • ….
  • repeat

This way, with each mistake that you make in the .yml file, you pollute your git version control with meaningless commits. What if you could do the testing of your github actions locally, without needing to commit anything?

A tool called [[https://github.com/nektos/act][act]] is here for that. It creates a docker image locally and runs our actions.

Act really helped me out when I was trying to create an action that fails if coverage requirements are not met.

2 Act installation

2.1 On NixOS

2.2 On Windows

Tutorial - https://www.youtube.com/watch?v=7xfDpoEBp60&ab_channel=ShawnWildermuth

  • Install act with winget install nektos.act reference link here
  • Restart vscode/vscode terminal

3 Create a demo action

  • Create .github folder in your repo
  • Inside of it, create workflows folder
  • Inside of it, create demo.yml file
  • It's content:

    name: GitHub Actions Demo
    run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
    on: [push]
    jobs:
      Explore-GitHub-Actions:
        runs-on: ubuntu-latest
        steps:
          - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
          - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
          - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
          - name: Check out repository code
    	uses: actions/checkout@v4
          - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
          - run: echo "🖥️ The workflow is now ready to test your code on the runner."
          - name: List files in the repository
    	run: |
    	  ls ${{ github.workspace }}
          - run: echo "🍏 This job's status is ${{ job.status }}."
    

4 Run the action

  • Make sure docker is running
  • Navigate to the same folder where your .github folder is
  • Run act -l to see if it sees that it's inside a repo where a github action lies
  • run act command to run all the workflows within the workflows file, in our case it's only 1 workflow
  • If you want to run a specific action, not all, can do it with such flag act -W '.github/workflows/checks.yml'. reference.
  • If your workflow is using secret keys(like in Django), can add specify them in a similar way "${{ secrets.SECRET_KEY }}"

5 Github action status badge

If you like, you can add a badge to your repo do show your action status.