Skip to content

Commit

Permalink
Create reusable .NET deployment workflow. (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zifah authored Aug 7, 2024
1 parent e0b8a09 commit c2ce7c8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 85 deletions.
48 changes: 6 additions & 42 deletions .github/workflows/api-deploy-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,9 @@ on:

jobs:
deploy:
if: github.repository_owner == 'Yorubaname'
runs-on: ubuntu-latest
environment: staging

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: '6.0.x'

- name: Build project
run: dotnet publish Api -c Release -o out

- name: Deploy API to EC2
env:
SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }}
EC2_INSTANCE_IP: ${{ vars.EC2_INSTANCE_IP}}
SERVICE_NAME: ${{ vars.API_SERVICE_NAME }}
SERVICE_PATH: ${{ vars.API_SERVICE_PATH }}
run: |
echo "$SSH_PRIVATE_KEY" > private_key
chmod 600 private_key
rsync -avz --exclude 'appsettings.json' -e "ssh -i private_key -o StrictHostKeyChecking=no" out/ ec2-user@$EC2_INSTANCE_IP:$SERVICE_PATH
ssh -i private_key -o StrictHostKeyChecking=no ec2-user@$EC2_INSTANCE_IP "
sudo systemctl restart $SERVICE_NAME
sleep 3
STATUS=\$(sudo systemctl is-active $SERVICE_NAME)
echo \"The service $SERVICE_NAME is currently \$STATUS\"
if [ -z \$STATUS ]; then
echo 'Service status is empty or not defined'
exit 1
elif [ \$STATUS != 'active' ]; then
sudo systemctl status $SERVICE_NAME --no-pager
exit 1
else
echo 'API was restarted successfully after deployment.'
fi
"
shell: bash
uses: ./.github/workflows/deploy-dotnet-service-ec2.yml
with:
project-name: Api
dotnet-version: '6.0.x'
service-name: ${{ vars.API_SERVICE_NAME }}
service-path: ${{ vars.API_SERVICE_PATH }}
62 changes: 62 additions & 0 deletions .github/workflows/deploy-dotnet-service-ec2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Deploy DotNet service to EC2

on:
workflow_call:
inputs:
project-name:
required: true
type: string
dotnet-version:
required: true
type: string
service-name:
required: true
type: string
service-path:
required: true
type: string

jobs:
deploy:
if: github.repository_owner == 'Yorubaname'
runs-on: ubuntu-latest
environment: staging

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: ${{ inputs.dotnet-version }}

- name: Build project
run: dotnet publish ${{ inputs.project-name }} -c Release -o out

- name: Deploy to EC2
env:
SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }}
EC2_INSTANCE_IP: ${{ vars.EC2_INSTANCE_IP }}
SERVICE_NAME: ${{ inputs.service-name }}
SERVICE_PATH: ${{ inputs.service-path }}
run: |
echo "$SSH_PRIVATE_KEY" > private_key
chmod 600 private_key
rsync -avz --exclude 'appsettings.json' -e "ssh -i private_key -o StrictHostKeyChecking=no" out/ ec2-user@$EC2_INSTANCE_IP:$SERVICE_PATH
ssh -i private_key -o StrictHostKeyChecking=no ec2-user@$EC2_INSTANCE_IP "
sudo systemctl restart $SERVICE_NAME
sleep 3
STATUS=\$(sudo systemctl is-active $SERVICE_NAME)
echo \"The service $SERVICE_NAME is currently \$STATUS\"
if [ -z \$STATUS ]; then
echo 'Service status is empty or not defined'
exit 1
elif [ \$STATUS != 'active' ]; then
sudo systemctl status $SERVICE_NAME --no-pager
exit 1
else
echo '${{ inputs.project-name }} was restarted successfully after deployment.'
fi
"
shell: bash
50 changes: 7 additions & 43 deletions .github/workflows/website-deploy-staging.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Deploy website to EC2 staging
name: Deploy Website to EC2 staging

on:
push:
Expand All @@ -7,45 +7,9 @@ on:

jobs:
deploy:
if: github.repository_owner == 'Yorubaname'
runs-on: ubuntu-latest
environment: staging

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: '8.0.x'

- name: Build project
run: dotnet publish Website -c Release -o out

- name: Deploy website to EC2
env:
SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }}
EC2_INSTANCE_IP: ${{ vars.EC2_INSTANCE_IP}}
SERVICE_NAME: ${{ vars.WEBSITE_SERVICE_NAME }}
SERVICE_PATH: ${{ vars.WEBSITE_SERVICE_PATH }}
run: |
echo "$SSH_PRIVATE_KEY" > private_key
chmod 600 private_key
rsync -avz --exclude 'appsettings.json' -e "ssh -i private_key -o StrictHostKeyChecking=no" out/ ec2-user@$EC2_INSTANCE_IP:$SERVICE_PATH
ssh -i private_key -o StrictHostKeyChecking=no ec2-user@$EC2_INSTANCE_IP "
sudo systemctl restart $SERVICE_NAME
sleep 3
STATUS=\$(sudo systemctl is-active $SERVICE_NAME)
echo \"The service $SERVICE_NAME is currently \$STATUS\"
if [ -z \$STATUS ]; then
echo 'Service status is empty or not defined'
exit 1
elif [ \$STATUS != 'active' ]; then
sudo systemctl status $SERVICE_NAME --no-pager
exit 1
else
echo 'Website was restarted successfully after deployment.'
fi
"
shell: bash
uses: ./.github/workflows/deploy-dotnet-service-ec2.yml
with:
project-name: Website
dotnet-version: '8.0.x'
service-name: ${{ vars.WEBSITE_SERVICE_NAME }}
service-path: ${{ vars.WEBSITE_SERVICE_PATH }}

0 comments on commit c2ce7c8

Please sign in to comment.