Skip to content

Commit

Permalink
clinic: add deploy job to the github workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshgngwr committed Oct 25, 2023
1 parent a8c692e commit 3dc0e33
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
108 changes: 108 additions & 0 deletions .github/workflows/clinic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ env:
JDK_DISTRIBUTION: temurin
JAVA_VERSION: 21
NODE_VERSION: 21
DEPLOYMENT_ID: ${{ startsWith(github.ref, 'refs/tags/') && 'production' || 'staging' }}

jobs:
tests:
Expand Down Expand Up @@ -113,3 +114,110 @@ jobs:
context: clinic
push: true
tags: ${{ steps.docker-image-tags.outputs.tags }}

deploy:
name: Deploy
runs-on: ubuntu-latest
timeout-minutes: 10
if: github.event_name == 'push' # skip for pull requests.
needs:
- build
environment: ${{ env.DEPLOYMENT_ID }}
env:
SCP_DEST_PATH: /tmp/${{ github.sha }}
AWS_DEFAULT_REGION: ${{ secrets.AWS_REGION }}
AWS_ACCESS_KEY_ID: ${{ secrets.DEPLOY_BOT_ACCESS_KEY }}
defaults:
run:
working-directory: ./clinic
steps:
- name: Install prerequisites
run: |
sudo apt-get -qq update -y
sudo apt-get -qq install -y awscli
- name: Checkout Source
uses: actions/checkout@v4
- name: Get Workflow Runner's Public IP
id: workflow-runner-ip
run: echo "ipv4=$(curl -s 'https://api.ipify.org')" >> "$GITHUB_OUTPUT"
- name: Authorize runner's SSH access to EC2 host
run: |
aws ec2 authorize-security-group-ingress \
--group-id "$AWS_EC2_SG_ID" \
--protocol tcp \
--port 22 \
--cidr ${{ steps.workflow-runner-ip.outputs.ipv4 }}/32
env:
AWS_SECRET_ACCESS_KEY: ${{ secrets.DEPLOY_BOT_SECRET_KEY }}
AWS_EC2_SG_ID: ${{ secrets.AWS_EC2_SG_ID }}

- name: Upload Systemd Service and Logrotate Config
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
port: ${{ secrets.SSH_PORT }}
key: ${{ secrets.SSH_KEY }}
source: deploy/systemd,deploy/logrotate
target: ${{ env.SCP_DEST_PATH }}
rm: true # remove target directory before uploading data

- name: Configure Deployment
uses: appleboy/[email protected]
env:
APP_ENV: ${{ secrets.APP_ENV }}
GHCR_USER: ${{ github.actor }}
GHCR_TOKEN: ${{ github.token }}
GHCR_IMAGE: ghcr.io/nilenso/ashutosh-onboarding/clinic:${{ env.DEPLOYMENT_ID == 'staging' && 'latest' || github.ref_name }}
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
port: ${{ secrets.SSH_PORT }}
key: ${{ secrets.SSH_KEY }}
envs: APP_ENV,GHCR_USER,GHCR_TOKEN,GHCR_IMAGE
script_stop: true # stop script after first failure.
script: |
# stop existing deployment
service_name="clinic@${DEPLOYMENT_ID}.service"
sudo systemctl stop "$service_name" || true
# pull and tag the new docker image
echo "$GHCR_TOKEN" | docker login ghcr.io -u "$GHCR_USER" --password-stdin
docker pull "$GHCR_IMAGE"
docker tag "$GHCR_IMAGE" "clinic:${DEPLOYMENT_ID}"
docker logout ghcr.io
# write environment configuration
sudo mkdir -p /etc/clinic
echo "$APP_ENV" | sudo tee "/etc/clinic/${DEPLOYMENT_ID}.env" > /dev/null
# update systemd unit
sudo chown root:root ${{ env.SCP_DEST_PATH }}/deploy/systemd/*
sudo chmod 644 ${{ env.SCP_DEST_PATH }}/deploy/systemd/*
sudo mv ${{ env.SCP_DEST_PATH }}/deploy/systemd/* /etc/systemd/system/
# reload systemd daemon and restart the service
sudo systemctl daemon-reload
sudo systemctl reenable "$service_name"
sudo systemctl restart "$service_name"
# deploy logrotate config
sudo chown root:root ${{ env.SCP_DEST_PATH }}/deploy/logrotate/*
sudo chmod 644 ${{ env.SCP_DEST_PATH }}/deploy/logrotate/*
sudo mv ${{ env.SCP_DEST_PATH }}/deploy/logrotate/* /etc/logrotate/
# clean-up
sudo docker image prune -af
sudo rm -rf ${{ env.SCP_DEST_PATH }}
- name: Revoke runner's SSH access from EC2 host
if: ${{ always() }}
run: |
aws ec2 revoke-security-group-ingress \
--group-id "$AWS_EC2_SG_ID" \
--protocol tcp \
--port 22 \
--cidr ${{ steps.workflow-runner-ip.outputs.ipv4 }}/32
env:
AWS_SECRET_ACCESS_KEY: ${{ secrets.DEPLOY_BOT_SECRET_KEY }}
AWS_EC2_SG_ID: ${{ secrets.AWS_EC2_SG_ID }}
9 changes: 9 additions & 0 deletions clinic/deploy/logrotate/clinic
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/var/log/clinic/*.log {
daily
rotate 30
copytruncate
nocompress
nodelaycompress
notifempty
missingok
}
23 changes: 23 additions & 0 deletions clinic/deploy/systemd/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[Unit]
Description=Clinic %i
After=docker.service hapi-%i.service
Requires=docker.service hapi-%i.service

[Service]
Type=simple
ExecStartPre=-docker stop %N
ExecStartPre=-docker rm %N
ExecStart=docker run --rm --name %N \
--pull never
--env-file /etc/clinic/%i.env \
--network host \
--memory 500M \
--cpus 0.25 \
clinic:%i
StandardOutput=append:/var/log/clinic/%i.log
StandardError=append:/var/log/clinic/%i.log
SuccessExitStatus=130
Restart=on-failure

[Install]
WantedBy=default.target

0 comments on commit 3dc0e33

Please sign in to comment.