我是 Github Actions 和 Docker 的新手。现在我正在尝试设置我的第一个 CI 并收到此错误:
`failed to connect to `host=/var/run/postgresql user= database=`: dial error (dial unix /var/run/postgresql/.s.PGSQL.5432: connect: no such file or directory)`
但本地一切正常。
我的 ci.yaml 文件:
name: CI
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
POSTGRES_DB: sso_dev
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '1.21'
- name: Install golang-migrate
run: |
curl -L https://github.com/golang-migrate/migrate/releases/download/v4.12.2/migrate.linux-amd64.tar.gz | tar xvz
sudo mv migrate.linux-amd64 /usr/bin/migrate
which migrate
- name: Install dependencies
run: go mod download
- name: Create /config directory
run: mkdir -p ./config
- name: Create .env file
run: echo "${{ secrets.ENV_CONFIG }}" > ./config/.env
- name: Display .env file
run: cat ./config/.env
- name: Wait for PostgreSQL to be ready
run: sleep 10
- name: Check PostgreSQL connection
run: pg_isready -h localhost -p 5432
- name: Run tests
run: make test-all-app
- name: Notify results
run: echo "Tests completed!"
在 ENV_CONFIG 中我有以下环境变量:
GRPC_SERVER_PORT=44044
GRPC_SERVER_TIMEOUT=30s
DB_HOST=localhost
DB_PORT=5432
DB_NAME=sso_dev
DB_USER=root
DB_PASSWORD=password
DB_SSL_MODE=disable
DB_CONN_URL="postgres://$DB_USER:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME?sslmode=$DB_SSL_MODE"
DB_CONN_POOL_SIZE=10
DB_READ_TIMEOUT=5s
DB_WRITE_TIMEOUT=5s
DB_IDLE_TIMEOUT=60s
DB_DIAL_TIMEOUT=10s
我不明白根本原因在哪里。我询问了 ChatGPT,得到的答案是应用程序正在尝试通过 Unix 套接字连接到 PostgreSQL,但找不到套接字文件。但如果我没记错的话,我使用 tcp 连接...
也许原因是我在代码中创建数据库连接的方式(我使用 Golang)?
package postgres
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
_ "github.com/jackc/pgx/v5/stdlib" // Import for side effects
"github.com/rshelekhov/sso/internal/config"
"github.com/rshelekhov/sso/internal/storage/postgres/sqlc"
"net"
)
// NewStorage creates a new Postgres storage
func NewStorage(cfg *config.ServerSettings) (*pgxpool.Pool, error) {
const method = "storage.postgres.NewStorage"
poolCfg, err := pgxpool.ParseConfig(cfg.Postgres.ConnURL)
if err != nil {
return nil, fmt.Errorf("%s: failed to parse config: %w", method, err)
}
poolCfg.MaxConnLifetime = cfg.Postgres.IdleTimeout
poolCfg.MaxConns = int32(cfg.Postgres.ConnPoolSize)
dialer := &net.Dialer{KeepAlive: cfg.Postgres.DialTimeout}
dialer.Timeout = cfg.Postgres.DialTimeout
poolCfg.ConnConfig.DialFunc = dialer.DialContext
pool, err := pgxpool.NewWithConfig(context.Background(), poolCfg)
if err != nil {
return nil, fmt.Errorf("%s: failed to create pgx connection pool: %w", method, err)
}
return pool, nil
}
您保证睡眠时间超过10秒吗? 如果不是最好将其添加到 yaml 文件中。但您的主机配置也可能存在问题,例如,如果您依赖 LOCALHOST 来连接数据库,请确保服务正确公开并且应用程序可以从 CI 环境访问它..
我将服务日志和
pg_isready
添加到您的yaml文件中,它看起来像这样
name: CI
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
POSTGRES_DB: sso_dev
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '1.21'
- name: Install golang-migrate
run: |
curl -L https://github.com/golang-migrate/migrate/releases/download/v4.12.2/migrate.linux-amd64.tar.gz | tar xvz
sudo mv migrate.linux-amd64 /usr/bin/migrate
which migrate
- name: Install dependencies
run: go mod download
- name: Create /config directory
run: mkdir -p ./config
- name: Create .env file
run: echo "${{ secrets.ENV_CONFIG }}" > ./config/.env
- name: Display .env file
run: cat ./config/.env
- name: Wait for PostgreSQL to be ready
run: |
for i in {1..20}; do
pg_isready -h localhost -p 5432 && break
sleep 3
done
- name: Check PostgreSQL logs
run: docker logs ${{ job.services.postgres.id }}
- name: Run tests
run: make test-all-app
- name: Notify results
run: echo "Tests completed!"