Skip to content

Commit

Permalink
Build(#4): Create deployment pipeline and bash script to run in produ…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
SverreNystad committed Dec 31, 2023
1 parent 4a201a7 commit 2e97597
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Deploy to EC2
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts
- name: Deploy to EC2
env:
EC2_HOST: ${{ secrets.EC2_HOST }}
EC2_USERNAME: ${{ secrets.EC2_USERNAME }}
run: |
ssh -o StrictHostKeyChecking=no $EC2_USERNAME@$EC2_HOST 'bash -s' < ./scripts/deploy_to_ec2.sh
31 changes: 31 additions & 0 deletions scripts/deploy_to_ec2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
# deploy_to_ec2.sh

# Navigate to your Django project directory
cd /path/to/your/django/project

# Pull the latest code
git pull origin main

# Install any new dependencies
pip install -r requirements.txt

# Make database migrations
python manage.py makemigrations

# Apply database migrations
python manage.py migrate

# Restart your Django application within a tmux session
TMUX_SESSION_NAME="django_app"

# Check if the tmux session exists
if tmux has-session -t $TMUX_SESSION_NAME 2>/dev/null; then
# If it exists, kill the session
tmux kill-session -t $TMUX_SESSION_NAME
fi

# Create a new tmux session and run your Django application
tmux new-session -d -s $TMUX_SESSION_NAME "python manage.py runserver 0.0.0.0:8000"

echo "Deployment completed."

0 comments on commit 2e97597

Please sign in to comment.