diff --git a/.github/workflows/development.yml b/.github/workflows/development.yml index 92488693..76754a49 100644 --- a/.github/workflows/development.yml +++ b/.github/workflows/development.yml @@ -178,4 +178,5 @@ jobs: gunzip -c /tmp/golang_dev.tar.gz | docker load rm -f /tmp/golang_dev.tar.gz cd ~/deployments/development + bash ./scripts/map_envs.sh app.env POSTGRES_USER=DB_USER POSTGRES_DB=DB_NAME POSTGRES_PASSWORD=PASSWORD docker compose -f docker-compose.yml up -d diff --git a/.github/workflows/development_pr_deploy.yml b/.github/workflows/development_pr_deploy.yml index 5d7a1e6a..71aaaebd 100644 --- a/.github/workflows/development_pr_deploy.yml +++ b/.github/workflows/development_pr_deploy.yml @@ -1,6 +1,7 @@ name: PR Deploy for Development on: pull_request: + branches: [dev] types: [opened, synchronize, reopened, closed] workflow_dispatch: diff --git a/.github/workflows/production.yml b/.github/workflows/production.yml index a9504a21..04d09f9a 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -181,4 +181,5 @@ jobs: gunzip -c /tmp/golang_prod.tar.gz | docker load rm -f /tmp/golang_prod.tar.gz cd ~/deployments/production + bash ./scripts/map_envs.sh app.env POSTGRES_USER=DB_USER POSTGRES_DB=DB_NAME POSTGRES_PASSWORD=PASSWORD docker compose -f docker-compose-production.yml up -d \ No newline at end of file diff --git a/.github/workflows/production_pr_deploy.yml b/.github/workflows/production_pr_deploy.yml index 917bb5d5..72015e17 100644 --- a/.github/workflows/production_pr_deploy.yml +++ b/.github/workflows/production_pr_deploy.yml @@ -1,6 +1,7 @@ name: PR Deploy for Production on: pull_request: + branches: [main] types: [opened, synchronize, reopened, closed] workflow_dispatch: diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index 6e85dbcc..809d87ca 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -181,4 +181,5 @@ jobs: gunzip -c /tmp/golang_staging.tar.gz | docker load rm -f /tmp/golang_staging.tar.gz cd ~/deployments/staging + bash ./scripts/map_envs.sh app.env POSTGRES_USER=DB_USER POSTGRES_DB=DB_NAME POSTGRES_PASSWORD=PASSWORD docker compose -f docker-compose-staging.yml up -d \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 0998b822..0cfe4aea 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,8 +14,7 @@ RUN go mod download && go mod verify COPY . . # Build the Go app -RUN if test -e app.env; then echo 'found app.env'; else mv app-sample.env app.env; fi; \ - go build -v -o /dist/app-name +RUN go build -v -o /dist/golang_app # Wait-for-it stage FROM alpine:3.17 as wait @@ -27,12 +26,12 @@ RUN chmod +x /wait-for-it.sh FROM alpine:3.17 WORKDIR /usr/src/app COPY --from=build /usr/src/app ./ -COPY --from=build /dist/app-name /usr/local/bin/app-name +COPY --from=build /dist/golang_app /usr/local/bin/golang_app COPY --from=wait /wait-for-it.sh /wait-for-it.sh # Install bash (required for wait-for-it script) RUN apk add --no-cache bash # Wait for DB and Redis, then start the application -# CMD /wait-for-it.sh $DB_HOST:$DB_PORT -t 10 -- /wait-for-it.sh $REDIS_HOST:$REDIS_PORT -t 10 -- app-name -CMD app-name \ No newline at end of file +# CMD /wait-for-it.sh $DB_HOST:$DB_PORT -t 10 -- /wait-for-it.sh $REDIS_HOST:$REDIS_PORT -t 10 -- golang_app +CMD golang_app \ No newline at end of file diff --git a/docker-compose-production.yml b/docker-compose-production.yml index f367771f..a6b568e2 100644 --- a/docker-compose-production.yml +++ b/docker-compose-production.yml @@ -3,42 +3,36 @@ name: golang_prod services: db: image: postgres:16 - environment: - POSTGRES_USER: production_user - POSTGRES_PASSWORD: password - POSTGRES_DB: production_db - POSTGRES_PORT: 5432 + env_file: + - app.env volumes: - - db_data:/var/lib/postgresql/data + - ../pgsql_volumes/golang_prod/:/var/lib/postgresql/data healthcheck: - test: ["CMD-SHELL", "pg_isready -U postgres"] - interval: 10s - timeout: 10s - retries: 2 + test: ["CMD-SHELL", "pg_isready -U production_user"] + interval: 10s + timeout: 5s + retries: 2 redis: image: redis:latest backend: - image: ${COMPOSE_PROJECT_NAME} + image: golang_prod build: context: . depends_on: - - db - - redis - ports: - - "8002:7002" + db: + condition: service_healthy + redis: + condition: service_started env_file: - app.env nginx: image: nginx:latest - ports: - - "8117:80" depends_on: - backend + ports: + - "7002:80" volumes: - ./nginx/nginx.prod.conf:/etc/nginx/nginx.conf - -volumes: - db_data: \ No newline at end of file diff --git a/docker-compose-staging.yml b/docker-compose-staging.yml index fdafdf5f..5b3529c9 100644 --- a/docker-compose-staging.yml +++ b/docker-compose-staging.yml @@ -3,42 +3,36 @@ name: golang_staging services: db: image: postgres:16 - environment: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: password - POSTGRES_DB: db_name - POSTGRES_PORT: 5432 + env_file: + - app.env volumes: - - db_data:/var/lib/postgresql/data + - ../pgsql_volumes/golang_staging/:/var/lib/postgresql/data healthcheck: - test: ["CMD-SHELL", "pg_isready -U postgres"] - interval: 10s - timeout: 10s - retries: 2 + test: ["CMD-SHELL", "pg_isready -U staging_user"] + interval: 10s + timeout: 5s + retries: 2 redis: image: redis:latest backend: - image: ${COMPOSE_PROJECT_NAME} + image: golang_staging build: context: . depends_on: - - db - - redis - ports: - - "8001:7010" + db: + condition: service_healthy + redis: + condition: service_started env_file: - app.env nginx: image: nginx:latest - ports: - - "8116:80" depends_on: - backend + ports: + - "7001:80" volumes: - ./nginx/nginx.staging.conf:/etc/nginx/nginx.conf - -volumes: - db_data: \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 809d2a28..b9db2242 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,34 +1,38 @@ -name: golang +name: golang_dev services: db: image: postgres:16 env_file: - - ./app.env + - app.env volumes: - - db_data:/var/lib/postgresql/data + - ../pgsql_volumes/golang_dev/:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U development_user"] + interval: 10s + timeout: 5s + retries: 2 redis: image: redis:latest backend: - image: golang_backend + image: golang_dev build: context: . depends_on: - - db - - redis + db: + condition: service_healthy + redis: + condition: service_started env_file: - - ./app.env - + - app.env + nginx: image: nginx:latest - ports: - - "7000:80" depends_on: - backend + ports: + - "7000:80" volumes: - ./nginx.conf:/etc/nginx/nginx.conf - -volumes: - db_data: diff --git a/scripts/map_envs.sh b/scripts/map_envs.sh new file mode 100644 index 00000000..6c8f3967 --- /dev/null +++ b/scripts/map_envs.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +if [ "$#" -lt 2 ]; then + echo "Description: creates new environment variables from existing ones" + echo "Usage: $0 .env_file NEW_ENV1=EXISTING_ENV1 [NEW_ENV2=EXISTING_ENV2 ...]" + exit 1 +fi + +env_file="$1" +shift + +if [ ! -f "$env_file" ]; then + echo "Error: .env file '$env_file' not found." + exit 1 +fi + +export $(grep -v '^#' "$env_file" | xargs) + +for arg in "$@"; do + IFS='=' read -r new_env existing_env <<< "$arg" + + if [ -z "${!existing_env}" ]; then + echo "Warning: Existing environment variable '$existing_env' is not set." + continue + fi + + # Get the value of the existing environment variable + value="${!existing_env}" + + # Export the new environment variable with the value of the existing one + export "$new_env=$value" +done + +# Write the new environment variables to the .env file +{ + echo + for arg in "$@"; do + IFS='=' read -r new_env _ <<< "$arg" + echo "$new_env=${!new_env}" + done +} >> "$env_file"