Skip to content

Backend_Deploy

Backend_Deploy #3

name: Build, Push, and Deploy
on:
push:
branches:
- '**'
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out the code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: Build Docker image
run: |
docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/kanban-backend:${{ github.sha }} .
docker tag ${{ secrets.DOCKER_HUB_USERNAME }}/kanban-backend:${{ github.sha }}
- name: Push Docker image to DockerHub
run: |
docker push ${{ secrets.DOCKER_HUB_USERNAME }}/kanban-backend:${{ github.sha }}
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: Check out the code
uses: actions/checkout@v3
- name: Deploy to Remote Server
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SERVER_HOST: ${{ secrets.SERVER_HOST }}
SERVER_USER: ${{ secrets.SERVER_USER }}
IMAGE_TAG: ${{ github.sha }}
run: |
echo "$SSH_PRIVATE_KEY" > private_key.pem
chmod 600 private_key.pem
ssh -i private_key.pem $SERVER_USER@$SERVER_HOST "
docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/kanban-backend:${{ github.sha }} &&
docker-compose -f /home/ubuntu/Projects/kanban-backend/docker-compose.yml down &&
docker-compose -f /home/ubuntu/Projects/kanban-backend/docker-compose.yml up -d"
cleanup:
runs-on: ubuntu-latest
needs: deploy
steps:
- name: Remove old Docker images
run: |
docker image prune -af