-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build(#4): Create deployment pipeline and bash script to run in produ…
…ction
- Loading branch information
1 parent
4a201a7
commit 2e97597
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
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
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." |