[MAC] How locally run GitHub Actions? It's easy.
Tags: dotnet, docker, githubactions, macos
Published: 2023-05-26 15:12:40

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](https://github.com/nektos/act?utm_source=telegram&utm_medium=social&utm_campaign=deflope&utm_term=kazhdyy-kto-kogda-libo-otlazhival-cicd-pay&utm_content=14722989).

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:
```Bash
$brew install act
```

For further work, we need to install GitHub CLI for launching commands from the Terminal.
```bash
$brew install gh
```]
The following step, it needs to authenticate on GitHub. For this, you should run this command:
```bash
$gh auth login
```

There will be suggestions for selecting a type of service. I'll choose GitHub.com:
```bash
? What account do you want to log into? [Use arrows to move, type to filter] > GitHub.com GitHub Enterprise Server
```

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.
```bash
? What is your preferred protocol for Git operations? [Use arrows to move, type to filter] > HTTPS SSH
```

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.
```bash
? How would you like to authenticate GitHub CLI? [Use arrows to move, type to filter] > Login with a web browser Paste an authentication token
```

If everything is made correctly, you'll see this message:
```bash
! First copy your one-time code: [YOUR_ONE_TIME_CODE] Press Enter to open github.com in your browser...
```

Finally, you'll see this report:
```bash
✓ Authentication complete. - gh config set -h github.com git_protocol https ✓ Configured git protocol ✓ Logged in as SergKorol
```

In the following step, you need to install the extension to GitHub:
```bash
gh extension install https://github.com/nektos/gh-act
```

If everything is correct, you'll see this message:
```bash
✓ Installed extension https://github.com/nektos/gh-act
```

Further, go to your favorite folder with projects, and let's clone the project sample:
```bash
git clone https://github.com/SergKorol/ManagementSystem.git
```

Let's check and go to the folder with the project:
```bash
cd ManagementSystem
```

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

And you'll see a configuration document:
```bash
# 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
```

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.
```bash
act -l
```

Something like that:
```bash
Stage Job ID Job name Workflow name Workflow file Events      
0   build  build   .NET      dotnet.yml   push,pull_request
```

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

It'll be so:
```bash
? 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
```

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.
```bash
act
```

In the end, you'll get a report like this:
```bash
[.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
```

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:
```yaml
greeting:
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v3
    - name: Say hello
      run: echo "hello world"
```

If you run this command, you can launch a specific job:
```bash
act -j greeting
```

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:
```yaml
- name: Setup .NET
  uses: actions/setup-dotnet@v3
  with:
    dotnet-version: 7.0.x
```

And paste this code:
```yaml
build:
  runs-on: ubuntu-latest
  container:
    image: mcr.microsoft.com/dotnet/sdk:7.0
    options: --user root
  steps:
```

And it works:
```bash
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
```

## 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!

<a href="https://www.buymeacoffee.com/b9z99kntvtQ" target="_blank"><img src="http://dartfordwaffler.co.uk/wp-content/uploads/2021/03/buy-me-a-beer.jpg" alt="Buy Me A Beer" style="height: 60px !important;width: 317px !important;" ></a>