-
Notifications
You must be signed in to change notification settings - Fork 7
203 lines (187 loc) · 7.83 KB
/
review-app.yml
File metadata and controls
203 lines (187 loc) · 7.83 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
# inspired from https://github.com/dokku/github-action/blob/master/example-workflows/review-app.yaml
name: Deploy review app
on:
workflow_dispatch:
inputs:
site:
description: 'Site to deploy in preview'
required: true
default: 'ecospheres'
type: choice
options:
- ecospheres
- meteo-france
- logistique
- defis
- hackathon
- simplifions
- culture
pr_number:
description: 'Pull Request number to deploy'
required: true
type: string
pull_request:
types: [synchronize, closed]
jobs:
deploy_review_app:
runs-on: ubuntu-latest
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ matrix.site }}
cancel-in-progress: false
strategy:
max-parallel: 2
matrix:
site:
- ecospheres
- meteo-france
- logistique
- defis
- hackathon
- simplifions
- culture
permissions:
deployments: write
# Run for all PRs including forks and for manual triggers
if: github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch'
steps:
- name: Check if review app exists
id: check_app
continue-on-error: true
uses: appleboy/ssh-action@823bd89e131d8d508129f9443cad5855e9ba96f0 # v1.2.4
with:
host: ${{ secrets.REVIEW_APP_SSH_HOST }}
username: dokku
key: ${{ secrets.REVIEW_APP_SSH_PRIVATE_KEY }}
port: 22
script: apps:exists deploy-preview-${{ github.event.pull_request.number }}--${{ matrix.site }}
- name: Set APP_EXISTS variable
run: |
if [ ${{ steps.check_app.outcome }} == 'success' ]; then
echo "APP_EXISTS=true" >> $GITHUB_ENV
else
echo "APP_EXISTS=false" >> $GITHUB_ENV
fi
- name: Determine deployment strategy
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
# For manual deployment, only process the selected site
if [ "${{ matrix.site }}" != "${{ github.event.inputs.site }}" ]; then
echo "Skipping site ${{ matrix.site }} (not selected for manual deployment)"
exit 0
fi
echo "SITE=${{ github.event.inputs.site }}" >> $GITHUB_ENV
echo "PR_NUMBER=${{ github.event.inputs.pr_number }}" >> $GITHUB_ENV
echo "EVENT_ACTION=manual" >> $GITHUB_ENV
echo "SHOULD_DEPLOY=true" >> $GITHUB_ENV
echo "SHOULD_CREATE=true" >> $GITHUB_ENV
else
echo "SITE=${{ matrix.site }}" >> $GITHUB_ENV
echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
echo "EVENT_ACTION=${{ github.event.action }}" >> $GITHUB_ENV
# Set defaults (no deployment, no creation)
echo "SHOULD_DEPLOY=false" >> $GITHUB_ENV
echo "SHOULD_CREATE=false" >> $GITHUB_ENV
# Override defaults based on event type
if [ "${{ github.event.action }}" = "synchronize" ]; then
if [ "$APP_EXISTS" = "true" ]; then
echo "SHOULD_DEPLOY=true" >> $GITHUB_ENV
echo "SHOULD_CREATE=false" >> $GITHUB_ENV
echo "Review app exists, will redeploy"
else
echo "Review app does not exist, manual creation required"
fi
elif [ "${{ github.event.action }}" = "closed" ]; then
if [ "$APP_EXISTS" = "true" ]; then
echo "Will cleanup existing review app"
echo "SHOULD_DESTROY=true" >> $GITHUB_ENV
else
echo "SHOULD_DESTROY=false" >> $GITHUB_ENV
echo "No cleanup needed"
exit 0
fi
fi
fi
- name: Debug event
run: |
echo "Event name: ${{ github.event_name }}"
echo "Site: ${{ env.SITE }}"
echo "PR number: ${{ env.PR_NUMBER }}"
echo "Event action: ${{ env.EVENT_ACTION }}"
echo "App exists: ${{ env.APP_EXISTS }}"
echo "Should deploy: ${{ env.SHOULD_DEPLOY }}"
echo "Should create: ${{ env.SHOULD_CREATE }}"
echo "Should destroy: ${{ env.SHOULD_DESTROY }}"
- name: Exit if no deployment needed (except for cleanup)
if: env.SHOULD_DEPLOY != 'true' && env.EVENT_ACTION != 'closed'
run: |
echo "No deployment needed for this event, exiting"
exit 0
- name: Cloning repo
if: env.SHOULD_DEPLOY == 'true'
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
with:
fetch-depth: 0
- name: Start deployment
if: env.SHOULD_DEPLOY == 'true'
uses: chrnorm/deployment-action@55729fcebec3d284f60f5bcabbd8376437d696b1 # v2.0.7
id: deployment
with:
token: ${{ github.token }}
environment: ${{ env.SITE }}-preview
initial-status: in_progress
transient-environment: true
- name: Create the review app
if: env.SHOULD_CREATE == 'true'
uses: dokku/github-action@823c08b33e974704528c7c7f3d3d8002426e7634 # v1.9.0
with:
command: review-apps:create
git_remote_url: ${{ secrets.REVIEW_APP_SSH_URL }}
review_app_name: deploy-preview-${{ env.PR_NUMBER }}--${{ env.SITE }}
ssh_private_key: ${{ secrets.REVIEW_APP_SSH_PRIVATE_KEY }}
# omitting this will prevent the app from being built (which is what we want)
# branch: 'main'
- name: Set site id as build arg
if: env.SHOULD_DEPLOY == 'true'
uses: appleboy/ssh-action@823bd89e131d8d508129f9443cad5855e9ba96f0 # v1.2.4
with:
host: ${{ secrets.REVIEW_APP_SSH_HOST }}
username: dokku
key: ${{ secrets.REVIEW_APP_SSH_PRIVATE_KEY }}
port: 22
script: |
docker-options:add deploy-preview-${{ env.PR_NUMBER }}--${{ env.SITE }} build "--build-arg VITE_SITE_ID=${{ env.SITE }}"
- name: Push to dokku
if: env.SHOULD_DEPLOY == 'true'
uses: dokku/github-action@823c08b33e974704528c7c7f3d3d8002426e7634 # v1.9.0
with:
git_remote_url: ${{ secrets.REVIEW_APP_SSH_URL }}
review_app_name: deploy-preview-${{ env.PR_NUMBER }}--${{ env.SITE }}
ssh_private_key: ${{ secrets.REVIEW_APP_SSH_PRIVATE_KEY }}
git_push_flags: '--force'
branch: 'main'
- name: Enable SSL with Let's Encrypt
if: env.SHOULD_CREATE == 'true'
uses: appleboy/ssh-action@823bd89e131d8d508129f9443cad5855e9ba96f0 # v1.2.4
with:
host: ${{ secrets.REVIEW_APP_SSH_HOST }}
username: dokku
key: ${{ secrets.REVIEW_APP_SSH_PRIVATE_KEY }}
port: 22
script: |
letsencrypt:enable deploy-preview-${{ env.PR_NUMBER }}--${{ env.SITE }}
- name: Destroy the review app
if: github.event.action == 'closed' && env.SHOULD_DESTROY == 'true'
uses: dokku/github-action@823c08b33e974704528c7c7f3d3d8002426e7634 # v1.9.0
with:
command: review-apps:destroy
git_remote_url: ${{ secrets.REVIEW_APP_SSH_URL }}
review_app_name: deploy-preview-${{ env.PR_NUMBER }}--${{ env.SITE }}
ssh_private_key: ${{ secrets.REVIEW_APP_SSH_PRIVATE_KEY }}
- name: Update deployment status
if: env.SHOULD_DEPLOY == 'true'
uses: chrnorm/deployment-status@9a72af4586197112e0491ea843682b5dc280d806 # v2.0.3
with:
token: ${{ github.token }}
environment-url: https://deploy-preview-${{ env.PR_NUMBER }}--${{ env.SITE }}.sandbox.data.developpement-durable.gouv.fr
state: ${{ job.status == 'success' && 'success' || 'failure' }}
deployment-id: ${{ steps.deployment.outputs.deployment_id }}