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

prdeploy-fix #591

Closed
wants to merge 46 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
5eae712
Update dev.yml
Rob-in-son Aug 9, 2024
0757d22
Merge pull request #558 from hngprojects/Lanky-23-patch-2-1
Rob-in-son Aug 9, 2024
df725ab
Merge pull request #559 from hngprojects/Rob-in-son-patch-0
Lanky-23 Aug 9, 2024
56d3924
Update dev.yml
Rob-in-son Aug 9, 2024
07a130d
Merge pull request #560 from hngprojects/Rob-in-son-patch-2
Lanky-23 Aug 9, 2024
56c1911
Update dev.yml
Rob-in-son Aug 9, 2024
2a176f0
Merge pull request #561 from hngprojects/Rob-in-son-patch-11
codak77 Aug 9, 2024
cbc11d4
Update dev.yml
Rob-in-son Aug 9, 2024
85a87d8
Merge pull request #562 from hngprojects/Rob-in-son-patch-3
codak77 Aug 9, 2024
c0bd7d1
fetch .env from server for compose up
Rob-in-son Aug 9, 2024
7217de1
Merge pull request #564 from hngprojects/Rob-in-son-dev.yml1
Lanky-23 Aug 9, 2024
ddce402
Update staging.yml
Rob-in-son Aug 9, 2024
2565a32
Update prod.yml
Rob-in-son Aug 9, 2024
b908517
Update dev.yml
Rob-in-son Aug 9, 2024
3de0541
Update prod.yml
Rob-in-son Aug 9, 2024
0c1f3b8
Update staging.yml
Rob-in-son Aug 9, 2024
e8f5c19
Update dev.yml
Rob-in-son Aug 9, 2024
187b0f4
Merge pull request #565 from hngprojects/Rob-in-son-dev.yml1
Lanky-23 Aug 9, 2024
9feb21b
Update .env path in docker-compose.yml
Rob-in-son Aug 9, 2024
5966e22
Update .env path in docker-compose.staging.yml
Rob-in-son Aug 9, 2024
f96e264
Update .env path in docker-compose.production.yml
Rob-in-son Aug 9, 2024
82179da
Update dev.yml
Rob-in-son Aug 9, 2024
3c3abbf
Update prod.yml
Rob-in-son Aug 9, 2024
df14e73
Update staging.yml
Rob-in-son Aug 9, 2024
41f37ae
Update CI.yml
Rob-in-son Aug 9, 2024
ffce86a
Update dev.yml
Rob-in-son Aug 9, 2024
de40e9a
Update prod.yml
Rob-in-son Aug 9, 2024
076de18
Update staging.yml
Rob-in-son Aug 9, 2024
b16153a
chore: user table
incredible-phoenix246 Aug 10, 2024
9bc4d4f
chore: user table
incredible-phoenix246 Aug 10, 2024
ab4a4ff
Merge pull request #572 from hngprojects/feat-user-table
incredible-phoenix246 Aug 10, 2024
b09641c
Merge pull request #568 from hngprojects/fix-workflows
Lanky-23 Aug 10, 2024
d645927
Update dev workflow
Rob-in-son Aug 10, 2024
d2f64a8
Merge pull request #583 from hngprojects/fix-workflows
Lanky-23 Aug 10, 2024
9cd70b3
Copy env file to .env
Rob-in-son Aug 10, 2024
b2d60bf
Merge pull request #585 from hngprojects/fix-workflows
Lanky-23 Aug 10, 2024
659af2d
change scp .env step
Rob-in-son Aug 10, 2024
82c5ce0
Merge pull request #586 from hngprojects/fix-workflows
Rob-in-son Aug 10, 2024
948f27a
change docker save
Rob-in-son Aug 10, 2024
570c02b
Merge pull request #587 from hngprojects/fix-workflows
Lanky-23 Aug 10, 2024
8c43195
Updated deployment dir
Rob-in-son Aug 10, 2024
6f72293
Merge pull request #588 from hngprojects/fix-workflows
Lanky-23 Aug 10, 2024
53cca85
updated dockercompose files and workflows
Rob-in-son Aug 10, 2024
975e89a
Merge pull request #589 from hngprojects/fix-workflows
Lanky-23 Aug 10, 2024
31a8bd7
cd: update dev.yml
Rob-in-son Aug 13, 2024
6805142
Merge pull request #597 from hngprojects/Rob-in-son-cd-patch
Lanky-23 Aug 13, 2024
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
9 changes: 6 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: CI

on:
push:
branches: [dev, staging, main]
pull_request:
branches:
- dev
types: [opened, synchronize, reopened]
branches: [dev, staging, main]

jobs:
test:
Expand All @@ -25,5 +27,6 @@ jobs:
run: yarn test
env:
CI: true
- name: buld the dist

- name: Build the dist
run: yarn build
75 changes: 44 additions & 31 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
@@ -1,48 +1,61 @@
name: Build, Test, and Deploy for Dev Branch
name: Deploy to Dev

on:
push:
branches:
- dev
workflow_run:
workflows: [CI]
types:
- completed
branches: [dev]

jobs:
build-and-deploy:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

# - name: Install sshpass
# run: sudo apt-get install sshpass

- name: Build Docker images using docker-compose
- name: Fetch .env file from server
run: |
docker compose -f docker-compose.yml build

- name: Save Docker images to tarball
run: |
docker save $(docker compose -f docker-compose.yml config | grep 'image:' | awk '{print $2}') | gzip > docker-images.tar.gz

- name: Install sshpass
run: sudo apt-get install sshpass

- name: Upload to server
run: |
sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no docker-images.tar.gz docker-compose.yml ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/tmp/
sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/var/www/aihomework/dev/.env .env
env:
SSH_HOST: ${{ secrets.HOST }}
SSH_USERNAME: ${{ secrets.USERNAME }}
SSH_PASSWORD: ${{ secrets.PASSWORD }}

- name: Deploy and start on remote server
- name: Build Docker images
run: |
docker compose --env-file .env -f docker-compose.yml build

- name: List Docker images
run: docker images

- name: Save Docker images to tarball
run: |
sshpass -p ${{ secrets.PASSWORD }} ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} "
cd /var/www/aihomework/dev
docker save hng_boilerplate_expressjs-backend:latest | gzip > dev-images.tar.gz

- name: Copy Docker images to server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
source: "dev-images.tar.gz"
target: "~/images-tar"

- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
cd ~/dev-deployment/hng_boilerplate_expressjs
git stash
git pull origin dev
docker load -i /tmp/docker-images.tar.gz
docker compose down
docker compose up -d
rm /tmp/docker-images.tar.gz
"
env:
SSH_HOST: ${{ secrets.HOST }}
SSH_USERNAME: ${{ secrets.USERNAME }}
SSH_PASSWORD: ${{ secrets.PASSWORD }}
git checkout dev
git pull
docker load -i ~/images-tar/dev-images.tar.gz
docker compose -f docker-compose.yml down
docker compose -f docker-compose.yml up -d
87 changes: 63 additions & 24 deletions .github/workflows/prod.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,74 @@
name: Build, Test, and Deploy for Prod Branch
name: Deploy to Prod

on:
push:
branches: [main]
workflow_run:
workflows: [CI]
types:
- completed
branches: [prod]

jobs:
build:
runs-on: self-hosted
defaults:
run:
working-directory: /var/www/aihomework/prod

on-success:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Pull from GitHub
- name: Checkout repository
uses: actions/checkout@v4

- name: Install sshpass
run: sudo apt-get install sshpass

- name: Fetch .env file from server
run: |
sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:~/prod-deployment/hng_boilerplate_expressjs/.env .env
env:
SSH_HOST: ${{ secrets.HOST }}
SSH_USERNAME: ${{ secrets.USERNAME }}
SSH_PASSWORD: ${{ secrets.PASSWORD }}

- name: Build Docker images
run: |
git stash
git pull origin main
docker compose --env-file .env -f docker-compose.production.yml build

- name: Install dependencies
run: yarn install
- name: List Docker images
run: docker images

- name: Build the dist
run: yarn build
- name: Save Docker images to tarball
run: |
docker save hng_boilerplate_expressjs-backend_prod:latest | gzip > prod-images.tar.gz

- name: migrate
run: yarn migrate
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: prod-images
path: prod-images.tar.gz

- name: Setup service file
run: sudo cp server-script/aihomeworkprod.service /etc/systemd/system
- name: Copy Docker images to server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
source: "prod-images.tar.gz"
target: "~/images-tar"

- name: Start the app
run: |
sudo systemctl daemon-reload
sudo systemctl restart aihomeworkprod
- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
cd ~/prod-deployment/hng_boilerplate_expressjs
git stash
git checkout prod
git pull
docker load -i ~/images-tar/prod-images.tar.gz
docker compose -f docker-compose.production.yml down
docker compose -f docker-compose.production.yml up -d

on-failure:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- run: echo "CI Workflow failed. Prod deployment was not triggered."
90 changes: 62 additions & 28 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,74 @@
name: Build, Test, and Deploy for Dev Branch
name: Deploy to staging

on:
push:
branches:
- dev
workflow_run:
workflows: [CI]
types:
- completed
branches: [staging]

jobs:
build:
runs-on: self-hosted
defaults:
run:
working-directory: /var/www/aihomework/staging

on-success:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Pull from github
id: pull
- name: Checkout repository
uses: actions/checkout@v4

- name: Install sshpass
run: sudo apt-get install sshpass

- name: Fetch .env file from server
run: |
git stash
git pull origin staging
sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:~/staging-deployment/hng_boilerplate_expressjs/.env .env
env:
SSH_HOST: ${{ secrets.HOST }}
SSH_USERNAME: ${{ secrets.USERNAME }}
SSH_PASSWORD: ${{ secrets.PASSWORD }}

- name: install dependencies
run: yarn install
- name: Build Docker images
run: |
docker compose --env-file .env -f docker-compose.staging.yml build

- name: Run Test
run: yarn test
- name: List Docker images
run: docker images

- name: buld the dist
run: yarn build
- name: Save Docker images to tarball
run: |
docker save hng_boilerplate_expressjs-backend_staging:latest | gzip > staging-images.tar.gz

- name: migrate
run: yarn reset-db
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: staging-images
path: staging-images.tar.gz

- name: setup service file
run: sudo cp server-script/aihomeworkstaging.service /etc/systemd/system
- name: Copy Docker images to server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
source: "staging-images.tar.gz"
target: "~/images-tar"

- name: start the app
run: |
sudo systemctl daemon-reload
sudo systemctl restart aihomeworkstaging
- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
cd ~/staging-deployment/hng_boilerplate_expressjs
git stash
git checkout staging
git pull
docker load -i ~/images-tar/staging-images.tar.gz
docker compose -f docker-compose.staging.yml down
docker compose -f docker-compose.staging.yml up -d

on-failure:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- run: echo "CI Workflow failed. Staging deployment was not triggered."
6 changes: 3 additions & 3 deletions docker-compose.production.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
services:
backend:
backend_prod:
container_name: backend_prod
build:
context: .
ports:
- 4444:8000
env_file:
- /var/www/aihomework/prod/.env
- .env
environment:
NODE_ENV: production
DB_HOST: backend_db_prod
Expand All @@ -24,7 +24,7 @@ services:
container_name: backend_db_prod
restart: unless-stopped
env_file:
- /var/www/aihomework/prod/.env
- .env
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.staging.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
services:
backend:
backend_staging:
container_name: backend_staging
build:
context: .
ports:
- 3333:8000
env_file:
- /var/www/aihomework/staging/.env
- .env
environment:
NODE_ENV: staging
DB_HOST: backend_db_staging
Expand All @@ -24,7 +24,7 @@ services:
container_name: backend_db_staging
restart: unless-stopped
env_file:
- /var/www/aihomework/staging/.env
- .env
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
ports:
- 2222:8000
env_file:
- /var/www/aihomework/dev/.env
- .env
environment:
NODE_ENV: development
DB_HOST: backend_db
Expand All @@ -24,7 +24,7 @@ services:
container_name: backend_db
restart: unless-stopped
env_file:
- /var/www/aihomework/dev/.env
- .env
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
Expand Down
Loading
Loading