-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from dprothero/automate-deployments-gha
Automated deployments
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |