208 lines
7.8 KiB
YAML
208 lines
7.8 KiB
YAML
# Reusable .NET 8 CI pipeline for this Gitea instance.
|
|
#
|
|
# Every constraint below is measured, not assumed — see the source of truth at
|
|
# CICD/platform/observed-state.md in the marketing monorepo:
|
|
# * service containers are reached BY NAME on the runner's per-job network;
|
|
# never publish 5432/6379, the runner host already uses them
|
|
# * the host is 2 vCPU / 8 GB shared with Gitea, so every `dotnet test` caps
|
|
# testhost to one core or the service Postgres crashes (57P03)
|
|
# * artifact actions are @v4 (both ends together — v3 and v4 do not interoperate)
|
|
# * `builtin:checkout` needs no Node runtime and no github.com round-trip
|
|
#
|
|
# Callers that still use Testcontainers (especially RabbitMQ) leave this workflow's
|
|
# Postgres/Redis services idle — extend this workflow with inputs rather than forking.
|
|
# RabbitMQ cannot be a workflow `services:` entry on this fleet at all; see
|
|
# node-service-ci.yml and observed-state.md.
|
|
#
|
|
# Caller:
|
|
# jobs:
|
|
# backend:
|
|
# uses: infra/ci-workflows/.gitea/workflows/dotnet8-ci.yml@v1
|
|
# with:
|
|
# working_directory: GrowthOS
|
|
# solution: GrowthOS.sln
|
|
# test_projects: |
|
|
# tests/Foo.UnitTests/Foo.UnitTests.csproj
|
|
# tests/Foo.IntegrationTests/Foo.IntegrationTests.csproj
|
|
name: dotnet8-ci
|
|
|
|
on:
|
|
workflow_call:
|
|
inputs:
|
|
runs_on:
|
|
description: "Runner label. One label only — multi-label runs-on is unreliable."
|
|
type: string
|
|
default: ubuntu-latest
|
|
working_directory:
|
|
description: "Directory holding the solution, relative to the repo root."
|
|
type: string
|
|
default: "."
|
|
solution:
|
|
description: "Solution file, relative to working_directory."
|
|
type: string
|
|
required: true
|
|
dotnet_version:
|
|
type: string
|
|
default: "8.0.x"
|
|
cache_dependency_path:
|
|
description: "Glob for setup-dotnet's NuGet cache key."
|
|
type: string
|
|
default: "**/*.csproj"
|
|
postgres_image:
|
|
type: string
|
|
default: "postgres:16"
|
|
postgres_db:
|
|
type: string
|
|
default: "app_test"
|
|
redis_image:
|
|
type: string
|
|
default: "redis:7"
|
|
pre_build_command:
|
|
description: "Optional shell run at the REPO ROOT before restore (schema checks, codegen)."
|
|
type: string
|
|
default: ""
|
|
isolated_test_projects:
|
|
description: |
|
|
Test projects that must NOT see the service database — typically unit tests on an
|
|
in-memory provider. Run first, with ConnectionStrings__* blanked, because a context
|
|
factory (or a stray UseNpgsql) will otherwise connect to the live Postgres and retry
|
|
until it thrashes the runner. One .csproj path per line.
|
|
type: string
|
|
default: ""
|
|
test_projects:
|
|
description: "Test projects that DO use the service containers. One .csproj path per line."
|
|
type: string
|
|
required: true
|
|
test_env:
|
|
description: "Optional extra KEY=VALUE lines exported for every test step."
|
|
type: string
|
|
default: ""
|
|
blame_hang_timeout:
|
|
description: "Kill testhost if a test stops completing."
|
|
type: string
|
|
default: "2m"
|
|
|
|
jobs:
|
|
build-and-test:
|
|
runs-on: ${{ inputs.runs_on }}
|
|
defaults:
|
|
run:
|
|
working-directory: ${{ inputs.working_directory }}
|
|
services:
|
|
postgres:
|
|
image: ${{ inputs.postgres_image }}
|
|
env:
|
|
POSTGRES_USER: postgres
|
|
POSTGRES_PASSWORD: postgres
|
|
POSTGRES_DB: ${{ inputs.postgres_db }}
|
|
# --shm-size is NOT stripped by the runner (verified on Runner 4.0).
|
|
# pg_isready can pass while Postgres is still in crash recovery, so the
|
|
# start-period is generous and tests must wait for queries, not health.
|
|
options: >-
|
|
--shm-size=256mb
|
|
--health-cmd "pg_isready -U postgres -d ${{ inputs.postgres_db }}"
|
|
--health-interval 2s
|
|
--health-timeout 5s
|
|
--health-retries 30
|
|
--health-start-period=20s
|
|
redis:
|
|
image: ${{ inputs.redis_image }}
|
|
options: >-
|
|
--health-cmd "redis-cli ping"
|
|
--health-interval 5s
|
|
--health-timeout 3s
|
|
--health-retries 10
|
|
env:
|
|
ASPNETCORE_ENVIRONMENT: Testing
|
|
ConnectionStrings__Postgres: "Host=postgres;Port=5432;Database=${{ inputs.postgres_db }};Username=postgres;Password=postgres;SSL Mode=Disable"
|
|
ConnectionStrings__Redis: "redis:6379"
|
|
# GITEA_TOKEN is injected into every job and job containers CAN reach this
|
|
# Gitea. Product code holding the token will make live API calls from CI
|
|
# unless it is blanked. Tests are fixtures, not writes.
|
|
GITEA_TOKEN: ""
|
|
steps:
|
|
- uses: builtin:checkout
|
|
|
|
- name: Pre-build checks
|
|
if: inputs.pre_build_command != ''
|
|
working-directory: .
|
|
run: ${{ inputs.pre_build_command }}
|
|
|
|
- name: Setup .NET
|
|
uses: actions/setup-dotnet@v4
|
|
with:
|
|
dotnet-version: ${{ inputs.dotnet_version }}
|
|
cache: true
|
|
cache-dependency-path: ${{ inputs.working_directory }}/${{ inputs.cache_dependency_path }}
|
|
|
|
- name: Restore
|
|
run: dotnet restore ${{ inputs.solution }}
|
|
|
|
- name: Build
|
|
run: dotnet build ${{ inputs.solution }} -c Release --no-restore
|
|
|
|
# Two steps, not one, because the connection strings differ. Both loop rather
|
|
# than becoming separate jobs: extra jobs queue at capacity 1 rather than
|
|
# running in parallel, and --no-build means the cost is the test run itself.
|
|
- name: Test (isolated — no service database)
|
|
if: inputs.isolated_test_projects != ''
|
|
env:
|
|
ConnectionStrings__Postgres: ""
|
|
ConnectionStrings__Redis: ""
|
|
DOTNET_PROCESSOR_COUNT: "1"
|
|
DOTNET_gcServer: "0"
|
|
DOTNET_GCHeapCount: "1"
|
|
run: |
|
|
set -euo pipefail
|
|
while IFS= read -r line; do
|
|
[ -n "$line" ] || continue
|
|
export "${line?}"
|
|
done <<< "${{ inputs.test_env }}"
|
|
|
|
while IFS= read -r proj; do
|
|
[ -n "$proj" ] || continue
|
|
name="$(basename "$proj" .csproj)"
|
|
echo "::group::$name"
|
|
dotnet test "$proj" -c Release --no-build \
|
|
--filter "Category!=Live" \
|
|
--blame-hang-timeout ${{ inputs.blame_hang_timeout }} \
|
|
--logger "trx;LogFileName=${name}.trx" \
|
|
--results-directory ./TestResults \
|
|
-- xUnit.MaxParallelThreads=1
|
|
echo "::endgroup::"
|
|
done <<< "${{ inputs.isolated_test_projects }}"
|
|
|
|
- name: Test (against the service containers)
|
|
env:
|
|
# 2 vCPU shared with Gitea. Uncapped testhost takes both cores, the
|
|
# service Postgres container dies, and later tests fail with 57P03.
|
|
DOTNET_PROCESSOR_COUNT: "1"
|
|
DOTNET_gcServer: "0"
|
|
DOTNET_GCHeapCount: "1"
|
|
run: |
|
|
set -euo pipefail
|
|
while IFS= read -r line; do
|
|
[ -n "$line" ] || continue
|
|
export "${line?}"
|
|
done <<< "${{ inputs.test_env }}"
|
|
|
|
while IFS= read -r proj; do
|
|
[ -n "$proj" ] || continue
|
|
name="$(basename "$proj" .csproj)"
|
|
echo "::group::$name"
|
|
dotnet test "$proj" -c Release --no-build \
|
|
--filter "Category!=Live" \
|
|
--blame-hang-timeout ${{ inputs.blame_hang_timeout }} \
|
|
--logger "trx;LogFileName=${name}.trx" \
|
|
--results-directory ./TestResults \
|
|
-- xUnit.MaxParallelThreads=1
|
|
echo "::endgroup::"
|
|
done <<< "${{ inputs.test_projects }}"
|
|
|
|
- name: Publish test results
|
|
if: always()
|
|
uses: actions/upload-artifact@v4
|
|
with:
|
|
name: test-results
|
|
path: ${{ inputs.working_directory }}/TestResults
|