|
1 |
| -name: Deploy to Vapor Production |
| 1 | +name: Deploy to Vapor and Frontend |
2 | 2 |
|
3 | 3 | on:
|
4 | 4 | push:
|
5 | 5 | branches:
|
6 |
| - - master |
7 |
| - - feat/vapor |
| 6 | + - main |
| 7 | + - develop |
8 | 8 |
|
9 | 9 | jobs:
|
10 |
| - vapor: |
11 |
| - name: Deploy to Vapor Production |
| 10 | + backend: |
| 11 | + name: Deploy Backend |
12 | 12 | runs-on: ubuntu-latest
|
13 | 13 |
|
14 | 14 | steps:
|
15 |
| - # Step 1: Checkout the repository |
16 | 15 | - uses: actions/checkout@v2
|
17 | 16 |
|
18 |
| - # Step 2: Setup PHP |
19 | 17 | - name: Setup PHP
|
20 | 18 | uses: shivammathur/setup-php@v2
|
21 | 19 | with:
|
22 | 20 | php-version: 8.3
|
23 | 21 | tools: composer:v2
|
24 | 22 | coverage: none
|
25 | 23 |
|
26 |
| - # Step 3: Prepare Laravel Environment |
27 | 24 | - name: Prepare Laravel Environment
|
28 | 25 | working-directory: ./backend
|
29 | 26 | run: |
|
30 | 27 | mkdir -p bootstrap/cache
|
31 | 28 | chmod -R 775 bootstrap/cache
|
32 | 29 |
|
33 |
| - # Step 4: Prepare HTMLPurifier Cache Directory |
34 | 30 | - name: Prepare HTMLPurifier Cache Directory
|
35 | 31 | working-directory: ./backend
|
36 | 32 | run: |
|
37 | 33 | mkdir -p storage/app/htmlpurifier
|
38 | 34 | chmod -R 775 storage/app/htmlpurifier
|
39 | 35 |
|
40 |
| - # Step 5: Install Dependencies |
41 | 36 | - name: Install Dependencies
|
42 | 37 | working-directory: ./backend
|
43 | 38 | run: composer install --no-dev --no-progress --no-scripts --optimize-autoloader
|
44 | 39 |
|
45 |
| - # Step 6: Install Vapor CLI |
46 | 40 | - name: Install Vapor CLI
|
47 | 41 | run: composer global require laravel/vapor-cli
|
48 | 42 |
|
49 |
| - # Step 7: Log the branch being deployed (Optional enhancement) |
50 |
| - - name: Log Branch |
51 |
| - run: echo "Deploying branch ${{ github.ref_name }}" |
| 43 | + - name: Set Deployment Environment |
| 44 | + run: | |
| 45 | + if [[ "${{ github.ref_name }}" == "develop" ]]; then |
| 46 | + echo "VAPOR_ENV=staging" >> "$GITHUB_ENV" |
| 47 | + else |
| 48 | + echo "VAPOR_ENV=production" >> "$GITHUB_ENV" |
| 49 | + fi |
| 50 | +
|
| 51 | + - name: Log Branch and Environment |
| 52 | + run: | |
| 53 | + echo "🚀 Deploying branch ${{ github.ref_name }} to Vapor environment: $VAPOR_ENV" |
52 | 54 |
|
53 |
| - # Step 8: Deploy to the Vapor Production Environment |
54 |
| - - name: Deploy to Production |
| 55 | + - name: Deploy to Vapor |
55 | 56 | working-directory: ./backend
|
56 |
| - run: vapor deploy production |
| 57 | + run: vapor deploy $VAPOR_ENV |
57 | 58 | env:
|
58 | 59 | VAPOR_API_TOKEN: ${{ secrets.VAPOR_API_TOKEN }}
|
| 60 | + |
| 61 | + frontend: |
| 62 | + name: Deploy Frontend |
| 63 | + runs-on: ubuntu-latest |
| 64 | + needs: backend |
| 65 | + |
| 66 | + steps: |
| 67 | + - uses: actions/checkout@v2 |
| 68 | + |
| 69 | + - name: Set Deployment Environment |
| 70 | + run: | |
| 71 | + if [[ "${{ github.ref_name }}" == "develop" ]]; then |
| 72 | + echo "DO_APP_ID=${{ secrets.DIGITALOCEAN_STAGING_APP_ID }}" >> "$GITHUB_ENV" |
| 73 | + else |
| 74 | + echo "DO_APP_ID=${{ secrets.DIGITALOCEAN_PRODUCTION_APP_ID }}" >> "$GITHUB_ENV" |
| 75 | + fi |
| 76 | +
|
| 77 | + - name: Trigger Deployment on DigitalOcean |
| 78 | + id: trigger_deployment |
| 79 | + run: | |
| 80 | + RESPONSE=$(curl -s -o response.json -w "%{http_code}" -X POST "https://api.digitalocean.com/v2/apps/$DO_APP_ID/deployments" \ |
| 81 | + -H "Authorization: Bearer ${{ secrets.DIGITALOCEAN_API_TOKEN }}" \ |
| 82 | + -H "Content-Type: application/json") |
| 83 | +
|
| 84 | + if [ "$RESPONSE" -ne 200 ]; then |
| 85 | + ERROR_MSG=$(jq -r '.message // "Unknown error occurred."' response.json) |
| 86 | + echo "❌ Failed to trigger deployment. HTTP Status: $RESPONSE. Error: $ERROR_MSG" |
| 87 | + exit 1 |
| 88 | + fi |
| 89 | +
|
| 90 | + DEPLOYMENT_ID=$(jq -r '.deployment.id' response.json) |
| 91 | + if [ "$DEPLOYMENT_ID" == "null" ]; then |
| 92 | + echo "❌ Failed to extract deployment ID." |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | +
|
| 96 | + echo "::add-mask::$DEPLOYMENT_ID" |
| 97 | + echo "✅ Deployment triggered successfully." |
| 98 | +
|
| 99 | + echo "deployment_id=$DEPLOYMENT_ID" >> "$GITHUB_ENV" |
| 100 | +
|
| 101 | + - name: Poll Deployment Status |
| 102 | + run: | |
| 103 | + MAX_RETRIES=30 |
| 104 | + SLEEP_TIME=10 |
| 105 | + COUNTER=0 |
| 106 | +
|
| 107 | + while [ $COUNTER -lt $MAX_RETRIES ]; do |
| 108 | + RESPONSE=$(curl -s -X GET "https://api.digitalocean.com/v2/apps/$DO_APP_ID/deployments/${{ env.deployment_id }}" \ |
| 109 | + -H "Authorization: Bearer ${{ secrets.DIGITALOCEAN_API_TOKEN }}" \ |
| 110 | + -H "Content-Type: application/json") |
| 111 | +
|
| 112 | + STATUS=$(echo "$RESPONSE" | jq -r '.deployment.phase') |
| 113 | +
|
| 114 | + echo "🔄 Deployment Status: $STATUS" |
| 115 | +
|
| 116 | + if [ "$STATUS" == "ACTIVE" ]; then |
| 117 | + echo "✅ Deployment completed successfully." |
| 118 | + exit 0 |
| 119 | + elif [[ "$STATUS" == "FAILED" || "$STATUS" == "CANCELLED" ]]; then |
| 120 | + echo "❌ Deployment failed or was cancelled." |
| 121 | + exit 1 |
| 122 | + fi |
| 123 | +
|
| 124 | + COUNTER=$((COUNTER + 1)) |
| 125 | + echo "⏳ Retrying in $SLEEP_TIME seconds... ($COUNTER/$MAX_RETRIES)" |
| 126 | + sleep $SLEEP_TIME |
| 127 | + done |
| 128 | +
|
| 129 | + echo "⏰ Deployment timed out." |
| 130 | + exit 1 |
0 commit comments