How to test GitHub Actions

This article is converted by SimpRead, original at zhuanlan.zhihu.com

  1. Preface

When writing github action, we often need to test the action code, which requires us to constantly push commits to trigger the workflow for testing. This operation is very cumbersome. Using act, we can run actions locally for testing, greatly improving efficiency.

The main content of this article is a record of using act, mainly for future reference.

  1. Environment

Environment: macOS 12.4

  1. Installation

3.1 Install Docker

brew install docker

3.2 Install act

brew install act

3.3 Repo Configuration

We clone the repo where the action is located to local, and we directly create workflows under this repo for testing.

The important part is that we need to let the workflow run the current action we want to test.

Adjust the uses: configuration in the yml file

name: Continuous Deploy 
on: [push] 

jobs:
  deploy_job:
    runs-on: ubuntu-latest  
    name: sftp
    steps:
      - name: Checkout    
        uses: actions/checkout@v2 

      - name: deploy file to server
        uses:  ./  # Note here we directly refer to the action in this repo
        with:  
            .....
  1. Using act

cd into the repo directory. If you have an Intel-based Mac, run the act command directly.

If you have an M-series Mac, please run act --container-architecture linux/amd64

At the first run, you can choose the docker image you want to install as needed.

  1. Some parameters for act

5.1 Secret

github action can read secrets saved in the repo, but when using act, these secrets cannot be accessed.

We can create a file named act_secret_file locally on the Mac to save passwords (you can name it yourself), the format is simply key-value pairs.

SERVER_IP="xxxxxx"
SSH_PRIVATE_KEY="xxxxxx"

When running, provide the file path

act --secret-file /Users/wl/act_secret_file

Secrets inside actions can then be read normally.

  1. Rebuild (Recommended)

Sometimes running just act cannot execute the latest code, so we can add the rebuild parameter.

This ensures that we are running the latest code.

act --rebuild --secret-file /Users/wl/act_secret_file

X. Finally

  1. The previously written SFTP-Deploy-Action was tested using act, avoiding the hassle of uploading for each test.

https://github.com/wlixcc/SFTP-Deploy-Action

  1. If you encounter the error executable file not found in $PATH, out:, you can refer to the following answer

https://stackoverflow.com/questions/65896681/exec-docker-credential-desktop-exe-executable-file-not-found-in-path

  1. If you encounter the following problem
Error: failed to start container: Error response from daemon: error while creating mount source path '/host_mnt/Users/xxx/.docker/run/docker.sock': mkdir /host_mnt/Users/xxx/.docker/run/docker.sock: operation not supported

We need to add the line --container-daemon-socket - to the ~/.actrc file, then run the act command.

https://github.com/nektos/act/issues/2239

  1. In the Dockerfile of the action, we can pre-install some required packages
RUN apk update
RUN apk add --no-cache rsync sshpass openssh