Update backend_deploy.yml #10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} . | |
- 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: | | |
mkdir -p ~/.ssh | |
touch ~/.ssh/id_rsa | |
chmod 666 ~/.ssh/id_rsa | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
chmod 400 ~/.ssh/id_rsa | |
ssh-keyscan $SERVER_HOST >> ~/.ssh/known_hosts | |
ssh -i id_rsa $SERVER_USER@$SERVER_HOST " | |
cd /home/ubuntu/Projects/kanban-backend/ | |
docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/kanban-backend:${{ github.sha }} && | |
docker compose down && | |
docker compose up -d" | |
cleanup: | |
runs-on: ubuntu-latest | |
needs: deploy | |
steps: | |
- name: Remove old Docker images | |
run: | | |
docker image prune -af |