跳至主要内容

GitHub Action 除錯案例

錯誤 1

錯誤資訊關鍵詞:cannot find package
問題描述:儘管專案實際上有正確引用 context 套件,依舊出現下列錯誤訊息

Error: db/sqlc/db.go:8:2: cannot find package "context" in any of:
/opt/hostedtoolcache/go/1.2.2/x64/src/pkg/context (from $GOROOT)
($GOPATH not set)
make: *** [Makefile:20: test] Error 1
Error: Process completed with exit code 2.

解決方法:修改 Workflow
參考文件

Workflow 修改前
# 修改前
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.20
Workflow 修改後
# 修改後
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version-file: 'go.mod'
- run: go version

錯誤 2

錯誤資訊關鍵描述:dial tcp [::1]:5432: connect: connection refused
問題描述:5432 是 PostgreSQL 預設埠號,錯誤訊息顯示連線遭到拒絕。這表示 Workflow 需要建立 PostgreSQL service。
解決方法:參考以下文件修改 Workflow。因為要在 Docker 容器內執行,所以也用得上 Docker Hub 的文件。

PostgreSQL service 設定範例
# 在 jobs: 以下找到適當位置編寫以下內容
services:
postgres:
image: postgres:[your image version]
# Provide the password for postgres
env:
POSTGRES_USER: root
POSTGRES_PASSWORD: your_password
POSTGRES_DB: your_db_name
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
# Maps tcp port 5432 on service container to the host
ports:
- 5432:5432