Skip to content

Merge pull request #572 from hngprojects/feat-user-table #4

Merge pull request #572 from hngprojects/feat-user-table

Merge pull request #572 from hngprojects/feat-user-table #4

Workflow file for this run

name: Deploy to staging Branch
on:
push:
branches:
- dev
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- 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 }}:/var/www/aihomework/staging/.env .env
env:
SSH_HOST: ${{ secrets.HOST }}
SSH_USERNAME: ${{ secrets.USERNAME }}
SSH_PASSWORD: ${{ secrets.PASSWORD }}
- name: Build Docker images using docker-compose
run: |
docker compose --env-file .env -f docker-compose.production.yml build
- 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: Deploy and start on remote server
run: |
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 }}