Adding sync script to the deploy, changing go dockerfile to multi stage

This commit is contained in:
Finley Ghosh 2025-07-23 21:22:50 +10:00
parent 87caa649ed
commit 049256025a
2 changed files with 33 additions and 13 deletions

View file

@ -1,15 +1,21 @@
# Staging/Production Dockerfile for Go server (no Air)
FROM golang:1.24.0 AS builder
FROM golang:1.24-alpine AS builder
RUN apk add --no-cache git
WORKDIR /app
COPY go-app/go.mod go-app/go.sum ./
RUN go mod download
COPY go-app/ .
RUN go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
RUN sqlc generate
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o server cmd/server/main.go
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/server .
COPY go-app/templates ./templates
COPY go-app/static ./static
COPY go-app/.env.example .env
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o server ./cmd/server/main.go
FROM debian:bullseye-slim
WORKDIR /app
COPY --from=builder /app/server ./server
COPY go-app/.env.example .env
EXPOSE 8082
CMD ["./server"]

View file

@ -1,13 +1,21 @@
#!/bin/bash
# Deploy staging environment for cmc-sales
# Usage: ./deploy-stg.sh [--no-cache]
# Usage: ./deploy-stg.sh [--no-cache] [--no-sync]
USE_CACHE=true
if [[ "$1" == "--no-cache" ]]; then
USE_CACHE=false
echo "No cache flag detected: will rebuild images without cache."
else
RUN_SYNC=true
for arg in "$@"; do
if [[ "$arg" == "--no-cache" ]]; then
USE_CACHE=false
echo "No cache flag detected: will rebuild images without cache."
fi
if [[ "$arg" == "--no-sync" ]]; then
RUN_SYNC=false
echo "No-sync flag detected: will NOT run sync_prod_to_staging.sh after deployment."
fi
done
if [[ "$USE_CACHE" == "true" ]]; then
echo "Using cached layers for docker build."
fi
@ -21,7 +29,7 @@ STAGING_DIR="cmc-sales-staging"
echo "Connecting to server $SERVER via SSH..."
# Pass variables into SSH session
ssh $SERVER \
"SERVER=$SERVER REPO='$REPO' BRANCH='$BRANCH' STAGING_DIR='$STAGING_DIR' USE_CACHE='$USE_CACHE' bash -s" << 'ENDSSH'
"SERVER=$SERVER REPO='$REPO' BRANCH='$BRANCH' STAGING_DIR='$STAGING_DIR' USE_CACHE='$USE_CACHE' RUN_SYNC='$RUN_SYNC' bash -s" << 'ENDSSH'
set -e
echo "Connected to $SERVER."
# Clone or update staging branch
@ -80,5 +88,11 @@ EOF
# Add cron job if not present
(crontab -l 2>/dev/null | grep -q '/home/cmc/sync_prod_to_staging.sh') || \
(crontab -l 2>/dev/null; echo '0 3 * * * /home/cmc/sync_prod_to_staging.sh') | crontab -
if [[ "$RUN_SYNC" == "true" ]]; then
echo "Running sync_prod_to_staging.sh now..."
/home/cmc/sync_prod_to_staging.sh
fi
echo "Deployment complete."
ENDSSH