DEV Community

Serhii Korol
Serhii Korol

Posted on

[MAC] How locally run GitHub Actions? It's easy.

All of you could face situations when you push your changes, and if the project is enormous, then after 30-60 minutes, you receive a message that the build failed. For this 30-60 minutes, you could switch to another task and branch. But, unfortunately, it's, in fact, inconvenient to switch back. To resolve this annoying case, you can take advantage of my tutorial. Before we start, you should ensure you have installed Docker on your computer. I'll be using the Mac OS. If you use Windows or Linux, you can find a manual for configuring your environment by this link.

If your environment is established, then we can start. Also, I want to note all manipulations I'll commit with CLI, aka Terminal, will be on Mac OS.

First, you must open the Terminal and install the ACT package:

$brew install act
Enter fullscreen mode Exit fullscreen mode

For further work, we need to install GitHub CLI for launching commands from the Terminal.

$brew install gh
Enter fullscreen mode Exit fullscreen mode


]
The following step, it needs to authenticate on GitHub. For this, you should run this command:

$gh auth login
Enter fullscreen mode Exit fullscreen mode

There will be suggestions for selecting a type of service. I'll choose GitHub.com:

? What account do you want to log into? [Use arrows to move, type to filter] > GitHub.com GitHub Enterprise Server
Enter fullscreen mode Exit fullscreen mode

In the next step, you should select the protocol. For example, I prefer HTTPS, but you should choose SSH if you have strict security policy rules.

? What is your preferred protocol for Git operations? [Use arrows to move, type to filter] > HTTPS SSH
Enter fullscreen mode Exit fullscreen mode

Further, it'll ask permission to authenticate on GitHub. You also should type Y.

After accepting, you should select the way how you'll authenticate. I chose a web browser, but you can select the authentication with a token if you know how.

? How would you like to authenticate GitHub CLI? [Use arrows to move, type to filter] > Login with a web browser Paste an authentication token
Enter fullscreen mode Exit fullscreen mode

If everything is made correctly, you'll see this message:

! First copy your one-time code: [YOUR_ONE_TIME_CODE] Press Enter to open github.com in your browser...
Enter fullscreen mode Exit fullscreen mode

Finally, you'll see this report:

βœ“ Authentication complete. - gh config set -h github.com git_protocol https βœ“ Configured git protocol βœ“ Logged in as SergKorol
Enter fullscreen mode Exit fullscreen mode

In the following step, you need to install the extension to GitHub:

gh extension install https://github.com/nektos/gh-act
Enter fullscreen mode Exit fullscreen mode

If everything is correct, you'll see this message:

βœ“ Installed extension https://github.com/nektos/gh-act
Enter fullscreen mode Exit fullscreen mode

Further, go to your favorite folder with projects, and let's clone the project sample:

git clone https://github.com/SergKorol/ManagementSystem.git
Enter fullscreen mode Exit fullscreen mode

Let's check and go to the folder with the project:

cd ManagementSystem
Enter fullscreen mode Exit fullscreen mode

There should be the project's root that contains ".github/workflows/dotnet.yml"
You can check with this command:

cat .github/workflows/dotnet.yml
Enter fullscreen mode Exit fullscreen mode

And you'll see a configuration document:

# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: .NET

on:
 push:
  branches: [ "main" ]
 pull_request:
  branches: [ "main" ]

jobs:
 build:
  runs-on: ubuntu-latest

  steps:
  - uses: actions/checkout@v3

  - name: Setup .NET
   uses: actions/setup-dotnet@v3
   with:
    dotnet-version: 7.0.x

  - name: Restore dependencies
   run: dotnet restore src/ShopManagementSystem

  - name: Build
   working-directory: src/ShopManagementSystem
   run: dotnet build --no-restore

  - name: Test
   working-directory: src/ShopManagementSystem
   run: dotnet test --no-build --verbosity normal
Enter fullscreen mode Exit fullscreen mode

Before we run commands, I want to remind you that this document and your project should be the same version of SDK. Differently, you can't build the project. And also, I ask you to run your Docker client.
Let's start running and configuring our ACT. If you run this command, you'll see some information about GitHub Actions.

act -l
Enter fullscreen mode Exit fullscreen mode

Something like that:

Stage Job ID Job name Workflow name Workflow file Events      
0   build  build   .NET      dotnet.yml   push,pull_request
Enter fullscreen mode Exit fullscreen mode

If we try to run the event, it'll be suggested to choose the image configuration:

act push
Enter fullscreen mode Exit fullscreen mode

It'll be so:

? Please choose the default image you want to use with act: - Large size image: +20GB Docker image, includes almost all tools used on GitHub Actions (IMPORTANT: currently only ubuntu-18.04 platform is available) - Medium size image: ~500MB, includes only necessary tools to bootstrap actions and aims to be compatible with all actions - Micro size image: <200MB, contains only NodeJS required to bootstrap actions, doesn't work with all actions Default image and other options can be changed manually in ~/.actrc (please refer to https://github.com/nektos/act#configuration for additional information about file structure) [Use arrows to move, type to filter, ? for more help] Large > Medium Micro
Enter fullscreen mode Exit fullscreen mode

Leave the default value and press enter.
After configuring the image, let's run the act commands.
If you run this command, it'll be executed all jobs, including setup. Usually, it takes a relatively long time since it'll download SDK.

act
Enter fullscreen mode Exit fullscreen mode

In the end, you'll get a report like this:

[.NET/build] πŸš€ Start image=catthehacker/ubuntu:act-latest [.NET/build] 🐳 docker pull image=catthehacker/ubuntu:act-latest platform= username= forcePull=true ... Test Run Successful. | Total tests: 20 | Passed: 20 | Total time: 1.7369 Seconds | 1>Done Building Project "/Users/serhiikorol/RiderProjects/ManagementSystem/src/ShopManagementSystem/ShopManagementSystem.sln" (VSTest target(s)). | | Build succeeded. | 0 Warning(s) | 0 Error(s) | | Time Elapsed 00:00:03.25 [.NET/build] βœ… Success - Main Test [.NET/build] 🏁 Job succeeded
Enter fullscreen mode Exit fullscreen mode

But sure, you can run a specific job. However, we have only one job. For instance, I'll add another simple job and perform it. Add locally this block:

greeting:
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v3
    - name: Say hello
      run: echo "hello world"
Enter fullscreen mode Exit fullscreen mode

If you run this command, you can launch a specific job:

act -j greeting
Enter fullscreen mode Exit fullscreen mode

Ultimately, I want to show a trick if you don't want to wait until the SDK will be installed.
Let's delete this code, where setuping .NET7:

- name: Setup .NET
  uses: actions/setup-dotnet@v3
  with:
    dotnet-version: 7.0.x
Enter fullscreen mode Exit fullscreen mode

And paste this code:

build:
  runs-on: ubuntu-latest
  container:
    image: mcr.microsoft.com/dotnet/sdk:7.0
    options: --user root
  steps:
Enter fullscreen mode Exit fullscreen mode

And it works:

Test Run Successful.
| Total tests: 20
|    Passed: 20
| Total time: 1.8573 Seconds
|    1>Done Building Project "/Users/serhiikorol/RiderProjects/act/ManagementSystem/src/ShopManagementSystem/ShopManagementSystem.sln" (VSTest target(s)).
| 
| Build succeeded.
|   0 Warning(s)
|   0 Error(s)
| 
| Time Elapsed 00:00:03.18
[.NET/build]  βœ… Success - Main Test
[.NET/build] 🏁 Job succeeded
Enter fullscreen mode Exit fullscreen mode

Conclusions.

This approach might save you time and this is absolutely safe. Furthermore, you can set your custom configuration and add your own steps or jobs without changing on the main branch.
Happy coding!

Buy Me A Beer

Top comments (0)