
Hey there, GitHub Actions whiz!
In the dynamic realm of software development, where agility and precision reign supreme, GitHub Actions has emerged as a formidable ally, empowering you to streamline your development workflow and consistently deliver high-quality code. However, testing these automated workflows can often be a time-consuming and repetitive task, requiring multiple pushes to the repository before issues are identified and resolved.
Fret not, for act has arrived – your secret weapon for conquering local testing of GitHub Actions workflows. This ingenious command-line tool transforms your machine into a mini-GitHub Actions runner, enabling you to simulate your workflows locally and nip potential problems in the bud. With act as your sidekick, you can bid farewell to lengthy testing cycles and embrace a more efficient and enjoyable development experience.
Ever wished you could test your workflows without waiting for a push to GitHub? Well, fear not, because your friendly neighborhood guide to awesomeness is here to introduce you to your new best friend: act!
Let’s get your workflows running smoothly locally!
Grab act
This nifty command-line tool acts like a mini-GitHub Actions runner right on your machine. To install it, you can use either Homebrew (macOS/Linux) or Chocolatey (Windows):
# macOS or Linux
brew install act# Windows (using Chocolatey)
choco install actCraft a Local Workflow Home
Make a cozy directory inside your project (think <root-project-dir>/.github/workflows/). This is where your local workflow files will reside, mirroring the structure of your actual workflows on GitHub.
Write Your Local Workflow Script
Create a YAML file (e.g., workflows/main.yml) and paste the same content as your GitHub Actions workflow. Just remember to swap out any secrets or environment variables with placeholders (local testing doesn’t have access to the real deal).
Here’s an example to get you started:
name: Cypress Tests
on:
push:
branches:
- templates/next-ts-cypress
jobs:
cypress-run:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
# Install npm dependencies, cache them correctly
# and run all Cypress tests
- name: Cypress run
uses: cypress-io/github-action@v6
with:
build: npm run build
start: npm start
wait-on: ‘http://localhost:3000’Time to Test with act!
Open your terminal, navigate to your project’s root directory, and run act with the name of your local workflow file:
act -l workflows/main.ymlact will work its magic, simulating the workflow and executing the steps just like it would on GitHub’s servers. Remember, act relies on Docker under the hood, so make sure you have Docker Desktop or the daemon installed and running.
Inspect the Results
Keep an eye on your terminal as act displays the steps being executed. You can use the following command to see them in more detail:
act -nWatch out for any errors, warnings, or unexpected behavior.
Debug and Refine
If you hit any snags, you can debug them locally:
Check the logs (similar to how you would on GitHub Actions).
Verify your environment variables and secrets (placeholders, remember?).
Double-check the order of step execution.
Make adjustments to your workflow file as needed.
Test More Workflows (Optional)
Feeling confident? Create additional local workflow files for other workflows you want to test, and run act for each of them.
Push with Confidence!
Once you’re happy that your workflows are functioning flawlessly locally, you can push your changes to the repository. GitHub Actions will then take over and run your workflows in the real GitHub environment.
Keep in Mind: While
actprovides a super close simulation, there might be slight differences between local execution and the actual GitHub environment. However, it’s a fantastic tool to catch most issues early on and save you tons of time.
Conclusion
Wielding the power of act for local testing of GitHub Actions workflows is a transformative step for any developer seeking to streamline their workflow and deliver bug-free code with unwavering confidence. By embracing this powerful tool, you can significantly reduce testing time, identify issues early, and ultimately elevate your development process to new heights.
So, the next time you embark on your GitHub Actions workflow adventures, remember act – your key to unlocking a smoother, more efficient, and enjoyable development journey. After all, testing is an indispensable part of the development process, and act equips you with the tools to make it a breeze. Embrace local testing, conquer bugs with ease, and watch your productivity soar!
Happy testing! And if you have any questions or need a helping hand, feel free to reach out to me via my social networks. We’re always here to cheer you on!
Github: https://github.com/maemreyo
X: https://twitter.com/_maemreyo
Email: [email protected]
