Skip to content

Django CD with staging version to OCI #103

Django CD with staging version to OCI

Django CD with staging version to OCI #103

name: Django CD with staging version to OCI
on:
# workflow_run:
# workflows: ["Django Tests"]
# types:
# - completed
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
timeout-minutes: 60
environment: deply-oci-single-service
steps:
- name: Add environment variables to .env
run: |
echo "DEBUG_STATE=False" >> .env
echo "DOMAIN_NAME=${{ secrets.DOMAIN_NAME }}" >> .env
echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env
echo "ALLOWED_HOSTS_IP=${{ secrets.ALLOWED_HOSTS_IP }}" >> .env
echo "IP_ADDRESSES1=${{ secrets.IP_ADDRESSES1 }}" >> .env
echo "IP_ADDRESSES2=${{ secrets.IP_ADDRESSES2 }}" >> .env
echo "CSRF_TRUSTED_ORIGINS=${{ secrets.CSRF_TRUSTED_ORIGINS }}" >> .env
echo "DEFAULT_DB_ENGINE=${{ secrets.DEFAULT_DB_ENGINE }}" >> .env
echo "DEFAULT_DB_HOST=${{ secrets.DEFAULT_DB_HOST }}" >> .env
echo "DEFAULT_DB_PORT=${{ secrets.DEFAULT_DB_PORT }}" >> .env
echo "DEFAULT_DB_NAME=${{ secrets.DEFAULT_DB_NAME }}" >> .env
echo "DEFAULT_DB_USER=${{ secrets.DEFAULT_DB_USER }}" >> .env
echo "DEFAULT_DB_PASSWORD=${{ secrets.DEFAULT_DB_PASSWORD }}" >> .env
echo "DEFAULT_DB_CHARSET=${{ secrets.DEFAULT_DB_CHARSET }}" >> .env
echo "DEFAULT_DB_OPTIONS=${{ secrets.DEFAULT_DB_OPTIONS }}" >> .env
echo "DEFAULT_DB_CONN_MAX_AGE=${{ secrets.DEFAULT_DB_CONN_MAX_AGE }}" >> .env
echo "REPLICA1_DB_ENGINE=${{ secrets.REPLICA1_DB_ENGINE }}" >> .env
echo "REPLICA1_DB_HOST=${{ secrets.REPLICA1_DB_HOST }}" >> .env
echo "REPLICA1_DB_PORT=${{ secrets.REPLICA1_DB_PORT }}" >> .env
echo "REPLICA1_DB_NAME=${{ secrets.REPLICA1_DB_NAME }}" >> .env
echo "REPLICA1_DB_USER=${{ secrets.REPLICA1_DB_USER }}" >> .env
echo "REPLICA1_DB_PASSWORD=${{ secrets.REPLICA1_DB_PASSWORD }}" >> .env
echo "REPLICA1_DB_CHARSET=${{ secrets.REPLICA1_DB_CHARSET }}" >> .env
echo "REPLICA1_DB_OPTIONS=${{ secrets.REPLICA1_DB_OPTIONS }}" >> .env
echo "REPLICA1_DB_CONN_MAX_AGE=${{ secrets.REPLICA1_DB_CONN_MAX_AGE }}" >> .env
echo "DEFAULT_CACHE_HOST=${{ secrets.DEFAULT_CACHE_HOST }}" >> .env
echo "DEFAULT_CACHE_PORT=${{ secrets.DEFAULT_CACHE_PORT }}" >> .env
echo "DEFAULT_CACHE_DATABASE=${{ secrets.DEFAULT_CACHE_DATABASE }}" >> .env
echo "MAILGUN_ACCESS_KEY=${{ secrets.MAILGUN_ACCESS_KEY }}" >> .env
echo "MAILGUN_SERVER_NAME=${{ secrets.MAILGUN_SERVER_NAME }}" >> .env
echo "MAILGUN_DEFAULT_FROM_EMAIL=${{ secrets.MAILGUN_DEFAULT_FROM_EMAIL }}" >> .env
echo "SENDINBLUE_EMAIL_DOMAIN=${{ secrets.SENDINBLUE_EMAIL_DOMAIN }}" >> .env
echo "SENDINBLUE_EMAIL_HOST_USER=${{ secrets.SENDINBLUE_EMAIL_HOST_USER }}" >> .env
echo "SENDINBLUE_EMAIL_HOST_API=${{ secrets.SENDINBLUE_EMAIL_HOST_API }}" >> .env
echo "SENDGRID_API_KEY=${{ secrets.SENDGRID_API_KEY }}" >> .env
echo "SENDGRID_DEFAULT_FROM_EMAIL=${{ secrets.SENDGRID_DEFAULT_FROM_EMAIL }}" >> .env
echo "NAVER_CLIENT_ID=${{ secrets.NAVER_CLIENT_ID }}" >> .env
echo "NAVER_SECRET_KEY=${{ secrets.NAVER_SECRET_KEY }}" >> .env
echo "SENTRY_DNS=${{ secrets.SENTRY_DNS }}" >> .env
echo "RECAPTCHA_PUBLIC_KEY=${{ secrets.RECAPTCHA_PUBLIC_KEY }}" >> .env
echo "RECAPTCHA_PRIVATE_KEY=${{ secrets.RECAPTCHA_PRIVATE_KEY }}" >> .env
echo "CELERY_BROKER_URL=${{ secrets.CELERY_BROKER_URL }}" >> .env
- name: copy file via ssh key
uses: appleboy/scp-action@v0.1.4
with:
host: ${{ secrets.REMOTE_IP }}
username: ${{ secrets.REMOTE_SSH_ID }}
key: ${{ secrets.REMOTE_SSH_KEY }}
port: ${{ secrets.REMOTE_SSH_PORT }}
source: ".env"
target: ~/project/github/devspoon-web/www/
- name: Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_IP }}
username: ${{ secrets.REMOTE_SSH_ID }}
key: ${{ secrets.REMOTE_SSH_KEY }}
port: ${{ secrets.REMOTE_SSH_PORT }}
script: |
cd ~/project/github/devspoon-web/www/devspoon-portfolio-blog/
rm .env
git pull origin main
mv ../.env ./
sed -i 's/config.settings.dev/config.settings.stage/' manage.py
sed -i 's/config.settings.dev/config.settings.stage/' ./config/wsgi.py
sed -i 's/domain/${{ secrets.STAGE_SERVER_URL }}/' ./templates/home/robots.txt
cd ~/project/github/devspoon-web/compose/web_service/nginx_gunicorn/
echo "${{ secrets.SUDO }}" | sudo -S docker-compose stop
echo "${{ secrets.SUDO }}" | sudo -S docker-compose --profile celery up -d
- name: send msg to slack channel
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
run: |
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"Workflow stage deploy: [${{ github.workflow }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) has completed deploy to OCI."}' \
$SLACK_WEBHOOK_URL
handle-failure:
runs-on: ubuntu-latest
needs: [deploy]
if: failure() && needs.deploy.result == 'failure'
steps:
- name: Send Report to slack
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
run: |
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"Workflow stage deploy: [${{ github.workflow }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) errors has occurred."}' \
$SLACK_WEBHOOK_URL