diff --git a/Dockerfile.stg.go b/Dockerfile.stg.go index af60e785..c984bac2 100644 --- a/Dockerfile.stg.go +++ b/Dockerfile.stg.go @@ -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"] diff --git a/deploy/deploy-stg.sh b/deploy/deploy-stg.sh index 6b235f87..272fd636 100755 --- a/deploy/deploy-stg.sh +++ b/deploy/deploy-stg.sh @@ -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