Skip to content

Commit

Permalink
Merge pull request #179 from dprothero/automate-deployments-gha
Browse files Browse the repository at this point in the history
Automated deployments
  • Loading branch information
eepMoody authored Mar 25, 2023
2 parents e39b244 + 5cd4b0d commit 8c4e362
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: "Build and deploy"
on:
push:
branches:
- staging
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set Docker image tag
run: |
if [[ $GITHUB_REF == refs/heads/main ]]; then
echo "DOCKER_IMAGE_TAG=latest" >> $GITHUB_ENV
elif [[ $GITHUB_REF == refs/heads/staging ]]; then
echo "DOCKER_IMAGE_TAG=staging" >> $GITHUB_ENV
fi
- name: Build and push
uses: docker/build-push-action@v4
with:
push: true
tags: eepmoody/open5e-api:${{ env.DOCKER_IMAGE_TAG }}

deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: Check out repository code
uses: actions/checkout@v3
- name: Install jq tool
run: |
sudo apt-get update
sudo apt-get install jq
- name: Set Digital Ocean app id
run: |
if [[ $GITHUB_REF == refs/heads/main ]]; then
echo "DIGITALOCEAN_APP_ID=${{ secrets.MAIN_APP_ID }}" >> $GITHUB_ENV
elif [[ $GITHUB_REF == refs/heads/staging ]]; then
echo "DIGITALOCEAN_APP_ID=${{ secrets.STAGING_APP_ID }}" >> $GITHUB_ENV
fi
- name: Deploy to Digital Ocean
env:
DIGITALOCEAN_TOKEN: ${{ secrets.DIGITALOCEAN_TOKEN }}
run: ./scripts/do_app_deploy.sh
18 changes: 18 additions & 0 deletions .github/workflows/pr_validation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "PR validation"
on:
pull_request:
types: [opened, reopened]
push:
branches-ignore:
- staging
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Build Docker image
uses: docker/build-push-action@v4
with:
push: false
tags: open5e-api:${{ github.ref_name }}
47 changes: 47 additions & 0 deletions scripts/do_app_deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

set -euo pipefail

echo "Triggering deployment..."

curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
-d '{"force_build": true}' \
-o ./deploy.json \
"https://api.digitalocean.com/v2/apps/$DIGITALOCEAN_APP_ID/deployments"

DEPLOYMENT_ID=$(jq -r '.deployment.id' ./deploy.json)
PHASE=$(jq -r '.deployment.phase' ./deploy.json)

COMPLETE_STATUSES="ACTIVE SUPERSEDED ERROR CANCELED"

# While the deployment is not complete, wait 10 seconds and check again.
while ! echo "$COMPLETE_STATUSES" | grep -q "$PHASE"; do
echo "Deployment phase: $PHASE"
sleep 10
curl -s \
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
-o ./deploy.json \
"https://api.digitalocean.com/v2/apps/$DIGITALOCEAN_APP_ID/deployments/$DEPLOYMENT_ID"

PHASE=$(jq -r '.deployment.phase' ./deploy.json)
done

if [ "$PHASE" = "ACTIVE" ]; then
curl -s \
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
-o ./app.json \
"https://api.digitalocean.com/v2/apps/$DIGITALOCEAN_APP_ID"

APP_URL=$(jq -r '.app.live_url' ./app.json)

echo "Deployment complete. Your app is live at $APP_URL"

exit 0
fi

echo "Deployment failed. Phase: $PHASE"
echo "Deployment logs: https://cloud.digitalocean.com/apps/$DIGITALOCEAN_APP_ID/deployments/$DEPLOYMENT_ID"

exit 1

0 comments on commit 8c4e362

Please sign in to comment.