Skip to content

Commit

Permalink
Chore: pull from dev branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyberguru1 committed Aug 9, 2024
2 parents 5b30e43 + a2b1d53 commit 16ca181
Show file tree
Hide file tree
Showing 15 changed files with 433 additions and 86 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/dev-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Dev Deployment

on:
workflow_dispatch:
push:
branches:
- dev

jobs:
build_docker_image:
runs-on: ubuntu-latest
if: github.event_name == 'push'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Build Docker image
run: docker build -t golang_dev .
- name: Save and compress Docker image
run: docker save golang_dev | gzip > golang_dev.tar.gz
- name: Upload Docker image
uses: actions/upload-artifact@v4
with:
name: golang_dev
path: golang_dev.tar.gz

upload_docker_image:
runs-on: ubuntu-latest
needs: build_docker_image
if: github.event.repository.fork == false
environment:
name: "development"
url: ${{ vars.URL }}
steps:
- name: Download Docker image
uses: actions/download-artifact@v4
with:
name: golang_dev
path: .

- name: Copy image to server
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
password: ${{ secrets.SSH_PASSWORD }}
source: golang_dev.tar.gz
target: "/tmp"

run_docker_container:
runs-on: ubuntu-latest
needs: upload_docker_image
if: github.event.repository.fork == false
environment: development
env:
SSH_USERNAME: ${{ secrets.SSH_USERNAME }}
SSH_HOST: ${{ secrets.SSH_HOST }}
SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }}

steps:
- name: Deploy on server
uses: appleboy/[email protected]
with:
host: ${{ env.HOST }}
username: ${{ env.USERNAME }}
password: ${{ env.PASSWORD }}
script: |
gunzip -c /tmp/golang_dev.tar.gz | docker load
rm -f /tmp/golang_dev.tar.gz
cd ~/deployments/development
git reset --hard
git pull origin dev
docker compose -f docker-compose.yml up -d
27 changes: 0 additions & 27 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
name: Build, Test, and Deploy for Development

on:
push:
branches:
- dev
pull_request:
branches:
- dev
Expand Down Expand Up @@ -158,27 +155,3 @@ jobs:
MAIL_PASSWORD=${{ secrets.MAIL_PASSWORD }} \
MAIL_PORT=${{ secrets.MAIL_PORT }} \
MIGRATE=true
run_docker_container:
runs-on: ubuntu-latest
needs: upload_docker_image
if: github.event.repository.fork == false && github.event_name == 'push'
environment: development
env:
SSH_USERNAME: ${{ secrets.SSH_USERNAME }}
SSH_HOST: ${{ secrets.SSH_HOST }}
SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }}

steps:
- name: Deploy on server
uses: appleboy/[email protected]
with:
host: ${{ env.HOST }}
username: ${{ env.USERNAME }}
password: ${{ env.PASSWORD }}
script: |
gunzip -c /tmp/golang_dev.tar.gz | docker load
rm -f /tmp/golang_dev.tar.gz
cd ~/deployments/development
docker compose -f docker-compose-development.yml up -d
40 changes: 40 additions & 0 deletions .github/workflows/development_pr_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: PR Deploy for Development
on:
pull_request:
branches: [dev]
types: [opened, synchronize, reopened, closed]
workflow_dispatch:

jobs:
deploy-pr:
environment:
name: development
# url: ${{ steps.deploy.outputs.preview-url }}
runs-on: ubuntu-latest
env:
SSH_USERNAME: ${{ secrets.SSH_USERNAME }}
SSH_HOST: ${{ secrets.SSH_HOST }}
SSH_PORT: ${{ secrets.SSH_PORT }}
SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }}

steps:
- name: Checkout to branch
uses: actions/checkout@v4
- id: deploy
name: Pull Request Deploy
uses: hngprojects/pr-deploy@dev
with:
server_host: ${{ env.SSH_HOST }}
server_username: ${{ env.SSH_USERNAME }}
server_password: ${{ env.SSH_PASSWORD }}
server_port: ${{ env.SSH_PORT }}
comment: true
context: '.'
dockerfile: 'Dockerfile'
exposed_port: '8019'
# host_volume_path: '/var/'
# container_volume_path: '/var/'
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Print Preview Url
run: |
echo "Preview Url: ${{ steps.deploy.outputs.preview-url }}"
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
40 changes: 40 additions & 0 deletions .github/workflows/production_pr_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: PR Deploy for Production
on:
pull_request:
branches: [main]
types: [opened, synchronize, reopened, closed]
workflow_dispatch:

jobs:
deploy-pr:
environment:
name: production
# url: ${{ steps.deploy.outputs.preview-url }}
runs-on: ubuntu-latest
env:
SSH_USERNAME: ${{ secrets.SSH_USERNAME }}
SSH_HOST: ${{ secrets.SSH_HOST }}
SSH_PORT: ${{ secrets.SSH_PORT }}
SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }}

steps:
- name: Checkout to branch
uses: actions/checkout@v4
- id: deploy
name: Pull Request Deploy
uses: hngprojects/pr-deploy@main
with:
server_host: ${{ env.SSH_HOST }}
server_username: ${{ env.SSH_USERNAME }}
server_password: ${{ env.SSH_PASSWORD }}
server_port: ${{ env.SSH_PORT }}
comment: true
context: '.'
dockerfile: 'Dockerfile'
exposed_port: '8019'
# host_volume_path: '/var/'
# container_volume_path: '/var/'
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Print Preview Url
run: |
echo "Preview Url: ${{ steps.deploy.outputs.preview-url }}"
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
46 changes: 20 additions & 26 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:
# - "8002:80"
# depends_on:
# - backend
# volumes:
# - ./nginx.conf:/etc/nginx/nginx.conf

volumes:
db_data:
nginx:
image: nginx:latest
depends_on:
- backend
ports:
- "7002:80"
volumes:
- ./nginx/nginx.prod.conf:/etc/nginx/nginx.conf
46 changes: 20 additions & 26 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:
# - "8001:80"
# depends_on:
# - backend
# volumes:
# - ./nginx.conf:/etc/nginx/nginx.conf

volumes:
db_data:
nginx:
image: nginx:latest
depends_on:
- backend
ports:
- "7001:80"
volumes:
- ./nginx/nginx.staging.conf:/etc/nginx/nginx.conf
Loading

0 comments on commit 16ca181

Please sign in to comment.