Running GitHub Actions Locally with Act 🚀
GitHub Actions is a powerful tool for automating your workflow. It allows you to build, test, and deploy your code directly from GitHub.
However, sometimes you may want to test your Actions locally before pushing them to production. This is where the open-source tool “act” comes in handy — https://github.com/nektos/act
Act allows you to run GitHub Actions locally on your own machine. This way, you can test and debug your Actions before deploying them.
Act is a useful tool for this purpose, allowing developers to run Actions on their own machines.
Here’s how to use the act to run a GitHub Action locally:
Setup Act
You can install Act on Mac using the following command
brew install act
Configure GitHub Actions
Next, create a directory for your GitHub Action and navigate to it.
mkdir my-action && cd my-action
Initialize a new GitHub Action by creating a .github directory and a workflows directory inside it.
mkdir -p .github/workflows
Create a new file in the workflows directory with the name my-action.yml. This file will contain the code for your GitHub Action.
touch .github/workflows/my-action.yml
Open my-action.yml in a text editor and add the following code:
name: My Action
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Hello, World!
run: echo "Hello, World!"
This is a simple GitHub Action that prints “Hello, World!” when triggered by a push event.
Run GitHub Actions using Act
Run the following command to list all available actions:
act -l
- To run a specific action, use the following command:
act -j <action_name>
Replace <action_name> with the name of the action you want to run.
Note: Make sure that you have all the necessary dependencies and environment variables set up for the action to run successfully.
- To run this Action locally using act, use the following command:
act -j build
This will execute the build job defined in the my-action.yml file. You should see the output “Hello, World!” in the terminal.
That’s it! You have successfully run a GitHub Action locally using act. You can now use Act to test and debug your Actions before deploying them to production.
I hope this helps! Let me know if you have any questions or need any further assistance.