# 如何测试github action

**URL:** <https://forum.beginner.center/t/topic/1757>\
**Category:** 🛠工具与编程\
**Tags:** github\
**Created:** [2025 年2 月 18 日 10:49 UTC](https://forum.beginner.center/t/topic/1757 "2025-02-18T10:49:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

作者： ![doggie](https://forum.beginner.center/user_avatar/forum.beginner.center/doggie/32/550_2.png) [@doggie](https://forum.beginner.center/u/doggie)\
发布日期： [2025 年2 月 18 日 10:49 UTC](https://forum.beginner.center/t/topic/1757/1 "2025-02-18T10:49:33Z")

</div>

> 本文由 [简悦 SimpRead](http://ksria.com/simpread/) 转码， 原文地址 [zhuanlan.zhihu.com](https://zhuanlan.zhihu.com/p/535798453)

1. 前言

* * *

在编写`github action`的时候, 我们经常需要测试`action` 的代码，这就需要我们不断的进行 push 提交触发 workflow 进行测试，这样操作非常繁琐。 利用 [act](https://github.com/nektos/act) 我们可以在本地直接运行 action 进行测试，极大的提高了效率。

> 本文主要的内容就是对 [act](https://github.com/nektos/act) 使用做一个记录，偏于以后参考

1. 环境

* * *

环境： macOS12.4

1. 安装

* * *

### 3.1 安装 Docker

```auto
brew install docker

```

### 3.2 安装 act

```auto
brew install act

```

### 3.3 Repo 配置

我们把`action` 所在的`repo`克隆到本地，我们直接在此`repo`下创建 workflow 进行测试

![](https://forum.beginner.center/images/transparent.png)

其中比较重要的是我们需要让 workflow 运行我们的当前需要测试`action`

调整`yml` 文件下的`uses:` 配置

```auto
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: ./ #这里要注意我们直接引用此repo下的action
        with:  
            .....

```

1. 使用 act

* * *

`cd`到 repo 目录下，如果只 intel 芯片的 mac 我们直接运行 `act` 命令

> 如果是 M 系列的 mac，请运行 `act --container-architecture linux/amd64`

首次运行我们可以根据需要选择我们需要安装的`docker image`

![](https://forum.beginner.center/images/transparent.png)

1. Act 使用的一些参数

* * *

### 5.1 Secret

`github action`可以读取保存在 repo 中的`secret` ，但是我们使用 act 的时候不能读取到这些`secret`

我们可以在 mac 本地创建一个名为`act_secret_file`保存密码 (这里名称你可以自己定义)，格式为键值对就行。

```auto
SERVER_IP="xxxxxx"
SSH_PRIVATE_KEY="xxxxxx"

```

在运行的时候我们带上文件路径

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

```

action 中的 secret 就可以正常被读取了

1. Rebuild(推荐)

* * *

直接运行 act 命名有时候不能执行到最新的代码，我们可以添加 `rebuild` 参数

这样确保我们运行的是最新的代码

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

```

## X. 最后

1. 以前写的 SFTP-Deploy-Action 就是用 `Act` 进行测试的，避免了每次都要上传测试的麻烦

[https://github.com/wlixcc/SFTP-Deploy-Action](https://github.com/wlixcc/SFTP-Deploy-Action)

1. 遇到`executable file not found in $PATH, out:` 问题，可以参考下面这个回答

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

1. 如果遇到下面这个问题

```auto
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

```

我们需要把`--container-daemon-socket -` 这行添加到`~/.actrc`这个文件下保存，然后在运行 act 命令。

[https://github.com/nektos/act/issues/2239](https://github.com/nektos/act/issues/2239)

1. 在`action`中的`Dockerfile`中我们可以提前安装一些需要的包

```auto
RUN apk update
RUN apk add --no-cache rsync sshpass openssh

```

---

<div class="post-metadata">

作者： ![doggie](https://forum.beginner.center/user_avatar/forum.beginner.center/doggie/32/550_2.png) [@doggie](https://forum.beginner.center/u/doggie)\
发布日期： [2025 年8 月 31 日 11:31 UTC](https://forum.beginner.center/t/topic/1757/2 "2025-08-31T11:31:27Z")

</div>

- [调试 github actions 的 4 种工具 - RUBLOG-分享我的生活](https://www.rxx0.com/software/diao-shi-github-actions-de-4-chong-gong-ju.html)
