diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index a118dcf..d1cba6e 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -15,6 +15,8 @@ jobs: verify: runs-on: ubuntu-latest timeout-minutes: 20 + env: + COMPOSE_FILE: compose.ci.yaml steps: - name: Checkout exact commit run: | @@ -55,21 +57,23 @@ jobs: - name: Prepare local stack run: | cp lab2.env.example .env - ./scripts/init-local-secrets.sh docker compose config --quiet docker compose up -d --build --wait --wait-timeout 120 + network=$(docker inspect --format '{{range $name, $_ := .NetworkSettings.Networks}}{{$name}}{{end}}' "$(docker compose ps -q api)") + docker network connect "$network" "$HOSTNAME" + printf '%s\n' "$network" > .ci-network - name: Public smoke - run: make test + run: make test BASE_URL=http://api:8080 - name: PostgreSQL and Valkey integration tests run: docker compose --profile tests run --rm --build tests - name: Dependency outage recovery - run: go run ./cmd/dependencycheck + run: go run ./cmd/dependencycheck -base-url=http://api:8080 - name: Persistence test - run: make test-persistence ACTION_SCRIPT=scripts/restart.sh + run: make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://api:8080 - name: Container logs if: failure() @@ -77,4 +81,6 @@ jobs: - name: Stop local stack if: always() - run: if [ -f .env ]; then docker compose --profile tests down -v --remove-orphans; fi + run: | + if [ -f .ci-network ]; then docker network disconnect "$(cat .ci-network)" "$HOSTNAME"; fi + if [ -f .env ]; then docker compose --profile tests down -v --remove-orphans; fi diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 789b239..ae9fc07 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -16,6 +16,8 @@ jobs: if: github.ref == 'refs/heads/main' && inputs.confirmation == 'DEPLOY' runs-on: ubuntu-latest timeout-minutes: 20 + env: + COMPOSE_FILE: compose.ci.yaml steps: - name: Checkout exact commit run: | @@ -49,16 +51,20 @@ jobs: - name: Verify release stack run: | cp lab2.env.example .env - ./scripts/init-local-secrets.sh docker compose up -d --build --wait --wait-timeout 120 - make test + network=$(docker inspect --format '{{range $name, $_ := .NetworkSettings.Networks}}{{$name}}{{end}}' "$(docker compose ps -q api)") + docker network connect "$network" "$HOSTNAME" + printf '%s\n' "$network" > .ci-network + make test BASE_URL=http://api:8080 docker compose --profile tests run --rm --build tests - go run ./cmd/dependencycheck - make test-persistence ACTION_SCRIPT=scripts/restart.sh + go run ./cmd/dependencycheck -base-url=http://api:8080 + make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://api:8080 - name: Stop release stack if: always() - run: if [ -f .env ]; then docker compose --profile tests down -v --remove-orphans; fi + run: | + if [ -f .ci-network ]; then docker network disconnect "$(cat .ci-network)" "$HOSTNAME"; fi + if [ -f .env ]; then docker compose --profile tests down -v --remove-orphans; fi - name: Configure SSH run: | diff --git a/compose.ci.yaml b/compose.ci.yaml new file mode 100644 index 0000000..0f17d66 --- /dev/null +++ b/compose.ci.yaml @@ -0,0 +1,63 @@ +services: + postgres: + image: postgres:17.6-alpine3.22 + environment: + POSTGRES_USER: messenger + POSTGRES_DB: messenger + POSTGRES_PASSWORD: ci-postgres-password + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 5s + timeout: 3s + retries: 20 + + valkey: + image: valkey/valkey:8.1.4-alpine + command: ["valkey-server", "--appendonly", "yes", "--appendfsync", "everysec", "--requirepass", "ci-valkey-password"] + volumes: + - valkey_data:/data + healthcheck: + test: ["CMD", "valkey-cli", "--no-auth-warning", "-a", "ci-valkey-password", "ping"] + interval: 5s + timeout: 3s + retries: 20 + + api: + build: . + env_file: .env + entrypoint: ["/bin/sh", "-eu", "-c"] + command: + - | + printf '%s' "$$DATABASE_URL" > /tmp/database_url + printf '%s' "$$CACHE_URL" > /tmp/cache_url + exec /app/messenger + environment: + DATABASE_URL: postgres://messenger:ci-postgres-password@postgres:5432/messenger?sslmode=disable + DATABASE_URL_FILE: /tmp/database_url + CACHE_URL: redis://default:ci-valkey-password@valkey:6379/0 + CACHE_URL_FILE: /tmp/cache_url + depends_on: + postgres: + condition: service_healthy + valkey: + condition: service_healthy + + tests: + profiles: ["tests"] + build: + context: . + target: test + environment: + TEST_DATABASE_URL: postgres://messenger:ci-postgres-password@postgres:5432/messenger?sslmode=disable + TEST_CACHE_URL: redis://default:ci-valkey-password@valkey:6379/0 + depends_on: + postgres: + condition: service_healthy + valkey: + condition: service_healthy + +volumes: + postgres_data: + valkey_data: