-
Notifications
You must be signed in to change notification settings - Fork 1
334 lines (311 loc) · 13.3 KB
/
deploy.yml
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# Reusable workflow that will automatically deploy a desired branch to a server via Deployer.
#
# Usage:
#
# jobs:
# deploy:
# uses: wearerequired/composer-deployer/.github/workflows/deploy.yml@v1
# secrets: inherit
name: Deploy
on:
workflow_call:
inputs:
project_working_directory:
description: Relative path under $GITHUB_WORKSPACE to place the project repository.
type: string
default: ./project-repository
env:
ENVIRONMENT_URL: ${{ secrets.ENVIRONMENT_URL }}
ENVIRONMENT: staging
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
env:
LANG: en_US.utf8
defaults:
run:
shell: bash
steps:
- name: Set custom environment variables
run: |
TIMESTAMP=$(date +'%s')
BRANCH=${GITHUB_REF#refs/heads/}
SHA_SHORT=${GITHUB_SHA:0:8}
CHECK_SUITE_URL=$(curl -s -H "authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID} | jq -r '.check_suite_url')
CHECK_RUN_ID=$(curl -s -H "authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.antiope-preview+json" $CHECK_SUITE_URL/check-runs | jq '.check_runs[] | select(.name|test("\/ Deploy$")) | .id ')
echo "TIMESTAMP=$TIMESTAMP" >> $GITHUB_ENV
echo "BRANCH=$BRANCH" >> $GITHUB_ENV
echo "SHA_SHORT=$SHA_SHORT" >> $GITHUB_ENV
echo "CHECK_RUN_ID=$CHECK_RUN_ID" >> $GITHUB_ENV
if [ "$BRANCH" == "production" ]; then
echo "ENVIRONMENT=production" >> $GITHUB_ENV
echo "ENVIRONMENT_URL=${{ secrets.ENVIRONMENT_URL_PRODUCTION }}" >> $GITHUB_ENV
elif [ "$BRANCH" == "testing" ]; then
echo "ENVIRONMENT=testing" >> $GITHUB_ENV
echo "ENVIRONMENT_URL=${{ secrets.ENVIRONMENT_URL_TESTING }}" >> $GITHUB_ENV
fi;
- name: Send init Slack notification
id: slack
uses: wearerequired/slack-messaging-action@b8d341605efb42cb7ca093a88faaffee59176928 # v3.0.0
with:
bot_token: ${{ secrets.SLACK_BOT_TOKEN }}
channel: ${{ secrets.SLACK_CHANNEL }}
payload: >-
{
"icon_emoji": ":rocket:",
"username": "Deployer",
"text": "Deployment initialized.",
"attachments": [
{
"author_name": "${{ github.event.sender.login }}",
"author_link": "${{ github.event.sender.html_url }}",
"author_icon": "${{ github.event.sender.avatar_url }}",
"color": "warning",
"actions": [
{
"type": "button",
"text": "View log",
"url": "https://github.com/${{ github.repository }}/runs/${{ env.CHECK_RUN_ID }}?check_suite_focus=true"
}
],
"footer": "<https://github.com/${{ github.repository }}|${{ github.repository }}>",
"ts": "${{ env.TIMESTAMP }}"
}
]
}
- name: Set GitHub deployment status
if: ${{ github.event_name != 'deployment' }}
uses: bobheadxi/deployments@648679e8e4915b27893bd7dbc35cb504dc915bc8 # v1.5.0
id: deployment
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
env: ${{ env.ENVIRONMENT }}
- name: Checkout project repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
clean: false
persist-credentials: false
path: ${{ inputs.project_working_directory }}
- name: Set up PHP
uses: shivammathur/setup-php@6d7209f44a25a59e904b1ee9f3b0c33ab2cd888d # v2.29.0
with:
php-version: "8.2"
coverage: none
- name: Set up Deployer
run: |
composer global require deployer/deployer:v7.5.8
echo "$(composer global config bin-dir --absolute --quiet)" >> $GITHUB_PATH
- name: Set up SSH
run: |
mkdir ~/.ssh
if [ -f "${{ inputs.project_working_directory }}/.github/ssh_config" ]; then
cp ${{ inputs.project_working_directory }}/.github/ssh_config ~/.ssh/config
chmod 644 ~/.ssh/config
fi
echo "${{ secrets.DEPLOYER_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 400 ~/.ssh/id_rsa
echo "${{ secrets.DEPLOYER_SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts
chmod 644 ~/.ssh/known_hosts
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa
echo "SSH_AUTH_SOCK=$SSH_AUTH_SOCK" >> $GITHUB_ENV
- name: Update custom environment variables
run: echo "TIMESTAMP=$(date +'%s')" >> $GITHUB_ENV
- name: Send start Slack notification
uses: wearerequired/slack-messaging-action@b8d341605efb42cb7ca093a88faaffee59176928 # v3.0.0
with:
bot_token: ${{ secrets.SLACK_BOT_TOKEN }}
channel: ${{ secrets.SLACK_CHANNEL }}
message_id: ${{ steps.slack.outputs.message_id }}
payload: >-
{
"icon_emoji": ":rocket:",
"username": "Deployer",
"text": "Deployment started.",
"attachments": [
{
"author_name": "${{ github.event.sender.login }}",
"author_link": "${{ github.event.sender.html_url }}",
"author_icon": "${{ github.event.sender.avatar_url }}",
"color": "warning",
"fields": [
{
"title": "Environment",
"value": "<${{ env.ENVIRONMENT_URL }}|${{ env.ENVIRONMENT_URL }}> [${{ env.ENVIRONMENT }}]",
"short": true
},
{
"title": "Revision",
"value": "<https://github.com/${{ github.repository }}/commit/${{ github.sha }}|${{ env.SHA_SHORT }}@${{ env.BRANCH }}>",
"short": true
}
],
"actions": [
{
"type": "button",
"text": "View log",
"url": "https://github.com/${{ github.repository }}/runs/${{ env.CHECK_RUN_ID }}?check_suite_focus=true"
}
],
"footer": "<https://github.com/${{ github.repository }}|${{ github.repository }}>",
"ts": "${{ env.TIMESTAMP }}"
}
]
}
- name: Deploy via Deployer
working-directory: ${{ inputs.project_working_directory }}
run: |
if [ -n "${RUNNER_DEBUG+1}" ]; then
dep deploy stage=$ENVIRONMENT --branch $BRANCH --no-interaction -vvv
else
dep deploy stage=$ENVIRONMENT --branch $BRANCH --no-interaction
fi
- name: Unlock Deployer
working-directory: ${{ inputs.project_working_directory }}
if: ${{ cancelled() }}
run: |
if hash dep 2>/dev/null; then
if [ -n "${RUNNER_DEBUG+1}" ]; then
dep deploy:unlock stage=$ENVIRONMENT --no-interaction -vvv
else
dep deploy:unlock stage=$ENVIRONMENT --no-interaction
fi
fi
- name: Update custom environment variables
if: ${{ always() }}
run: echo "TIMESTAMP=$(date +'%s')" >> $GITHUB_ENV
- name: Send success Slack notification
if: ${{ success() }}
uses: wearerequired/slack-messaging-action@b8d341605efb42cb7ca093a88faaffee59176928 # v3.0.0
with:
bot_token: ${{ secrets.SLACK_BOT_TOKEN }}
channel: ${{ secrets.SLACK_CHANNEL }}
message_id: ${{ steps.slack.outputs.message_id }}
payload: >-
{
"icon_emoji": ":rocket:",
"username": "Deployer",
"text": "Deployment finished.",
"attachments": [
{
"author_name": "${{ github.event.sender.login }}",
"author_link": "${{ github.event.sender.html_url }}",
"author_icon": "${{ github.event.sender.avatar_url }}",
"color": "good",
"fields": [
{
"title": "Environment",
"value": "<${{ env.ENVIRONMENT_URL }}|${{ env.ENVIRONMENT_URL }}> [${{ env.ENVIRONMENT }}]",
"short": true
},
{
"title": "Revision",
"value": "<https://github.com/${{ github.repository }}/commit/${{ github.sha }}|${{ env.SHA_SHORT }}@${{ env.BRANCH }}>",
"short": true
}
],
"actions": [
{
"type": "button",
"text": "View wp-admin",
"url": "${{ env.ENVIRONMENT_URL }}/wp-admin/"
}
],
"footer": "<https://github.com/${{ github.repository }}|${{ github.repository }}>",
"ts": "${{ env.TIMESTAMP }}"
}
]
}
- name: Send failure Slack notification
if: ${{ failure() }}
uses: wearerequired/slack-messaging-action@b8d341605efb42cb7ca093a88faaffee59176928 # v3.0.0
with:
bot_token: ${{ secrets.SLACK_BOT_TOKEN }}
channel: ${{ secrets.SLACK_CHANNEL }}
message_id: ${{ steps.slack.outputs.message_id }}
payload: >-
{
"icon_emoji": ":boom:",
"username": "Deployer",
"text": "Deployment failed.",
"attachments": [
{
"author_name": "${{ github.event.sender.login }}",
"author_link": "${{ github.event.sender.html_url }}",
"author_icon": "${{ github.event.sender.avatar_url }}",
"color": "danger",
"fields": [
{
"title": "Environment",
"value": "<${{ env.ENVIRONMENT_URL }}|${{ env.ENVIRONMENT_URL }}> [${{ env.ENVIRONMENT }}]",
"short": true
},
{
"title": "Revision",
"value": "<https://github.com/${{ github.repository }}/commit/${{ github.sha }}|${{ env.SHA_SHORT }}@${{ env.BRANCH }}>",
"short": true
}
],
"actions": [
{
"type": "button",
"text": "View log",
"url": "https://github.com/${{ github.repository }}/runs/${{ env.CHECK_RUN_ID }}?check_suite_focus=true"
}
],
"footer": "<https://github.com/${{ github.repository }}|${{ github.repository }}>",
"ts": "${{ env.TIMESTAMP }}"
}
]
}
- name: Send cancelled Slack notification
if: ${{ cancelled() }}
uses: wearerequired/slack-messaging-action@b8d341605efb42cb7ca093a88faaffee59176928 # v3.0.0
with:
bot_token: ${{ secrets.SLACK_BOT_TOKEN }}
channel: ${{ secrets.SLACK_CHANNEL }}
message_id: ${{ steps.slack.outputs.message_id }}
payload: >-
{
"icon_emoji": ":rocket:",
"username": "Deployer",
"text": "Deployment cancelled.",
"attachments": [
{
"author_name": "${{ github.event.sender.login }}",
"author_link": "${{ github.event.sender.html_url }}",
"author_icon": "${{ github.event.sender.avatar_url }}",
"actions": [
{
"type": "button",
"text": "View log",
"url": "https://github.com/${{ github.repository }}/runs/${{ env.CHECK_RUN_ID }}?check_suite_focus=true"
}
],
"footer": "<https://github.com/${{ github.repository }}|${{ github.repository }}>",
"ts": "${{ env.TIMESTAMP }}"
}
]
}
- name: Update GitHub deployment status (deployment event)
uses: bobheadxi/deployments@648679e8e4915b27893bd7dbc35cb504dc915bc8 # v1.5.0
if: ${{ github.event_name == 'deployment' && always() }}
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
env: ${{ env.ENVIRONMENT }}
status: ${{ job.status }}
env_url: ${{ env.ENVIRONMENT_URL }}
deployment_id: ${{ github.event.deployment.id }}
- name: Update GitHub deployment status (push event)
uses: bobheadxi/deployments@648679e8e4915b27893bd7dbc35cb504dc915bc8 # v1.5.0
if: ${{ github.event_name != 'deployment' && always() }}
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
env: ${{ env.ENVIRONMENT }}
status: ${{ job.status }}
env_url: ${{ env.ENVIRONMENT_URL }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}