Skip to content

Merge pull request #111 from mongodb-developer:main #68

Merge pull request #111 from mongodb-developer:main

Merge pull request #111 from mongodb-developer:main #68

Workflow file for this run

name: Deployment (all)
on:
push:
branches:
- PROD
- STAGING
env:
EC2_PRIVATE_KEY: ${{ secrets.EC2_PRIVATE_KEY }}
PROD_IP: ${{ secrets.PROD_IP }}
STAGING_IP: ${{ secrets.STAGING_IP }}
GAME_CLIENT_PORT: ${{ secrets.GAME_CLIENT_PORT }}
CONNECTION_STRING_PROD: ${{ secrets.CONNECTION_STRING_PROD }}
REST_SERVICE_IP_PROD: ${{ secrets.REST_SERVICE_IP_PROD }}
REST_SERVICE_PORT_PROD: ${{ secrets.REST_SERVICE_PORT_PROD }}
ATLAS_CHART_EMBED_DASHBOARD_URL_PROD: ${{ secrets.ATLAS_CHART_EMBED_DASHBOARD_URL_PROD }}
ATLAS_CHART_ID_EVENT_PROD: ${{ secrets.ATLAS_CHART_ID_EVENT_PROD }}
ATLAS_CHART_ID_PLAYER_PROD: ${{ secrets.ATLAS_CHART_ID_PLAYER_PROD }}
ATLAS_CHART_ID_HOME_PROD: ${{ secrets.ATLAS_CHART_ID_HOME_PROD }}
ATLAS_CHART_ID_SIMILAR_PROD: ${{ secrets.ATLAS_CHART_ID_SIMILAR_PROD }}
CONNECTION_STRING_STAGING: ${{ secrets.CONNECTION_STRING_STAGING }}
REST_SERVICE_IP_STAGING: ${{ secrets.REST_SERVICE_IP_STAGING }}
REST_SERVICE_PORT_STAGING: ${{ secrets.REST_SERVICE_PORT_STAGING }}
ATLAS_CHART_EMBED_DASHBOARD_URL_STAGING: ${{ secrets.ATLAS_CHART_EMBED_DASHBOARD_URL_STAGING }}
ATLAS_CHART_ID_EVENT_STAGING: ${{ secrets.ATLAS_CHART_ID_EVENT_STAGING }}
ATLAS_CHART_ID_PLAYER_STAGING: ${{ secrets.ATLAS_CHART_ID_PLAYER_STAGING}}
ATLAS_CHART_ID_HOME_STAGING: ${{ secrets.ATLAS_CHART_ID_HOME_STAGING }}
ATLAS_CHART_ID_SIMILAR_STAGING: ${{ secrets.ATLAS_CHART_ID_SIMILAR_STAGING }}
jobs:
deployment:
runs-on: ubuntu-latest
steps:
# Environment
- name: Set Environment Variables For PROD And STAGING
run: |
if [ "${{ github.ref }}" == "refs/heads/STAGING" ]; then
echo "ENVIRONMENT_IP=${STAGING_IP}" >> $GITHUB_ENV
echo "DATABASE_NAME=Leafsteroids_stage" >> $GITHUB_ENV
echo "CONNECTION_STRING=${CONNECTION_STRING_STAGING}" >> $GITHUB_ENV
echo "REST_SERVICE_IP=${REST_SERVICE_IP_STAGING}" >> $GITHUB_ENV
echo "REST_SERVICE_PORT=${REST_SERVICE_PORT_STAGING}" >> $GITHUB_ENV
echo "ATLAS_CHART_EMBED_DASHBOARD_URL=${ATLAS_CHART_EMBED_DASHBOARD_URL_STAGING}" >> $GITHUB_ENV
echo "ATLAS_CHART_ID_EVENT=${ATLAS_CHART_ID_EVENT_STAGING}" >> $GITHUB_ENV
echo "ATLAS_CHART_ID_PLAYER=${ATLAS_CHART_ID_PLAYER_STAGING}" >> $GITHUB_ENV
echo "ATLAS_CHART_ID_HOME=${ATLAS_CHART_ID_HOME_STAGING}" >> $GITHUB_ENV
echo "ATLAS_CHART_ID_SIMILAR=${ATLAS_CHART_ID_SIMILAR_STAGING}" >> $GITHUB_ENV
echo "GAME_CLIENT_PORT=${GAME_CLIENT_PORT}" >> $GITHUB_ENV
else
echo "ENVIRONMENT_IP=${PROD_IP}" >> $GITHUB_ENV
echo "DATABASE_NAME=Leafsteroids" >> $GITHUB_ENV
echo "CONNECTION_STRING=${CONNECTION_STRING_PROD}" >> $GITHUB_ENV
echo "REST_SERVICE_IP=${REST_SERVICE_IP_PROD}" >> $GITHUB_ENV
echo "REST_SERVICE_PORT=${REST_SERVICE_PORT_PROD}" >> $GITHUB_ENV
echo "ATLAS_CHART_EMBED_DASHBOARD_URL=${ATLAS_CHART_EMBED_DASHBOARD_URL_PROD}" >> $GITHUB_ENV
echo "ATLAS_CHART_ID_EVENT=${ATLAS_CHART_ID_EVENT_PROD}" >> $GITHUB_ENV
echo "ATLAS_CHART_ID_PLAYER=${ATLAS_CHART_ID_PLAYER_PROD}" >> $GITHUB_ENV
echo "ATLAS_CHART_ID_HOME=${ATLAS_CHART_ID_HOME_PROD}" >> $GITHUB_ENV
echo "ATLAS_CHART_ID_SIMILAR=${ATLAS_CHART_ID_SIMILAR_PROD}" >> $GITHUB_ENV
echo "GAME_CLIENT_PORT=${GAME_CLIENT_PORT}" >> $GITHUB_ENV
fi
- name: Checkout Code
uses: actions/checkout@v3
- name: Create SSH Key File
run: |
echo "${EC2_PRIVATE_KEY}" > ec2_private_key.pem
chmod 400 ec2_private_key.pem
- name: Install .NET SDK 7 On The AWS EC2 Instance
run: |
echo ${{ env.ENVIRONMENT_IP }}
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem ubuntu@${{ env.ENVIRONMENT_IP }} 'sudo apt-get update && sudo apt-get install -y dotnet-sdk-7.0'
# REST service
- name: Create The .env File Inside The `rest_service` Directory
run: |
echo "CONNECTION_STRING=${{ env.CONNECTION_STRING }}" > ./rest_service/.env
echo "DATABASE_NAME=${{ env.DATABASE_NAME }}" >> ./rest_service/.env
- name: Copy The rest_service Directory To The AWS EC2 Instance
run: |
ssh -o StrictHostKeyChecking=no -i ec2_private_key.pem ubuntu@${{ env.ENVIRONMENT_IP }} "rm -rf /home/ubuntu/rest_service && mkdir /home/ubuntu/rest_service"
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem -r ./rest_service ubuntu@${{ env.ENVIRONMENT_IP }}:/home/ubuntu/
- name: Copy The Systemd Service To The AWS EC2 Instance
run: |
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem deployment/rest_service.service ubuntu@${{ env.ENVIRONMENT_IP }}:/home/ubuntu/rest_service.service
- name: Enable And Start The Systemd Service On The AWS EC2 Instance
run: |
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem ubuntu@${{ env.ENVIRONMENT_IP }} 'sudo mv /home/ubuntu/rest_service.service /etc/systemd/system/rest_service.service'
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem ubuntu@${{ env.ENVIRONMENT_IP }} 'sudo systemctl enable rest_service.service'
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem ubuntu@${{ env.ENVIRONMENT_IP }} 'sudo systemctl daemon-reload'
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem ubuntu@${{ env.ENVIRONMENT_IP }} 'sudo systemctl start rest_service.service'
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem ubuntu@${{ env.ENVIRONMENT_IP }} 'sudo systemctl restart rest_service.service'
# Website
- name: Create The .env File Inside website Directory
run: |
echo "REST_SERVICE_IP=${{ env.REST_SERVICE_IP }}" > ./website/.env
echo "REST_SERVICE_PORT=${{ env.REST_SERVICE_PORT }}" >> ./website/.env
echo "ATLAS_CHART_EMBED_DASHBOARD_URL=${{ env.ATLAS_CHART_EMBED_DASHBOARD_URL }}" >> ./website/.env
echo "ATLAS_CHART_ID_EVENT=${{ env.ATLAS_CHART_ID_EVENT }}" >> ./website/.env
echo "ATLAS_CHART_ID_PLAYER=${{ env.ATLAS_CHART_ID_PLAYER }}" >> ./website/.env
echo "ATLAS_CHART_ID_HOME=${{ env.ATLAS_CHART_ID_HOME }}" >> ./website/.env
echo "ATLAS_CHART_ID_HOME=${{ env.ATLAS_CHART_ID_HOME }}" >> ./website/.env
echo "ATLAS_CHART_ID_SIMILAR=${{ env.ATLAS_CHART_ID_SIMILAR }}" >> ./website/.env
echo "GAME_CLIENT_PORT=${{ env.GAME_CLIENT_PORT }}" >> ./website/.env
- name: Copy The website Directory To The AWS EC2 Instance
run: |
ssh -o StrictHostKeyChecking=no -i ec2_private_key.pem ubuntu@${{ env.ENVIRONMENT_IP }} "rm -rf /home/ubuntu/website && mkdir /home/ubuntu/website"
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem -r ./website ubuntu@${{ env.ENVIRONMENT_IP }}:/home/ubuntu/
- name: Copy The Systemd Service To The AWS EC2 Instance
run: |
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem deployment/website.service ubuntu@${{ env.ENVIRONMENT_IP }}:/home/ubuntu/website.service
- name: Enable And Start The Systemd Service On The AWS EC2 Instance
run: |
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem ubuntu@${{ env.ENVIRONMENT_IP }} 'sudo mv /home/ubuntu/website.service /etc/systemd/system/website.service'
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem ubuntu@${{ env.ENVIRONMENT_IP }} 'sudo systemctl enable website.service'
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem ubuntu@${{ env.ENVIRONMENT_IP }} 'sudo systemctl daemon-reload'
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem ubuntu@${{ env.ENVIRONMENT_IP }} 'sudo systemctl start website.service'
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ec2_private_key.pem ubuntu@${{ env.ENVIRONMENT_IP }} 'sudo systemctl restart website.service'