Update README.md #13
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: Deploy Django Project to EC2 | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout 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: Prepare SSH Key | |
run: | | |
echo "${{ secrets.EC2_SSH_KEY }}" > ec2_key.pem | |
chmod 600 ec2_key.pem | |
- name: Deploy to EC2 | |
env: | |
DJANGO_SECRET_KEY: ${{ secrets.DJANGO_SECRET_KEY }} | |
DB_NAME: ${{ secrets.DB_NAME }} | |
DB_USER: ${{ secrets.DB_USER }} | |
DB_PASSWORD: ${{ secrets.DB_PASSWORD }} | |
DB_HOST: ${{ secrets.DB_HOST }} | |
DB_PORT: ${{ secrets.DB_PORT }} | |
ALPHA_VANTAGE_API_KEY: ${{ secrets.ALPHA_VANTAGE_API_KEY }} | |
run: | | |
# Create a compressed archive of the project files | |
tar -czf django_project.tar.gz * | |
# Copy the archive to the EC2 instance | |
scp -o StrictHostKeyChecking=no -i ec2_key.pem django_project.tar.gz ec2-user@${{ secrets.EC2_HOST }}:~ | |
# SSH into EC2, extract files, set up environment, and run Docker Compose | |
ssh -o StrictHostKeyChecking=no -i ec2_key.pem ec2-user@${{ secrets.EC2_HOST }} " | |
mkdir -p ~/django_project && | |
tar -xzf ~/django_project.tar.gz -C ~/django_project && | |
cd ~/django_project && | |
echo 'DJANGO_SECRET_KEY=$DJANGO_SECRET_KEY' >> .env && | |
echo 'DB_NAME=$DB_NAME' >> .env && | |
echo 'DB_USER=$DB_USER' >> .env && | |
echo 'DB_PASSWORD=$DB_PASSWORD' >> .env && | |
echo 'DB_HOST=$DB_HOST' >> .env && | |
echo 'DB_PORT=$DB_PORT' >> .env && | |
echo 'ALPHA_VANTAGE_API_KEY=$ALPHA_VANTAGE_API_KEY' >> .env && | |
docker-compose up -d --build | |
" | |