Skip to main content

Run github action locally using act

ยท 5 min read
Saintmalik

Yeah, being doing the CI/CD implementations via github workflow lately and i am also trapped in the process of making commits to trigger the workflows or better still making empty commits, haha.

Well, you can say why don't you just use Jenkins, CircleCi, Gitlab CI? i think this all boils down to what your org uses, i think there is no need to go the jenkins way, when my org uses github and github has the workflow to implement CI/CD.

github spammy actions

Now, looking at the above screenshot, i am very sure that's how your Actions page looks like too, but what if you could test the workflows you write locally before pushing it to github?

That way, you can limit the spammy look of failed builds on your Actions page.

Thanks to nektos for building and open sourcing Act.

Act is a golang tool that allows you to run your GitHub Actions locally ๐Ÿš€, making the feedback process really quick for your tests.

Now let's jump into how to use this

Firstly, you will need to install docker, Act depends fully on it,

Done with that? now lets install act on our enviroment, i use macOS, so i will be installing it using brew, you can also install on linux using brew

brew install act

You can as well check the installation guide for other installation methods.

Time to run your github actions locallyโ€‹

Done with the installation of act ? it's time to run act.

Navigate into your project folder where you have your github workflow files and run the following command on your terminal.

act

That should spin things up and then display some outputs where you are asked to select the image you would like to use with act.

You will only see this once, because it's your first time running `act.

You can choose the medium image size, it is sufficient and okay, way better than micro option, after selecting the image size and hitting the enter button.

It will run and spin up the ~/.actrc file that contains the image configurations, you can always change this manually anytime.

So Immediately after selecting the image size, act also auto detect any workflow file in your .github/workflow folder and it will proceed with spining up a docker conatiner and run the actions workflows in it.

act image selection
note

Whenever the github actions workflow is done, the docker container gets discarded, but there is a way around that

In situations where you need to get the results/feedback of your workflow actions which are probably passed to html, json or xml files as output, you won't be able to access it because the docker container gets discarded.

But act has a subcommand to help with that, which makes your docker containers resusable.

hence you can enter the docker container to view your actions output or copy it into your local system, which everone is okay with you.

Here is the parameter to achieve that

act --reuse

Saving time on running your actions locallyโ€‹

You can also save time running this actions workflows, sure you don't want to wait all day for the local option you've choosen to take so much of your time, so if you will be running npm install or yarn install in your workflow and by chance already have a node_modules folder in your project root.

act can feed on the node_modules folder and speed/skip the dependencies installation process, here is the command to get that done

act -b
skip dependencies in workflow actions

Yeah, there is more, what about the github actions secrets? will your workflows with the secrets run locally? Yes they will

Passing secrets into your local github actions workflowโ€‹

Passing github workflow secrets can be done in various ways, you can enter them manually using the -s parameter.

act -s GITHUB_TOKEN=[insert token]

Alternatively, you can use the --secret-file parameter too, that way you can pass more workflow secrets along with your workflow, just create and open my.secrets file in your project root folder.

touch my.secrets
open my.secrets

Then add your action secrets into the file in the following formats

GITHUB_TOKEN=${YOUR TOKEN}

then run the file with the secret-file parameter

act --secret-file my.secrets

And thats all, there are more act options you can play with to speed up your devsecops or devops with github action by testing workflows locally.


Comments