Skip to content

Commit 1e3bea0

Browse files
committed
feat: enhance CI/CD pipeline with PostgreSQL health checks and coverage reporting
1 parent 598a990 commit 1e3bea0

1 file changed

Lines changed: 193 additions & 49 deletions

File tree

.github/workflows/rust.yml

Lines changed: 193 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,206 @@
1-
name: Rust
1+
name: CI/CD Pipeline
22

33
on:
44
push:
5-
branches: [ "main" ]
5+
branches: ["main"]
66
pull_request:
7-
branches: [ "main" ]
7+
branches: ["main"]
88

99
env:
1010
CARGO_TERM_COLOR: always
11-
DATABASE_URL: postgres://postgres:dev@localhost:5432/e2ee
11+
RUST_BACKTRACE: 1
1212

1313
jobs:
14-
build:
14+
test:
15+
name: Test
16+
runs-on: ubuntu-latest
17+
18+
services:
19+
postgres:
20+
image: postgres:17-alpine
21+
env:
22+
POSTGRES_USER: postgres
23+
POSTGRES_PASSWORD: dev
24+
POSTGRES_DB: e2ee
25+
ports:
26+
- 5432:5432
27+
options: >-
28+
--health-cmd pg_isready
29+
--health-interval 10s
30+
--health-timeout 5s
31+
--health-retries 5
32+
33+
steps:
34+
- name: Checkout code
35+
uses: actions/checkout@v4
36+
37+
- name: Setup Rust
38+
uses: dtolnay/rust-toolchain@stable
39+
with:
40+
components: rustfmt, clippy
41+
42+
- name: Cache Cargo dependencies
43+
uses: actions/cache@v4
44+
with:
45+
path: |
46+
~/.cargo/bin
47+
~/.cargo/registry/index
48+
~/.cargo/registry/cache
49+
~/.cargo/git/db
50+
target
51+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
52+
restore-keys: |
53+
${{ runner.os }}-cargo-
54+
55+
- name: Install PostgreSQL client
56+
run: sudo apt-get update && sudo apt-get install -y postgresql-client
57+
58+
- name: Wait for PostgreSQL
59+
run: |
60+
until pg_isready -h localhost -p 5432 -U postgres; do
61+
echo "Waiting for PostgreSQL..."
62+
sleep 2
63+
done
64+
65+
- name: Setup database schema
66+
env:
67+
PGPASSWORD: dev
68+
run: |
69+
psql -h localhost -U postgres -d e2ee -f sql_models/seed.sql
70+
71+
- name: Check formatting
72+
run: cargo fmt --all -- --check
73+
74+
- name: Clippy
75+
run: cargo clippy --all-targets --all-features -- -D warnings
76+
77+
- name: Build
78+
env:
79+
DATABASE_URL: postgres://postgres:dev@localhost:5432/e2ee
80+
run: cargo build --verbose
81+
82+
- name: Run tests
83+
env:
84+
DATABASE_URL: postgres://postgres:dev@localhost:5432/e2ee
85+
run: cargo test --verbose
86+
87+
docker:
88+
name: Build Docker Image
89+
runs-on: ubuntu-latest
90+
needs: test
91+
92+
steps:
93+
- name: Checkout code
94+
uses: actions/checkout@v4
95+
96+
- name: Set up Docker Buildx
97+
uses: docker/setup-buildx-action@v3
98+
99+
- name: Build Docker image
100+
uses: docker/build-push-action@v5
101+
with:
102+
context: .
103+
file: ./Dockerfile
104+
push: false
105+
tags: hushnet-backend:latest
106+
cache-from: type=gha
107+
cache-to: type=gha,mode=max
108+
109+
- name: Test Docker Compose
110+
run: |
111+
docker compose up -d
15112
113+
# Wait for services to be healthy
114+
echo "Waiting for services to be healthy..."
115+
timeout 60 bash -c 'until docker compose ps | grep -q "healthy"; do sleep 2; done'
116+
117+
# Test backend health
118+
curl -f http://localhost:8080/ || exit 1
119+
120+
echo "✅ Docker Compose test passed"
121+
122+
# Cleanup
123+
docker compose down -v
124+
125+
security:
126+
name: Security Audit
127+
runs-on: ubuntu-latest
128+
129+
steps:
130+
- name: Checkout code
131+
uses: actions/checkout@v4
132+
133+
- name: Setup Rust
134+
uses: dtolnay/rust-toolchain@stable
135+
136+
- name: Cache Cargo dependencies
137+
uses: actions/cache@v4
138+
with:
139+
path: |
140+
~/.cargo/bin
141+
~/.cargo/registry/index
142+
~/.cargo/registry/cache
143+
~/.cargo/git/db
144+
key: ${{ runner.os }}-cargo-audit-${{ hashFiles('**/Cargo.lock') }}
145+
146+
- name: Install cargo-audit
147+
run: cargo install cargo-audit || true
148+
149+
- name: Run security audit
150+
run: cargo audit
151+
152+
coverage:
153+
name: Code Coverage
16154
runs-on: ubuntu-latest
17155

156+
services:
157+
postgres:
158+
image: postgres:17-alpine
159+
env:
160+
POSTGRES_USER: postgres
161+
POSTGRES_PASSWORD: dev
162+
POSTGRES_DB: e2ee
163+
ports:
164+
- 5432:5432
165+
options: >-
166+
--health-cmd pg_isready
167+
--health-interval 10s
168+
--health-timeout 5s
169+
--health-retries 5
170+
18171
steps:
19-
- uses: actions/checkout@v4
20-
- name: Build Postgres image
21-
run: docker build -t postgres-ee -f Dockerfile .
22-
23-
- name: Start Postgres container
24-
run: |
25-
docker run -d --name postgres-ee \
26-
-p 5432:5432 \
27-
-e POSTGRES_USER=postgres \
28-
-e POSTGRES_PASSWORD=dev \
29-
-e POSTGRES_DB=e2ee \
30-
postgres
31-
32-
until docker exec postgres-ee pg_isready -U postgres > /dev/null 2>&1; do
33-
echo "Waiting for Postgres to be ready..."
34-
sleep 2
35-
done
36-
echo "Postgres is ready."
37-
- name: Cache Cargo
38-
uses: actions/cache@v4
39-
with:
40-
path: |
41-
~/.cargo/bin
42-
~/.cargo/registry/index
43-
~/.cargo/registry/cache
44-
~/.cargo/git/db
45-
target
46-
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
47-
restore-keys: |
48-
${{ runner.os }}-cargo-
49-
- name: Build
50-
run: cargo build --verbose
51-
- name: Run tests
52-
run: cargo test --verbose
53-
- name: Stop Postgres container
54-
if: always()
55-
run: docker rm -f postgres-ee
56-
- name: Format code
57-
env:
58-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
59-
run: |
60-
cargo fmt
61-
git diff --exit-code || { git config user.name "GitHub Actions"; git config user.email "actions@github.com"; git add .; git commit -m "Auto-fix ESLint issues"; git push origin $GITHUB_REF; }
62-
172+
- name: Checkout code
173+
uses: actions/checkout@v4
174+
175+
- name: Setup Rust
176+
uses: dtolnay/rust-toolchain@stable
177+
178+
- name: Install PostgreSQL client
179+
run: sudo apt-get update && sudo apt-get install -y postgresql-client
180+
181+
- name: Install tarpaulin
182+
run: cargo install cargo-tarpaulin || true
183+
184+
- name: Wait for PostgreSQL
185+
run: |
186+
until pg_isready -h localhost -p 5432 -U postgres; do
187+
echo "Waiting for PostgreSQL..."
188+
sleep 2
189+
done
190+
191+
- name: Setup database schema
192+
env:
193+
PGPASSWORD: dev
194+
run: |
195+
psql -h localhost -U postgres -d e2ee -f sql_models/seed.sql
196+
197+
- name: Generate coverage
198+
env:
199+
DATABASE_URL: postgres://postgres:dev@localhost:5432/e2ee
200+
run: cargo tarpaulin --verbose --all-features --workspace --timeout 300 --out xml
201+
202+
- name: Upload coverage to Codecov
203+
uses: codecov/codecov-action@v4
204+
with:
205+
files: ./cobertura.xml
206+
fail_ci_if_error: false

0 commit comments

Comments
 (0)