fix workflows location #1
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 to EC2 on Push | |
on: | |
push: | |
branches: | |
- main | |
- dev | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
environment: | |
name: ${{ github.ref == 'refs/heads/main' && 'prod' || 'dev' }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up SSH key | |
uses: webfactory/ssh-agent@v0.5.3 | |
with: | |
ssh-private-key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
- name: Push new code to EC2 | |
run: | | |
rsync -avz --exclude '.git*' --exclude 'node_modules' ./ ${{ env.EC2_USER }}@${{ env.EC2_SSH_HOST }}:/path/to/your/project/ | |
echo "Code has been pushed to the EC2 instance." | |
- name: Check for pending migrations | |
id: check_migrations | |
run: | | |
echo "Checking for pending migrations..." | |
pending_migrations=$(ssh -o "StrictHostKeyChecking=no" ${{ env.EC2_USER }}@${{ env.EC2_SSH_HOST }} "cd /path/to/your/project/ && source env/bin/activate && python manage.py showmigrations --plan | grep '\[ \]'") | |
echo "::set-output name=pending::${pending_migrations}" | |
if [ -z "$pending_migrations" ]; then | |
echo "No migrations found." | |
else: | |
echo "Migrations found, stopping services." | |
- name: Stop services if migrations are pending | |
if: steps.check_migrations.outputs.pending | |
run: | | |
echo "Stopping services..." | |
ssh -o "StrictHostKeyChecking=no" ${{ env.EC2_USER }}@${{ env.EC2_SSH_HOST }} "sudo systemctl stop gunicorn.service indexer.service" | |
- name: Run migrations | |
if: steps.check_migrations.outputs.pending | |
run: | | |
echo "Running migrations..." | |
ssh -o "StrictHostKeyChecking=no" ${{ env.EC2_USER }}@${{ env.EC2_SSH_HOST }} "cd /path/to/your/project/ && source env/bin/activate && python manage.py migrate" | |
- name: Run collectstatic | |
run: | | |
echo "Running collectstatic..." | |
ssh -o "StrictHostKeyChecking=no" ${{ env.EC2_USER }}@${{ env.EC2_SSH_HOST }} "cd /path/to/your/project/ && source env/bin/activate && python manage.py collectstatic --noinput" | |
- name: Restart services if migrations were run | |
if: steps.check_migrations.outputs.pending | |
run: | | |
echo "Restarting services after migrations..." | |
ssh -o "StrictHostKeyChecking=no" ${{ env.EC2_USER }}@${{ env.EC2_SSH_HOST }} "sudo systemctl restart gunicorn.service indexer.service" | |
- name: Restart services if no migrations | |
if: steps.check_migrations.outputs.pending == '' | |
run: | | |
echo "Restarting services without migration..." | |
ssh -o "StrictHostKeyChecking=no" ${{ env.EC2_USER }}@${{ env.EC2_SSH_HOST }} "sudo systemctl restart gunicorn.service" |