Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: setup successful docker builds #332

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions .github/workflows/development_pr_deploy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: PR Deploy for Development
on:
pull_request:
branches: [dev]
types: [opened, synchronize, reopened, closed]
workflow_dispatch:

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions .github/workflows/production_pr_deploy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: PR Deploy for Production
on:
pull_request:
branches: [main]
types: [opened, synchronize, reopened, closed]
workflow_dispatch:

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 4 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
# 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
34 changes: 14 additions & 20 deletions docker-compose-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
34 changes: 14 additions & 20 deletions docker-compose-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
30 changes: 17 additions & 13 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
41 changes: 41 additions & 0 deletions scripts/map_envs.sh
Original file line number Diff line number Diff line change
@@ -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"
Loading