返回

如何在 Windows 上使用 Act 运行 GitHub Actions?

开发配置

使用 Act 运行 GitHub Actions 工作流:问题与解决方案

在 Windows 系统中使用 Act 运行 GitHub Actions 工作流时遇到的问题及其解决方案。Act 是一个在本地运行 GitHub Actions 的工具,非常适合在提交前测试工作流。

遇到的问题

在尝试使用 Act 运行工作流时,我遇到了以下几个问题:

  1. Chocolatey 安装报错

    警告: An existing Chocolatey installation was detected. Installation will not continue. This script will not overwrite existing installations.
    
  2. Docker 相关错误

    time="2024-07-16T14:35:34+08:00" level=info msg="Using docker host 'npipe:////./pipe/docker_engine', and daemon socket 'npipe:////./pipe/docker_engine'"
    Error: CreateFile C:\Windows\system32\.github\workflows: The system cannot find the path specified.
    
  3. Docker Desktop 未正确配置

    Docker Desktop
    Docker Desktop - Windows containers not enabled
    Containers feature is disabled.
    
  4. 运行 Act 时的 Docker 凭据错误

    Error: error getting credentials - err: exec: "docker-credential-desktop": executable file not found in %PATH%, out: ``
    
  5. Git 操作错误

    fatal: --unshallow on a complete repository does not make sense
    

解决方案

1. Chocolatey 安装报错

如果 Chocolatey 已经安装但无法继续安装新版本,可以手动删除现有安装目录并重新安装:

Remove-Item -Recurse -Force C:\ProgramData\chocolatey
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

2. Docker 相关错误

确保 Docker Desktop 已启动,并在 PowerShell 中正确配置 Docker 环境变量:

[Environment]::SetEnvironmentVariable("DOCKER_HOST", "npipe:////./pipe/docker_engine", "User")

重启 PowerShell 并运行 docker info 检查 Docker 状态。

3. Docker Desktop 未正确配置

启用 Hyper-V 和容器功能,并重启计算机:

Enable-WindowsOptionalFeature -Online -FeatureName $("Microsoft-Hyper-V", "Containers") -All

确认 Docker Desktop 运行在 Linux 容器模式,右键点击任务栏中的 Docker 图标,选择 "Switch to Linux Containers..."。

4. Docker 凭据错误

手动登录 Docker 以配置凭据:

docker login

确保 Docker Desktop 安装路径已添加到系统 PATH 环境变量中,例如 C:\Program Files\Docker\Docker\resources\bin

5. Git 操作错误

.github/workflows 中检查并调整 manual.yml 文件,确保在浅克隆的情况下使用 --unshallow 参数:

steps:
- name: Checkout repository
  uses: actions/checkout@v4
  with:
    fetch-depth: 0

- name: Fetch all history for all tags and branches
  run: |
    if [ -f .git/shallow ]; then
      git fetch --prune --unshallow
    else
      git fetch --prune
    fi

- name: Merge upstream
  run: |
    git remote add upstream https://github.com/upstream/repo.git
    git fetch upstream
    git merge upstream/main

最后

在解决这些问题后,我成功使用 Act 运行了 GitHub Actions 工作流。希望这些解决方案对遇到类似问题的你有所帮助。如果问题仍然存在,请提供更多详细信息以便进一步诊断。