Skip to content

Commit 8ea94c7

Browse files
committed
feat: seeder works with compose up
1 parent bd39453 commit 8ea94c7

9 files changed

Lines changed: 210 additions & 19 deletions
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# I'm gonna make modifications to docker.yml here. I don't really understand all of it so this is [retty experimental]
2+
3+
name: Build chaos next.js version
4+
5+
on:
6+
pull_request:
7+
branches: [main, "renovate/*"]
8+
push:
9+
branches: ["*"]
10+
11+
jobs:
12+
build-backend:
13+
name: Build (Backend)
14+
runs-on: ubuntu-latest
15+
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
16+
steps:
17+
- uses: actions/checkout@v4
18+
with:
19+
submodules: true
20+
token: ${{ secrets.GH_TOKEN }}
21+
- name: Set up Docker Buildx
22+
id: buildx
23+
uses: docker/setup-buildx-action@master
24+
with:
25+
install: true
26+
- name: Cache Docker layers
27+
uses: actions/cache@v3
28+
with:
29+
path: /tmp/.buildx-cache
30+
key: ${{ runner.os }}-multi-buildx-${{ github.sha }}
31+
restore-keys: |
32+
${{ runner.os }}-multi-buildx
33+
- name: Docker Login
34+
uses: docker/login-action@v2.2.0
35+
with:
36+
registry: ghcr.io
37+
username: ${{ github.actor }}
38+
password: ${{ secrets.GH_TOKEN }}
39+
- name: Build production image
40+
uses: docker/build-push-action@v4
41+
with:
42+
context: backend
43+
builder: ${{ steps.buildx.outputs.name }}
44+
file: backend/Dockerfile
45+
push: ${{ github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }}
46+
tags: |
47+
ghcr.io/devsoc-unsw/chaos-backend:${{ github.sha }}
48+
ghcr.io/devsoc-unsw/chaos-backend:latest
49+
labels: ${{ steps.meta.outputs.labels }}
50+
cache-from: type=local,src=/tmp/.buildx-cache
51+
# Note the mode=max here
52+
# More: https://github.com/moby/buildkit#--export-cache-options
53+
# And: https://github.com/docker/buildx#--cache-tonametypetypekeyvalue
54+
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new
55+
- name: Move cache
56+
run: |
57+
rm -rf /tmp/.buildx-cache
58+
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
59+
build-client:
60+
name: Build (Client)
61+
runs-on: ubuntu-latest
62+
permissions:
63+
contents: read
64+
packages: write
65+
steps:
66+
- name: Checkout repository
67+
uses: actions/checkout@v4
68+
- name: Set up QEMU
69+
uses: docker/setup-qemu-action@v2
70+
with:
71+
platforms: arm64
72+
- name: Set up Docker Buildx
73+
uses: docker/setup-buildx-action@v2
74+
- name: Log into registry ${{ env.REGISTRY }}
75+
uses: docker/login-action@v2
76+
with:
77+
registry: ghcr.io
78+
username: ${{ github.actor }}
79+
password: ${{ secrets.GH_TOKEN }}
80+
- name: Build and push Docker image
81+
uses: docker/build-push-action@v4
82+
with:
83+
context: frontend
84+
push: ${{ github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }}
85+
platforms: linux/amd64
86+
file: frontend/Dockerfile
87+
build-args: |
88+
VITE_API_BASE_URL=${{ secrets.API_BASE_URL }}
89+
VITE_OAUTH_CALLBACK_URL=${{ secrets.OAUTH_CALLBACK_URL }}
90+
tags: |
91+
ghcr.io/devsoc-unsw/chaos-frontend:${{ github.sha }}
92+
ghcr.io/devsoc-unsw/chaos-frontend:latest
93+
labels: ${{ steps.meta.outputs.labels }}
94+
deploy:
95+
name: Deploy (CD)
96+
runs-on: ubuntu-latest
97+
needs: [build-client, build-backend]
98+
if: ${{ github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }}
99+
concurrency: staging
100+
environment:
101+
name: staging
102+
url: https://chaos.staging.csesoc.unsw.edu.au
103+
steps:
104+
- name: Checkout repository
105+
uses: actions/checkout@v4
106+
with:
107+
repository: devsoc-unsw/deployment
108+
token: ${{ secrets.GH_TOKEN }}
109+
ref: dev
110+
- name: Install yq - portable yaml processor
111+
uses: mikefarah/yq@v4.35.2
112+
- name: Update deployment
113+
env:
114+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
115+
run: |
116+
git config user.name "CSESoc CD"
117+
git config user.email "technical@csesoc.org.au"
118+
git checkout -b update/chaos/staging/${{ github.sha }}
119+
yq -i '.items[0].spec.template.spec.containers[0].image = "ghcr.io/devsoc-unsw/chaos-frontend:${{ github.sha }}"' projects/chaos/staging/deploy-frontend.yml
120+
yq -i '.items[0].spec.template.spec.containers[0].image = "ghcr.io/devsoc-unsw/chaos-backend:${{ github.sha }}"' projects/chaos/staging/deploy-backend.yml
121+
git add .
122+
git commit -m "feat(chaos/staging): update images"
123+
git push -u origin update/chaos/staging/${{ github.sha }}
124+
gh pr create --title "feat(chaos/staging): update images" --body "Updates the images for the chaos staging deployment to commit devsoc-unsw/chaos@${{ github.sha }}." > URL
125+
gh pr merge $(cat URL) --squash -d

.github/workflows/deploy.yml

Whitespace-only changes.

backend/Dockerfile

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,30 @@ WORKDIR /backend/server
1919
COPY --from=planner /backend/server/recipe.json ./recipe.json
2020

2121
RUN cargo chef cook --release --recipe-path recipe.json
22-
COPY migrations/ ./migrations/
22+
23+
COPY migrations/ ../migrations/
2324
COPY server/ ./
2425

25-
# build the application
26+
# Use sqlx folders for compile time checking, need to generate these in the workflow
27+
ENV SQLX_OFFLINE=true
28+
29+
# Build the application
2630
WORKDIR /backend/server
2731
RUN cargo build --release
2832

33+
# Build deploy-seeder
34+
FROM chef AS seeder-builder
35+
WORKDIR /backend
36+
37+
38+
COPY server/ ./server/
39+
COPY backend-deploy-utils/deploy-seeder/ ./backend-deploy-utils/deploy-seeder/
40+
41+
ENV SQLX_OFFLINE=true
42+
43+
WORKDIR /backend/backend-deploy-utils/deploy-seeder
44+
RUN cargo build --release
45+
2946
FROM debian:trixie-slim
3047

3148
RUN apt-get update && apt-get install -y \
@@ -35,14 +52,15 @@ RUN apt-get update && apt-get install -y \
3552
netcat-openbsd \
3653
&& rm -rf /var/lib/apt/lists/*
3754

38-
# copy sqlx-cli and migrations for running migrations on startup
55+
# Copy sqlx-cli and migrations for running migrations on startup
3956
COPY --from=chef /usr/local/bin/sqlx /usr/local/bin/sqlx
40-
COPY --from=builder /backend/server/migrations /backend/migrations
57+
COPY --from=builder /backend/migrations /backend/migrations
4158
COPY --from=builder /backend/server/target/release/server /backend/server
59+
COPY --from=seeder-builder /backend/backend-deploy-utils/deploy-seeder/target/release/deploy-seeder /backend/deploy-seeder
4260

43-
# setup backend start script
61+
# Setup backend start script
4462
COPY backend-deploy-utils/startup-script/start-backend.sh /backend/start-backend.sh
4563
RUN chmod +x /backend/start-backend.sh
4664

4765
ENTRYPOINT ["/backend/start-backend.sh"]
48-
EXPOSE 8080
66+
EXPOSE 8080

backend/backend-deploy-utils/deploy-seeder/.sqlx/query-163970f6f447b4341920dca8a27a55b880c5715fe271b308cb8f5ac21b9c3bae.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/backend-deploy-utils/deploy-seeder/.sqlx/query-7ab3b7179c666056d23829278c1927adf526fb1c4e36ced9994f265791e1ae93.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/backend-deploy-utils/deploy-seeder/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub mod seeder;
33

44
#[tokio::main]
55
async fn main() {
6-
dotenvy::dotenv().expect("Failed to load .env");
6+
dotenvy::dotenv().ok();
77

88
let seeder = init().await;
99

backend/backend-deploy-utils/startup-script/start-backend.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ if ! sqlx migrate run; then
1010
exit 1
1111
fi
1212

13-
echo "Migrations run successfully! Starting server..."
14-
echo "Running server..."
13+
echo "Migrations run successfully!"
14+
15+
# Run database seeding (this'll be idempotent so we're chill)
16+
echo "Running database seeding..."
17+
cd /backend
18+
if /backend/deploy-seeder; then
19+
echo "Seeding completed successfully!"
20+
else
21+
echo "Seeding failed! Aborting startup..."
22+
exit 1
23+
fi
24+
25+
echo "Starting server..."
1526
exec /backend/server

backend/migrations/20240406023149_create_users.sql

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,4 @@ CREATE TABLE users (
1414
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
1515
);
1616

17-
-- -- Seed initial superuser COMMENTING THIS OUT SO THAT WE USE .env INSTEAD
18-
-- INSERT INTO users (id, email, name, role)
19-
-- VALUES (10000, 'chaosdirectors@devsoc.app', 'Super Admin', 'SuperUser');
20-
2117
CREATE UNIQUE INDEX IDX_users_email_lower on users((lower(email)));

docker-compose.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: '3.8'
22

33
services:
4-
# PostgreSQL Database
4+
# db
55
db:
66
image: postgres:17
77
restart: unless-stopped
@@ -19,7 +19,7 @@ services:
1919
timeout: 5s
2020
retries: 5
2121

22-
# Rust Backend API
22+
# server
2323
backend:
2424
build:
2525
context: ./backend
@@ -28,7 +28,6 @@ services:
2828
depends_on:
2929
db:
3030
condition: service_healthy
31-
# Load env vars from backend/.env file
3231
env_file:
3332
- ./backend/.env
3433
environment:
@@ -42,7 +41,7 @@ services:
4241
retries: 3
4342
start_period: 40s
4443

45-
# Next.js Frontend
44+
# next.js-frontend
4645
frontend:
4746
build:
4847
context: ./frontend-nextjs
@@ -51,11 +50,9 @@ services:
5150
depends_on:
5251
backend:
5352
condition: service_healthy
54-
# Load env vars from frontend-nextjs/.env file
5553
env_file:
5654
- ./frontend-nextjs/.env
5755
environment:
58-
# Override API URL to use Docker service name (container-to-container communication)
5956
NEXT_API_BASE_URL: ${NEXT_API_BASE_URL:-http://backend:8080}
6057
NODE_ENV: production
6158
ports:

0 commit comments

Comments
 (0)