Skip to content

Commit

Permalink
Merge pull request hngprojects#565 from hngprojects/Rob-in-son-dev.yml1
Browse files Browse the repository at this point in the history
Update cd files
  • Loading branch information
Lanky-23 authored Aug 9, 2024
2 parents 7217de1 + e8f5c19 commit 187b0f4
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 52 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ jobs:
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/
Expand All @@ -51,7 +48,7 @@ jobs:
docker load -i /tmp/docker-images.tar.gz
docker compose -f /tmp/docker-compose.yml down
docker compose -f /tmp/docker-compose.yml up -d
rm /tmp/docker-images.tar.gz /tmp/docker-compose.yml
rm /tmp/docker-images.tar.gz
"
env:
SSH_HOST: ${{ secrets.HOST }}
Expand Down
67 changes: 44 additions & 23 deletions .github/workflows/prod.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,56 @@
name: Build, Test, and Deploy for Prod Branch
name: Deploy to staging Branch

on:
push:
branches: [main]
branches:
- dev

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

build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Pull from GitHub
run: |
git stash
git pull origin main
- name: Checkout repository
uses: actions/checkout@v4

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

- name: Build the dist
run: yarn build
- name: Fetch .env file from server
run: |
sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/var/www/aihomework/staging/.env .env
env:
SSH_HOST: ${{ secrets.HOST }}
SSH_USERNAME: ${{ secrets.USERNAME }}
SSH_PASSWORD: ${{ secrets.PASSWORD }}

- name: migrate
run: yarn migrate
- name: Build Docker images using docker-compose
run: |
docker compose --env-file .env -f docker-compose.production.yml build
- name: Setup service file
run: sudo cp server-script/aihomeworkprod.service /etc/systemd/system
- name: Save Docker images to tarball
run: |
docker save $(docker compose -f docker-compose.production.yml config | grep 'image:' | awk '{print $2}') | gzip > docker-images.tar.gz
- name: Upload to server
run: |
sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no docker-images.tar.gz ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/tmp/
env:
SSH_HOST: ${{ secrets.HOST }}
SSH_USERNAME: ${{ secrets.USERNAME }}
SSH_PASSWORD: ${{ secrets.PASSWORD }}

- name: Start the app
- name: Deploy and start on remote server
run: |
sudo systemctl daemon-reload
sudo systemctl restart aihomeworkprod
sshpass -p ${{ secrets.PASSWORD }} ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} "
cd /var/www/aihomework/dev
git stash
git pull origin dev
docker load -i /tmp/docker-images.tar.gz
docker compose -f docker-compose.production.yml down
docker compose -f docker-compose.production.yml up -d
rm /tmp/docker-images.tar.gz
"
env:
SSH_HOST: ${{ secrets.HOST }}
SSH_USERNAME: ${{ secrets.USERNAME }}
SSH_PASSWORD: ${{ secrets.PASSWORD }}
66 changes: 41 additions & 25 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,56 @@
name: Build, Test, and Deploy for Dev Branch
name: Deploy to staging Branch

on:
push:
branches:
- dev

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

build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Pull from github
id: pull
run: |
git stash
git pull origin staging
- name: Checkout repository
uses: actions/checkout@v4

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

- name: Run Test
run: yarn test
- name: Fetch .env file from server
run: |
sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/var/www/aihomework/staging/.env .env
env:
SSH_HOST: ${{ secrets.HOST }}
SSH_USERNAME: ${{ secrets.USERNAME }}
SSH_PASSWORD: ${{ secrets.PASSWORD }}

- name: buld the dist
run: yarn build
- name: Build Docker images using docker-compose
run: |
docker compose --env-file .env -f docker-compose.staging.yml build
- name: migrate
run: yarn reset-db
- name: Save Docker images to tarball
run: |
docker save $(docker compose -f docker-compose.staging.yml config | grep 'image:' | awk '{print $2}') | gzip > docker-images.tar.gz
- name: setup service file
run: sudo cp server-script/aihomeworkstaging.service /etc/systemd/system
- name: Upload to server
run: |
sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no docker-images.tar.gz ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/tmp/
env:
SSH_HOST: ${{ secrets.HOST }}
SSH_USERNAME: ${{ secrets.USERNAME }}
SSH_PASSWORD: ${{ secrets.PASSWORD }}

- name: start the app
- name: Deploy and start on remote server
run: |
sudo systemctl daemon-reload
sudo systemctl restart aihomeworkstaging
sshpass -p ${{ secrets.PASSWORD }} ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} "
cd /var/www/aihomework/dev
git stash
git pull origin dev
docker load -i /tmp/docker-images.tar.gz
docker compose -f docker-compose.staging.yml down
docker compose -f docker-compose.staging.yml up -d
rm /tmp/docker-images.tar.gz
"
env:
SSH_HOST: ${{ secrets.HOST }}
SSH_USERNAME: ${{ secrets.USERNAME }}
SSH_PASSWORD: ${{ secrets.PASSWORD }}

0 comments on commit 187b0f4

Please sign in to comment.