added backup host #16
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: Django CI/CD | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Build and push Docker image | |
run: | | |
IMAGE_NAME="${{ secrets.DOCKERHUB_USERNAME }}/portfolio" | |
docker build -f build/docker/python/Dockerfile -t $IMAGE_NAME:latest . | |
docker push $IMAGE_NAME:latest | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build-and-push | |
if: github.ref == 'refs/heads/main' | |
steps: | |
- name: Setup SSH | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ secrets.EC2_SSH_KEY }} | |
- name: Deploy to EC2 | |
run: | | |
ssh -o StrictHostKeyChecking=no ec2-user@${{ secrets.EC2_PUBLIC_API }} << 'EOF' | |
set -e | |
# In this approach, we assume you have docker-compose files already present on the EC2 instance | |
# Or, you can store them in S3 or a config repository, and sync them if needed. | |
# Pull the latest Docker image | |
docker compose pull | |
# Run database migrations (assuming Django app is the "web" service): | |
docker compose run --rm web python manage.py migrate --noinput | |
# Bring up the containers | |
docker compose up -d | |
EOF |