-
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.
- Loading branch information
1 parent
152cb35
commit f740c64
Showing
1 changed file
with
90 additions
and
49 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 |
---|---|---|
@@ -1,66 +1,107 @@ | ||
# This is a basic workflow to help you get started with Actions | ||
name: Deploy to EC2 on Push | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
- dev | ||
branches: [dev] | ||
|
||
env: | ||
AWS_REGION: "us-east-1" | ||
|
||
# Permission can be added at job level or workflow level | ||
permissions: | ||
id-token: write # This is required for requesting the JWT | ||
contents: read # This is required for actions/checkout | ||
jobs: | ||
deploy: | ||
AssumeRoleAndCallIdentity: | ||
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/[email protected] | ||
- name: Git clone the repository | ||
uses: actions/checkout@v3 | ||
- name: configure aws credentials | ||
uses: aws-actions/[email protected] | ||
with: | ||
ssh-private-key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | ||
|
||
- name: Push new code to EC2 | ||
role-to-assume: arn:aws:iam::471112976510:role/GitHubAction-AssumeRoleWithAction | ||
role-session-name: GitHub_to_AWS_via_FederatedOIDC | ||
aws-region: ${{ env.AWS_REGION }} | ||
# Hello from AWS: WhoAmI | ||
- name: Sts GetCallerIdentity | ||
run: | | ||
rsync -avz --exclude '.git*' --exclude 'node_modules' ./ ${{ vars.EC2_USER }}@${{ vars.EC2_SSH_HOST }}:/home/ec2-user/django-indexer | ||
echo "Code has been pushed to the EC2 instance." | ||
aws sts get-caller-identity | ||
- name: Check for pending migrations | ||
id: check_migrations | ||
# Step 3 - check the application-name and deployment group name | ||
- name: Create CodeDeploy Deployment | ||
id: deploy | ||
run: | | ||
echo "Checking for pending migrations..." | ||
pending_migrations=$(ssh -o "StrictHostKeyChecking=no" ${{ vars.EC2_USER }}@${{ vars.EC2_SSH_HOST }} "cd /home/ec2-user/django-indexer && 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." | ||
aws deploy create-deployment \ | ||
--application-name django-indexer \ | ||
--deployment-group-name django-indexer-dev \ | ||
--deployment-config-name CodeDeployDefault.AllAtOnce \ | ||
--github-location repository=${{ github.repository }},commitId=${{ github.sha }} | ||
- name: Stop services if migrations are pending | ||
if: steps.check_migrations.outputs.pending | ||
run: | | ||
echo "Stopping services..." | ||
ssh -o "StrictHostKeyChecking=no" ${{ vars.EC2_USER }}@${{ vars.EC2_SSH_HOST }} "sudo systemctl stop gunicorn.service indexer.service" | ||
# name: Deploy to EC2 on Push | ||
|
||
- name: Run migrations | ||
if: steps.check_migrations.outputs.pending | ||
run: | | ||
echo "Running migrations..." | ||
ssh -o "StrictHostKeyChecking=no" ${{ vars.EC2_USER }}@${{ vars.EC2_SSH_HOST }} "cd /home/ec2-user/django-indexer && source env/bin/activate && python manage.py migrate" | ||
# on: | ||
# push: | ||
# branches: | ||
# - main | ||
# - dev | ||
|
||
- name: Run collectstatic | ||
run: | | ||
echo "Running collectstatic..." | ||
ssh -o "StrictHostKeyChecking=no" ${{ vars.EC2_USER }}@${{ vars.EC2_SSH_HOST }} "cd /home/ec2-user/django-indexer && source env/bin/activate && python manage.py collectstatic --noinput" | ||
# jobs: | ||
# deploy: | ||
# runs-on: ubuntu-latest | ||
# environment: | ||
# name: ${{ github.ref == 'refs/heads/main' && 'prod' || 'dev' }} | ||
# steps: | ||
# - name: Checkout code | ||
# uses: actions/checkout@v2 | ||
|
||
- name: Restart services if migrations were run | ||
if: steps.check_migrations.outputs.pending | ||
run: | | ||
echo "Restarting services after migrations..." | ||
ssh -o "StrictHostKeyChecking=no" ${{ vars.EC2_USER }}@${{ vars.EC2_SSH_HOST }} "sudo systemctl restart gunicorn.service indexer.service" | ||
# - name: Set up SSH key | ||
# uses: webfactory/[email protected] | ||
# with: | ||
# ssh-private-key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | ||
|
||
- name: Restart services if no migrations | ||
if: steps.check_migrations.outputs.pending == '' | ||
run: | | ||
echo "Restarting services without migration..." | ||
ssh -o "StrictHostKeyChecking=no" ${{ vars.EC2_USER }}@${{ vars.EC2_SSH_HOST }} "sudo systemctl restart gunicorn.service" | ||
# - name: Push new code to EC2 | ||
# run: | | ||
# rsync -avz --exclude '.git*' --exclude 'node_modules' ./ ${{ vars.EC2_USER }}@${{ vars.EC2_SSH_HOST }}:/home/ec2-user/django-indexer | ||
# 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" ${{ vars.EC2_USER }}@${{ vars.EC2_SSH_HOST }} "cd /home/ec2-user/django-indexer && 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" ${{ vars.EC2_USER }}@${{ vars.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" ${{ vars.EC2_USER }}@${{ vars.EC2_SSH_HOST }} "cd /home/ec2-user/django-indexer && source env/bin/activate && python manage.py migrate" | ||
|
||
# - name: Run collectstatic | ||
# run: | | ||
# echo "Running collectstatic..." | ||
# ssh -o "StrictHostKeyChecking=no" ${{ vars.EC2_USER }}@${{ vars.EC2_SSH_HOST }} "cd /home/ec2-user/django-indexer && 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" ${{ vars.EC2_USER }}@${{ vars.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" ${{ vars.EC2_USER }}@${{ vars.EC2_SSH_HOST }} "sudo systemctl restart gunicorn.service" |