-
-
Notifications
You must be signed in to change notification settings - Fork 599
206 lines (175 loc) Β· 7.31 KB
/
deploy.yml
File metadata and controls
206 lines (175 loc) Β· 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
name: Deploy to Vapor and Frontend
on:
push:
branches:
- main
- develop
- feature/vapor-env-updates
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy to'
required: true
default: 'staging'
type: choice
options:
- staging
- production
test_mode:
description: 'Run in test mode (no actual deployment)'
required: false
default: false
type: boolean
jobs:
backend:
name: Deploy Backend
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- 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.event_name }}" == "workflow_dispatch" ]]; then
echo "VAPOR_ENV=${{ github.event.inputs.environment }}" >> "$GITHUB_ENV"
echo "TEST_MODE=${{ github.event.inputs.test_mode }}" >> "$GITHUB_ENV"
elif [[ "${{ github.ref_name }}" == "main" ]]; then
echo "VAPOR_ENV=production" >> "$GITHUB_ENV"
echo "TEST_MODE=false" >> "$GITHUB_ENV"
elif [[ "${{ github.ref_name }}" == "develop" ]]; then
echo "VAPOR_ENV=staging" >> "$GITHUB_ENV"
echo "TEST_MODE=false" >> "$GITHUB_ENV"
else
echo "VAPOR_ENV=staging" >> "$GITHUB_ENV"
echo "TEST_MODE=false" >> "$GITHUB_ENV"
fi
- name: Log Branch and Environment
run: |
echo "π Deploying to Vapor environment: ${{ env.VAPOR_ENV }}"
echo "π§ͺ Test mode: ${{ env.TEST_MODE }}"
- name: Configure AWS Credentials
if: env.TEST_MODE != 'true'
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_ID }}
aws-region: eu-west-1
- name: Download Encrypted Environment File from S3
if: env.TEST_MODE != 'true'
working-directory: ./backend
run: |
aws s3 cp s3://hi.events-env-secrets/.env.${{ env.VAPOR_ENV }}.encrypted .env.${{ env.VAPOR_ENV }}.encrypted
echo "β
Downloaded .env.${{ env.VAPOR_ENV }}.encrypted from S3"
- name: Validate Deployment Configuration
working-directory: ./backend
run: |
if [[ "${{ env.TEST_MODE }}" == "true" ]]; then
echo "β
TEST MODE: Would deploy to ${{ env.VAPOR_ENV }} environment"
echo "vapor deploy ${{ env.VAPOR_ENV }} --dry-run"
exit 0
fi
- name: Deploy to Vapor
if: env.TEST_MODE != 'true'
working-directory: ./backend
run: vapor deploy ${{ env.VAPOR_ENV }}
env:
VAPOR_API_TOKEN: ${{ secrets.VAPOR_API_TOKEN }}
frontend:
name: Deploy Frontend
runs-on: ubuntu-latest
needs: backend
steps:
- uses: actions/checkout@v3
- name: Set Deployment Environment
run: |
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
if [[ "${{ github.event.inputs.environment }}" == "staging" ]]; 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
echo "TEST_MODE=${{ github.event.inputs.test_mode }}" >> "$GITHUB_ENV"
elif [[ "${{ github.ref_name }}" == "main" ]]; then
echo "DO_APP_ID=${{ secrets.DIGITALOCEAN_PRODUCTION_APP_ID }}" >> "$GITHUB_ENV"
echo "TEST_MODE=false" >> "$GITHUB_ENV"
elif [[ "${{ github.ref_name }}" == "develop" ]]; then
echo "DO_APP_ID=${{ secrets.DIGITALOCEAN_STAGING_APP_ID }}" >> "$GITHUB_ENV"
echo "TEST_MODE=false" >> "$GITHUB_ENV"
else
echo "DO_APP_ID=${{ secrets.DIGITALOCEAN_STAGING_APP_ID }}" >> "$GITHUB_ENV"
echo "TEST_MODE=false" >> "$GITHUB_ENV"
fi
- name: Log Environment Settings
run: |
echo "π Deploying frontend to DigitalOcean App: ${{ env.DO_APP_ID }}"
echo "π§ͺ Test mode: ${{ env.TEST_MODE }}"
- name: Validate Deployment Configuration (Test Mode)
if: env.TEST_MODE == 'true'
run: |
echo "β
TEST MODE: Would trigger deployment for DigitalOcean App: ${{ env.DO_APP_ID }}"
echo "curl -X POST 'https://api.digitalocean.com/v2/apps/${{ env.DO_APP_ID }}/deployments'"
exit 0
- name: Trigger Deployment on DigitalOcean
if: env.TEST_MODE != 'true'
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 201 ] && [ "$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
if: env.TEST_MODE != 'true'
run: |
MAX_RETRIES=60
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" == "CANCELED" ]]; 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