Skip to content

Commit

Permalink
cd: Updated prod.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Lanky-23 authored Aug 15, 2024
1 parent a85235c commit 91257ec
Showing 1 changed file with 63 additions and 59 deletions.
122 changes: 63 additions & 59 deletions .github/workflows/prod.yml
Original file line number Diff line number Diff line change
@@ -1,74 +1,78 @@
name: Deploy to Prod
name: Deploy to production

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

jobs:
on-success:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
deploy:
runs-on: bpProd-runner
defaults:
run:
working-directory: /var/www/aihomework/boilerplate

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install sshpass
run: sudo apt-get install sshpass
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0

- 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: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '20'

- name: Build Docker images
- name: Remove old actions remote URL
continue-on-error: true
run: |
docker compose --env-file .env -f docker-compose.production.yml build
git remote rm action
- name: List Docker images
run: docker images
# - name: Stash or remove local changes
# run: |
# if git diff --quiet; then
# echo "No local changes to stash."
# else
# echo "Stashing local changes..."
# git stash --include-untracked || echo "Failed to stash changes. Attempting to reset..."
# git reset --hard || exit 1
# fi

- name: Save Docker images to tarball
- name: Pull from GitHub
id: pull
run: |
docker save hng_boilerplate_expressjs-backend_prod:latest | gzip > prod-images.tar.gz
remote_repo="https://${GITHUB_ACTOR}:${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git"
# git remote add action $remote_repo
git clone $remote_repo prod
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: prod-images
path: prod-images.tar.gz
- name: Install dependencies
run: yarn install --frozen-lockfile

- 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: Run tests
run: yarn test

- 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 main
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
- name: Build the application
run: yarn build && sudo rm -rf build

on-failure:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- run: echo "CI Workflow failed. Prod deployment was not triggered."
# - name: Generate migrations
# run: yarn migration:generate

# - name: Run migrations
# run: yarn migration:run

- name: Setup and restart service
run: |
sudo cp server-script/aihomeworkprod.service /etc/systemd/system
sudo systemctl daemon-reload
sudo systemctl restart aihomeworkprod.service
- name: Verify deployment
run: |
echo "Waiting for service to start..."
sleep 10
if sudo systemctl is-active --quiet aihomeworkprod.service; then
echo "Deployment successful!"
else
echo "Deployment failed!"
exit 1
fi

0 comments on commit 91257ec

Please sign in to comment.