Skip to content

simplify logic

simplify logic #747

name: Go Tests (with Database)
on:
push:
branches: [main]
pull_request:
branches: [main]
paths:
- 'apps/workspace-engine/**'
- '.github/workflows/golang-tests-with-db.yaml'
jobs:
workspace-engine-tests:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
# Add PostgreSQL service for integration tests
services:
postgres:
image: postgres:18
env:
POSTGRES_USER: ctrlplane_test
POSTGRES_PASSWORD: test_password
POSTGRES_DB: ctrlplane_test
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
defaults:
run:
working-directory: apps/workspace-engine
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.24"
- name: Cache Go modules
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install dependencies
run: go mod download
- name: Wait for PostgreSQL
run: |
until pg_isready -h localhost -p 5432 -U ctrlplane_test; do
echo "Waiting for postgres..."
sleep 2
done
- name: Set up Node.js for migrations
uses: actions/setup-node@v4
with:
node-version: '22'
- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 10
- name: Install dependencies for migrations
working-directory: ./
run: |
pnpm install --filter @ctrlplane/db --filter @ctrlplane/validators --filter @ctrlplane/logger --filter @ctrlplane/secrets --frozen-lockfile
- name: Build packages needed for migrations
working-directory: ./
run: |
pnpm --filter @ctrlplane/validators build
pnpm --filter @ctrlplane/logger build
pnpm --filter @ctrlplane/secrets build
pnpm --filter @ctrlplane/db build
- name: Run migrations
working-directory: ./packages/db
env:
POSTGRES_URL: "postgresql://ctrlplane_test:test_password@localhost:5432/ctrlplane_test?sslmode=disable"
run: npx tsx ./migrate.ts
- name: Run DB package tests
env:
POSTGRES_URL: "postgresql://ctrlplane_test:test_password@localhost:5432/ctrlplane_test?sslmode=disable"
run: go test -v -race -coverprofile=coverage.out -covermode=atomic ./pkg/db/...
- name: Generate coverage report
run: go tool cover -html=coverage.out -o coverage.html
- name: Calculate coverage percentage
id: coverage
run: |
COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}')
echo "percentage=$COVERAGE" >> $GITHUB_OUTPUT
echo "Total coverage: $COVERAGE"
- name: Upload coverage report
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: |
apps/workspace-engine/coverage.out
apps/workspace-engine/coverage.html
- name: Comment coverage on PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v6
with:
script: |
const coverage = '${{ steps.coverage.outputs.percentage }}';
const comment = `## 📊 DB Package Test Coverage
**pkg/db** coverage: \`${coverage}\`
[View detailed coverage report in artifacts](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});