Merge HiEventsDev/Hi.Events/tree/v1.0.0-alpha.1 #5
This file contains hidden or 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
name: Deploy to Vapor and Frontend | |
on: | |
push: | |
branches: | |
- main | |
- develop | |
jobs: | |
backend: | |
name: Deploy Backend | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Setup PHP | |
uses: shivammathur/setup-php@v2 | |
with: | |
php-version: 8.3 | |
tools: composer:v2 | |
coverage: none | |
- name: Prepare Laravel Environment | |
working-directory: ./backend | |
run: | | |
mkdir -p bootstrap/cache | |
chmod -R 775 bootstrap/cache | |
- name: Prepare HTMLPurifier Cache Directory | |
working-directory: ./backend | |
run: | | |
mkdir -p storage/app/htmlpurifier | |
chmod -R 775 storage/app/htmlpurifier | |
- name: Install Dependencies | |
working-directory: ./backend | |
run: composer install --no-dev --no-progress --no-scripts --optimize-autoloader | |
- name: Install Vapor CLI | |
run: composer global require laravel/vapor-cli | |
- name: Set Deployment Environment | |
run: | | |
if [[ "${{ github.ref_name }}" == "develop" ]]; then | |
echo "VAPOR_ENV=staging" >> "$GITHUB_ENV" | |
else | |
echo "VAPOR_ENV=production" >> "$GITHUB_ENV" | |
fi | |
- name: Log Branch and Environment | |
run: | | |
echo "π Deploying branch ${{ github.ref_name }} to Vapor environment: $VAPOR_ENV" | |
- name: Deploy to Vapor | |
working-directory: ./backend | |
run: vapor deploy $VAPOR_ENV | |
env: | |
VAPOR_API_TOKEN: ${{ secrets.VAPOR_API_TOKEN }} | |
frontend: | |
name: Deploy Frontend | |
runs-on: ubuntu-latest | |
needs: backend | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Set Deployment Environment | |
run: | | |
if [[ "${{ github.ref_name }}" == "develop" ]]; then | |
echo "DO_APP_ID=${{ secrets.DIGITALOCEAN_STAGING_APP_ID }}" >> "$GITHUB_ENV" | |
else | |
echo "DO_APP_ID=${{ secrets.DIGITALOCEAN_PRODUCTION_APP_ID }}" >> "$GITHUB_ENV" | |
fi | |
- name: Trigger Deployment on DigitalOcean | |
id: trigger_deployment | |
run: | | |
RESPONSE=$(curl -s -o response.json -w "%{http_code}" -X POST "https://api.digitalocean.com/v2/apps/$DO_APP_ID/deployments" \ | |
-H "Authorization: Bearer ${{ secrets.DIGITALOCEAN_API_TOKEN }}" \ | |
-H "Content-Type: application/json") | |
if [ "$RESPONSE" -ne 200 ]; then | |
ERROR_MSG=$(jq -r '.message // "Unknown error occurred."' response.json) | |
echo "β Failed to trigger deployment. HTTP Status: $RESPONSE. Error: $ERROR_MSG" | |
exit 1 | |
fi | |
DEPLOYMENT_ID=$(jq -r '.deployment.id' response.json) | |
if [ "$DEPLOYMENT_ID" == "null" ]; then | |
echo "β Failed to extract deployment ID." | |
exit 1 | |
fi | |
echo "::add-mask::$DEPLOYMENT_ID" | |
echo "β Deployment triggered successfully." | |
echo "deployment_id=$DEPLOYMENT_ID" >> "$GITHUB_ENV" | |
- name: Poll Deployment Status | |
run: | | |
MAX_RETRIES=30 | |
SLEEP_TIME=10 | |
COUNTER=0 | |
while [ $COUNTER -lt $MAX_RETRIES ]; do | |
RESPONSE=$(curl -s -X GET "https://api.digitalocean.com/v2/apps/$DO_APP_ID/deployments/${{ env.deployment_id }}" \ | |
-H "Authorization: Bearer ${{ secrets.DIGITALOCEAN_API_TOKEN }}" \ | |
-H "Content-Type: application/json") | |
STATUS=$(echo "$RESPONSE" | jq -r '.deployment.phase') | |
echo "π Deployment Status: $STATUS" | |
if [ "$STATUS" == "ACTIVE" ]; then | |
echo "β Deployment completed successfully." | |
exit 0 | |
elif [[ "$STATUS" == "FAILED" || "$STATUS" == "CANCELLED" ]]; then | |
echo "β Deployment failed or was cancelled." | |
exit 1 | |
fi | |
COUNTER=$((COUNTER + 1)) | |
echo "β³ Retrying in $SLEEP_TIME seconds... ($COUNTER/$MAX_RETRIES)" | |
sleep $SLEEP_TIME | |
done | |
echo "β° Deployment timed out." | |
exit 1 |