Skip to content

Commit 9bfde71

Browse files
committed
Adding CI/CD for repo
1 parent 909d94e commit 9bfde71

File tree

10 files changed

+567
-25
lines changed

10 files changed

+567
-25
lines changed

.github/README-CICD.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# CI/CD Quick Start
2+
3+
## Pull Production Images
4+
5+
```bash
6+
# Backend
7+
docker pull ghcr.io/hackstrix/ocpp-chaos-simulator:latest
8+
9+
# Frontend
10+
docker pull ghcr.io/hackstrix/ocpp-chaos-simulator-frontend:latest
11+
```
12+
13+
## Deploy with Docker Compose
14+
15+
```bash
16+
# Use production compose file
17+
docker-compose -f deployments/docker-compose.prod.yml up -d
18+
```
19+
20+
## Pipeline Status
21+
22+
-**Build & Test**: Runs on all pushes and PRs
23+
- 🚀 **Deploy**: Runs on main branch merges
24+
- 📦 **Registry**: Images pushed to GHCR automatically
25+
26+
For detailed documentation, see [docs/ci-cd-pipeline.md](../docs/ci-cd-pipeline.md)

.github/workflows/build-test.yml

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
name: Build and Test
2+
3+
on:
4+
push:
5+
branches: ["*"]
6+
pull_request:
7+
branches: ["*"]
8+
9+
jobs:
10+
test-backend:
11+
name: Test Go Backend
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
18+
- name: Set up Go
19+
uses: actions/setup-go@v5
20+
with:
21+
go-version: '1.21'
22+
cache: true
23+
24+
- name: Install dependencies
25+
run: go mod download
26+
27+
- name: Run tests
28+
run: go test -v -race -coverprofile=coverage.out ./...
29+
30+
- name: Generate coverage report
31+
run: go tool cover -html=coverage.out -o coverage.html
32+
33+
- name: Upload coverage reports
34+
uses: actions/upload-artifact@v4
35+
with:
36+
name: coverage-report
37+
path: |
38+
coverage.out
39+
coverage.html
40+
41+
- name: Build simulator binary
42+
run: |
43+
CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o bin/simulator ./cmd/simulator
44+
CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o bin/test-charger ./cmd/test-charger
45+
46+
- name: Upload backend artifacts
47+
uses: actions/upload-artifact@v4
48+
with:
49+
name: backend-binaries
50+
path: bin/
51+
52+
test-frontend:
53+
name: Test Frontend
54+
runs-on: ubuntu-latest
55+
56+
defaults:
57+
run:
58+
working-directory: ./frontend
59+
60+
steps:
61+
- name: Checkout code
62+
uses: actions/checkout@v4
63+
64+
- name: Set up Node.js
65+
uses: actions/setup-node@v4
66+
with:
67+
node-version: '24'
68+
cache: 'npm'
69+
cache-dependency-path: './frontend/package-lock.json'
70+
71+
- name: Install dependencies
72+
run: npm ci
73+
74+
- name: Run linter
75+
run: npm run lint
76+
77+
- name: Build frontend
78+
run: npm run build
79+
80+
- name: Upload frontend build artifacts
81+
uses: actions/upload-artifact@v4
82+
with:
83+
name: frontend-build
84+
path: frontend/.next/
85+
86+
integration-test:
87+
name: Integration Tests
88+
runs-on: ubuntu-latest
89+
needs: [test-backend, test-frontend]
90+
91+
services:
92+
docker:
93+
image: docker:24-dind
94+
options: --privileged
95+
96+
steps:
97+
- name: Checkout code
98+
uses: actions/checkout@v4
99+
100+
- name: Set up Go
101+
uses: actions/setup-go@v5
102+
with:
103+
go-version: '1.21'
104+
105+
- name: Build Docker image for testing
106+
run: |
107+
docker build -f deployments/Dockerfile -t ocpp-simulator:test .
108+
109+
- name: Run integration tests
110+
run: |
111+
# Start the service in background
112+
docker run -d --name simulator-test -p 8080:8080 ocpp-simulator:test
113+
sleep 10
114+
115+
# Run integration tests
116+
go test -v ./tests/...
117+
118+
# Cleanup
119+
docker stop simulator-test
120+
docker rm simulator-test

.github/workflows/deploy.yml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
name: Build and Deploy
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
workflow_dispatch: # Allow manual triggering
7+
8+
env:
9+
REGISTRY: ghcr.io
10+
IMAGE_NAME: ${{ github.repository }}
11+
12+
jobs:
13+
build-and-push:
14+
name: Build and Push to GHCR
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: read
18+
packages: write
19+
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v4
23+
24+
- name: Set up Docker Buildx
25+
uses: docker/setup-buildx-action@v3
26+
27+
- name: Log in to Container Registry
28+
uses: docker/login-action@v3
29+
with:
30+
registry: ${{ env.REGISTRY }}
31+
username: ${{ github.actor }}
32+
password: ${{ secrets.GITHUB_TOKEN }}
33+
34+
- name: Extract metadata
35+
id: meta
36+
uses: docker/metadata-action@v5
37+
with:
38+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
39+
tags: |
40+
type=ref,event=branch
41+
type=sha,prefix={{branch}}-
42+
type=raw,value=latest,enable={{is_default_branch}}
43+
44+
- name: Build and push Docker image
45+
uses: docker/build-push-action@v5
46+
with:
47+
context: .
48+
file: ./deployments/Dockerfile
49+
push: true
50+
tags: ${{ steps.meta.outputs.tags }}
51+
labels: ${{ steps.meta.outputs.labels }}
52+
cache-from: type=gha
53+
cache-to: type=gha,mode=max
54+
platforms: linux/amd64,linux/arm64
55+
56+
build-frontend-image:
57+
name: Build Frontend Container
58+
runs-on: ubuntu-latest
59+
permissions:
60+
contents: read
61+
packages: write
62+
63+
steps:
64+
- name: Checkout code
65+
uses: actions/checkout@v4
66+
67+
- name: Set up Docker Buildx
68+
uses: docker/setup-buildx-action@v3
69+
70+
- name: Log in to Container Registry
71+
uses: docker/login-action@v3
72+
with:
73+
registry: ${{ env.REGISTRY }}
74+
username: ${{ github.actor }}
75+
password: ${{ secrets.GITHUB_TOKEN }}
76+
77+
- name: Extract metadata for frontend
78+
id: meta
79+
uses: docker/metadata-action@v5
80+
with:
81+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-frontend
82+
tags: |
83+
type=ref,event=branch
84+
type=sha,prefix={{branch}}-
85+
type=raw,value=latest,enable={{is_default_branch}}
86+
87+
- name: Build and push Frontend Docker image
88+
uses: docker/build-push-action@v5
89+
with:
90+
context: ./frontend
91+
file: ./frontend/Dockerfile
92+
push: true
93+
tags: ${{ steps.meta.outputs.tags }}
94+
labels: ${{ steps.meta.outputs.labels }}
95+
cache-from: type=gha
96+
cache-to: type=gha,mode=max
97+
platforms: linux/amd64,linux/arm64
98+
99+
notify-deployment:
100+
name: Notify Deployment
101+
runs-on: ubuntu-latest
102+
needs: [build-and-push, build-frontend-image]
103+
if: always()
104+
105+
steps:
106+
- name: Deployment Summary
107+
run: |
108+
echo "🚀 Deployment Summary" >> $GITHUB_STEP_SUMMARY
109+
echo "" >> $GITHUB_STEP_SUMMARY
110+
echo "**Images built and pushed to GHCR:**" >> $GITHUB_STEP_SUMMARY
111+
echo "- Backend: \`ghcr.io/${{ github.repository }}:latest\`" >> $GITHUB_STEP_SUMMARY
112+
echo "- Frontend: \`ghcr.io/${{ github.repository }}-frontend:latest\`" >> $GITHUB_STEP_SUMMARY
113+
echo "" >> $GITHUB_STEP_SUMMARY
114+
echo "**Pull commands for your orchestration tool:**" >> $GITHUB_STEP_SUMMARY
115+
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
116+
echo "docker pull ghcr.io/${{ github.repository }}:latest" >> $GITHUB_STEP_SUMMARY
117+
echo "docker pull ghcr.io/${{ github.repository }}-frontend:latest" >> $GITHUB_STEP_SUMMARY
118+
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY

deployments/Dockerfile

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,49 @@
1-
FROM golang:1.21-alpine AS builder
1+
# Multi-stage Dockerfile for OCPP Chaos Simulator Backend
2+
FROM golang:1.21 AS builder
3+
4+
# Install build dependencies
5+
RUN apt-get update && apt-get install -y gcc libc6-dev libsqlite3-dev build-essential
26

37
WORKDIR /app
48

5-
# Copy go mod files
9+
# Copy go mod files first for better caching
610
COPY go.mod go.sum ./
7-
RUN go mod download
11+
RUN go mod download && go mod verify
812

913
# Copy source code
1014
COPY . .
1115

12-
# Build the application
13-
RUN CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o simulator ./cmd/simulator
16+
# Build both applications with optimizations
17+
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -a -installsuffix cgo -o simulator ./cmd/simulator
18+
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -a -installsuffix cgo -o test-charger ./cmd/test-charger
19+
20+
FROM debian:bookworm-slim
21+
22+
# Add labels for better container management
23+
LABEL org.opencontainers.image.source="https://github.com/HackStrix/ocpp-chaos-simulator"
24+
LABEL org.opencontainers.image.description="OCPP Chaos Simulator Backend"
25+
LABEL org.opencontainers.image.licenses="MIT"
1426

15-
FROM alpine:latest
27+
# Install runtime dependencies and create non-root user
28+
RUN apt-get update && apt-get install -y ca-certificates sqlite3 tzdata && \
29+
groupadd -g 1001 appgroup && \
30+
useradd -u 1001 -g appgroup -s /bin/sh appuser && \
31+
apt-get clean && rm -rf /var/lib/apt/lists/*
1632

17-
RUN apk --no-cache add ca-certificates sqlite
18-
WORKDIR /root/
33+
WORKDIR /app
34+
35+
# Copy binaries and configs
36+
COPY --from=builder --chown=appuser:appgroup /app/simulator /app/test-charger ./
37+
COPY --from=builder --chown=appuser:appgroup /app/configs ./configs
1938

20-
# Copy the binary from builder
21-
COPY --from=builder /app/simulator .
22-
COPY --from=builder /app/configs ./configs
39+
# Create data directory with proper permissions
40+
RUN mkdir -p ./data && chown -R appuser:appgroup ./data
2341

24-
# Create data directory
25-
RUN mkdir -p ./data
42+
USER appuser
2643

2744
EXPOSE 8080
2845

46+
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
47+
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1
48+
2949
CMD ["./simulator"]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: '3.8'
2+
3+
services:
4+
simulator:
5+
image: ghcr.io/hackstrix/ocpp-chaos-simulator:latest
6+
ports:
7+
- "8080:8080"
8+
volumes:
9+
- simulator_data:/app/data
10+
- ./configs:/app/configs:ro
11+
environment:
12+
- LOG_LEVEL=info
13+
- DATABASE_PATH=/app/data/simulator.db
14+
healthcheck:
15+
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8080/health"]
16+
interval: 30s
17+
timeout: 10s
18+
retries: 3
19+
start_period: 40s
20+
restart: unless-stopped
21+
22+
frontend:
23+
image: ghcr.io/hackstrix/ocpp-chaos-simulator-frontend:latest
24+
ports:
25+
- "3000:3000"
26+
environment:
27+
- NODE_ENV=production
28+
- NEXT_PUBLIC_API_URL=http://simulator:8080
29+
depends_on:
30+
- simulator
31+
restart: unless-stopped
32+
33+
volumes:
34+
simulator_data:

0 commit comments

Comments
 (0)