-
Notifications
You must be signed in to change notification settings - Fork 140
268 lines (234 loc) · 9.07 KB
/
doc-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
name: Deploy and Cleanup MkDocs
on:
pull_request_target:
types: [assigned, opened, synchronize, reopened, closed]
paths:
- docs/**
push:
branches:
- master
paths:
- docs/**
workflow_dispatch:
inputs:
cleanup_branch:
description: "Nom de la branche à nettoyer"
required: true
type: string
permissions:
contents: write
concurrency:
group: deploy-${{ github.repository }}
cancel-in-progress: false
jobs:
build:
if: "!(github.event.action == 'closed' || github.event_name == 'workflow_dispatch')"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: Get artifact ID from the latest successful run
id: get_artifact
uses: actions/github-script@v6
with:
script: |
const { owner, repo } = context.repo;
const workflow_id = 'doc-deploy.yml';
console.log('Récupération des derniers artefacts');
try {
const runs = await github.rest.actions.listWorkflowRuns({
owner,
repo,
workflow_id: workflow_id,
status: "success",
per_page: 1
});
if (runs.data.total_count === 0) {
console.log("Aucun artefact trouvé. On continue quand même.");
return;
}
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner,
repo,
run_id: runs.data.workflow_runs[0].id
});
const artifact = artifacts.data.artifacts.find(a => a.name === "github-pages");
if (artifact) {
console.log("Artifact trouvé avec ID :", artifact.id);
const response = await github.rest.actions.downloadArtifact({
owner,
repo,
artifact_id: artifact.id,
archive_format: 'zip'
});
require('fs').writeFileSync("github-pages.zip", Buffer.from(response.data));
require('child_process').execSync(`unzip -o github-pages.zip -d "/tmp/gh-artifact-extract" && mkdir -p docs/site && tar xvf /tmp/gh-artifact-extract/artifact.tar -C docs/site/`);
console.log("Artefact téléchargé et extrait");
} else {
console.log("Aucun artefact trouvé.");
}
} catch (error) {
console.error("Erreur lors de la récupération de l'artefact :", error);
}
- uses: actions/setup-python@v5
with:
python-version: 3.13.0
- name: Install dependencies
run: |
echo "Installation des dépendances"
pip install -r docs/requirements.txt
- name: Build MkDocs site
run: |
echo "Compilation du site MkDocs"
cd docs
rm -rf site/${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
mkdocs build --site-dir site/${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
- name: Upload static files as artifact
id: deployment
uses: actions/upload-pages-artifact@v3
with:
path: docs/site
deploy:
if: "!(github.event.action == 'closed' || github.event_name == 'workflow_dispatch')"
needs: build
permissions:
pages: write
id-token: write
issues: write
pull-requests: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
- name: Override page_url using PR branch name
id: change-page-url
run: |
echo "Mise à jour de l'URL de déploiement"
PR_BRANCH="${{ github.event.pull_request.head.ref }}"
BASE_URL="https://doc.tock.ai/tock/"
NEW_URL="${BASE_URL}${PR_BRANCH}/"
echo "new_page_url=$NEW_URL" >> $GITHUB_OUTPUT
- uses: actions/github-script@v7
name: Post comment
if: ${{ github.event_name == 'pull_request_target' }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
console.log('Commentaire sur la PR :');
console.log(context.payload);
context.payload.pull_request
github.rest.issues.createComment({
issue_number: context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Website is published: [${{ steps.change-page-url.outputs.new_page_url }}](${{ steps.change-page-url.outputs.new_page_url }})`
});
cleanup:
if: github.event.action == 'closed' || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
permissions:
pages: write
id-token: write
issues: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Get artifact ID from the latest successful run
id: get_artifact
uses: actions/github-script@v6
with:
script: |
const { owner, repo } = context.repo;
const workflow_id = 'doc-deploy.yml';
console.log('Récupération des derniers artefacts');
try {
const runs = await github.rest.actions.listWorkflowRuns({
owner,
repo,
workflow_id: workflow_id,
status: "success",
per_page: 1
});
if (runs.data.total_count === 0) {
console.log("Aucun artefact trouvé. On continue quand même.");
return;
}
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner,
repo,
run_id: runs.data.workflow_runs[0].id
});
const artifact = artifacts.data.artifacts.find(a => a.name === "github-pages");
if (artifact) {
console.log("Artifact trouvé avec ID :", artifact.id);
const response = await github.rest.actions.downloadArtifact({
owner,
repo,
artifact_id: artifact.id,
archive_format: 'zip'
});
require('fs').writeFileSync("github-pages.zip", Buffer.from(response.data));
require('child_process').execSync(`unzip -o github-pages.zip -d "/tmp/gh-artifact-extract" && mkdir -p docs/site && tar xvf /tmp/gh-artifact-extract/artifact.tar -C docs/site/`);
console.log("Artefact téléchargé et extrait");
} else {
console.log("Aucun artefact trouvé.");
}
} catch (error) {
console.error("Erreur lors de la récupération de l'artefact :", error);
}
- name: Lister les fichiers de l'artefact
run: |
echo "📂 Contenu de l'artefact après extraction :"
ls -R /tmp/gh-artifact-extract/
- name: Déterminer la branche à nettoyer
id: determine_branch
run: |
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
branch_name="${{ github.event.inputs.cleanup_branch }}"
else
branch_name="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
fi
echo "BRANCH_NAME=$branch_name" >> $GITHUB_ENV
echo "🛠️ Branche ciblée pour le nettoyage : $branch_name"
- name: Supprimer le dossier associé à la PR fermée
run: |
echo "📂 Vérification du contenu :"
ls -l docs/site/
folder_path="docs/site/${BRANCH_NAME}"
echo "🛠️ Dossier ciblé : $folder_path"
if [ ! -d "$folder_path" ]; then
echo "❌ Aucun dossier trouvé pour la branche ${BRANCH_NAME} !"
else
rm -rf "$folder_path"
echo "✅ Dossier supprimé avec succès."
fi
ls -l docs/site/
- name: Upload static files as artifact
uses: actions/upload-pages-artifact@v3
with:
path: docs/site
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
- name: Post cleanup comment
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
try {
const response = await github.rest.issues.createComment({
issue_number: context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `🚀 Cleanup completed for PR and associated branch: ${process.env.BRANCH_NAME}`
});
} catch (error) {
console.error("Erreur lors de l'ajout du commentaire de nettoyage :", error);
}