Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 29 additions & 24 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,72 +1,77 @@
name: Go
name: Go CI

on:
push:
branches: ["main"]
branches: [ "main" ]
pull_request:
branches: ["main"]
branches: [ "main" ]
types: [opened, synchronize, reopened, ready_for_review]

jobs:
build:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:16-alpine
env:
POSTGRES_USER: app
POSTGRES_PASSWORD: app_password
POSTGRES_DB: productdb
# 映射一个随机主机端口,避免占用冲突
ports:
- 5432:5432
- 5432/tcp
options: >-
--health-cmd="pg_isready -U app -d productdb"
--health-cmd="pg_isready -U app -d productdb -h 127.0.0.1 || exit 1"
--health-interval=10s
--health-timeout=5s
--health-retries=5
--health-retries=10

steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.24.x'
go-version: '1.23.3'
check-latest: true
cache: true

- name: Tidy modules
run: go mod tidy

- name: Install psql client
run: |
sudo apt-get update
sudo apt-get install -y postgresql-client

- name: Wait for Postgres
- name: Wait for Postgres (by health)
env:
PGPASSWORD: app_password
PGPORT: ${{ job.services.postgres.ports['5432'] }} # 映射到的主机端口
run: |
for i in {1..30}; do
if pg_isready -h localhost -p 5432 -U app -d productdb; then
echo "Postgres is ready"; break; fi; sleep 2; done
pg_isready -h localhost -p 5432 -U app -d productdb
for i in {1..60}; do
if pg_isready -h 127.0.0.1 -p "$PGPORT" -U app -d productdb; then
echo "Postgres is ready"; exit 0
fi
sleep 2
done
echo "Postgres not ready after timeout" >&2
exit 1

- name: Apply migrations (psql)
env:
PGPASSWORD: app_password
PGPORT: ${{ job.services.postgres.ports['5432'] }}
run: |
psql -v ON_ERROR_STOP=1 -h localhost -p 5432 -U app -d productdb -f apps/product-query-svc/adapters/outbound/postgres/migrations/000001_init.up.sql
psql -v ON_ERROR_STOP=1 -h localhost -p 5432 -U app -d productdb -f apps/product-query-svc/adapters/outbound/postgres/migrations/000002_seed_test_data.up.sql
psql -v ON_ERROR_STOP=1 -h 127.0.0.1 -p "$PGPORT" -U app -d productdb -f apps/product-query-svc/adapters/outbound/postgres/migrations/000001_init.up.sql
psql -v ON_ERROR_STOP=1 -h 127.0.0.1 -p "$PGPORT" -U app -d productdb -f apps/product-query-svc/adapters/outbound/postgres/migrations/000002_seed_test_data.up.sql

- name: Test (integration: Postgres endpoints)
- name: Test integration Postgres endpoints
env:
DATABASE_URL: postgres://app:app_password@localhost:5432/productdb?sslmode=disable
run: |
go test -v ./test/... -run Postgres
DATABASE_URL: postgres://app:app_password@127.0.0.1:${{ job.services.postgres.ports['5432'] }}/productdb?sslmode=disable
run: go test -v ./test/... -run Postgres

- name: Build
run: go build -v ./...

- name: Test (all)
env:
DATABASE_URL: postgres://app:app_password@localhost:5432/productdb?sslmode=disable
run: go test -v ./...
DATABASE_URL: postgres://app:app_password@127.0.0.1:${{ job.services.postgres.ports['5432'] }}/productdb?sslmode=disable
run: go test -v ./...
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ require (
github.com/go-openapi/swag/jsonname v0.24.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down
Loading