1- name : Deploy MkDocs
1+ name : Deploy and Cleanup MkDocs
22
33on :
4+ pull_request_target :
5+ types : [assigned, opened, synchronize, reopened, closed]
6+ paths :
7+ - docs/**
48 push :
59 branches :
610 - master
7- tags :
8- - ' tock-*'
911 paths :
1012 - docs/**
11- workflow_dispatch : {}
13+ workflow_dispatch :
14+ inputs :
15+ cleanup_branch :
16+ description : " Nom de la branche à nettoyer"
17+ required : true
18+ type : string
1219
1320permissions :
14- contents : read
15- pages : write
16- id-token : write
21+ contents : write
1722
1823concurrency :
1924 group : deploy-${{ github.repository }}
2025 cancel-in-progress : false
2126
2227jobs :
2328 build :
29+ if : " !(github.event.action == 'closed' || github.event_name == 'workflow_dispatch')"
2430 runs-on : ubuntu-latest
2531 steps :
26- - name : Checkout code
27- uses : actions/checkout@v4
32+ - uses : actions/checkout@v4
33+ with :
34+ ref : ${{ github.event.pull_request.head.ref }}
35+ repository : ${{ github.event.pull_request.head.repo.full_name }}
36+
37+ - name : Get artifact ID from the latest successful run
38+ id : get_artifact
39+ uses : actions/github-script@v6
40+ with :
41+ script : |
42+ const { owner, repo } = context.repo;
43+ const workflow_id = 'doc-deploy.yml';
2844
29- - name : Setup Python 3.13
30- uses : actions/setup-python@v5
45+ console.log('Récupération des derniers artefacts');
46+ try {
47+ const runs = await github.rest.actions.listWorkflowRuns({
48+ owner,
49+ repo,
50+ workflow_id: workflow_id,
51+ status: "success",
52+ per_page: 1
53+ });
54+
55+ if (runs.data.total_count === 0) {
56+ console.log("Aucun artefact trouvé. On continue quand même.");
57+ return;
58+ }
59+
60+ const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
61+ owner,
62+ repo,
63+ run_id: runs.data.workflow_runs[0].id
64+ });
65+
66+ const artifact = artifacts.data.artifacts.find(a => a.name === "github-pages");
67+ if (artifact) {
68+ console.log("Artifact trouvé avec ID :", artifact.id);
69+ const response = await github.rest.actions.downloadArtifact({
70+ owner,
71+ repo,
72+ artifact_id: artifact.id,
73+ archive_format: 'zip'
74+ });
75+ require('fs').writeFileSync("github-pages.zip", Buffer.from(response.data));
76+ 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/`);
77+ console.log("Artefact téléchargé et extrait");
78+ } else {
79+ console.log("Aucun artefact trouvé.");
80+ }
81+ } catch (error) {
82+ console.error("Erreur lors de la récupération de l'artefact :", error);
83+ }
84+
85+ - uses : actions/setup-python@v5
3186 with :
3287 python-version : 3.13.9
3388
3489 - name : Install dependencies
3590 run : |
36- echo "Installing dependencies "
91+ echo "Installation des dépendances "
3792 pip install -r docs/requirements.txt
3893
3994 - name : Build MkDocs site
4095 run : |
41- echo "Building MkDocs site"
96+ echo "Compilation du site MkDocs "
4297 cd docs
43- mkdocs build
98+ rm -rf site/${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
99+ mkdocs build --site-dir site/${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
44100
45101 - name : Upload static files as artifact
102+ id : deployment
46103 uses : actions/upload-pages-artifact@v3
47104 with :
48105 path : docs/site
49106
50107 deploy :
108+ if : " !(github.event.action == 'closed' || github.event_name == 'workflow_dispatch')"
51109 needs : build
52110 permissions :
53- pages : write
111+ pages : write
54112 id-token : write
113+ issues : write
114+ pull-requests : write
55115
56116 environment :
57117 name : github-pages
@@ -62,3 +122,147 @@ jobs:
62122 - name : Deploy to GitHub Pages
63123 id : deployment
64124 uses : actions/deploy-pages@v4
125+
126+ - name : Override page_url using PR branch name
127+ id : change-page-url
128+ run : |
129+ echo "Mise à jour de l'URL de déploiement"
130+ PR_BRANCH="${{ github.event.pull_request.head.ref }}"
131+ BASE_URL="https://doc.tock.ai/tock/"
132+ NEW_URL="${BASE_URL}${PR_BRANCH}/"
133+ echo "new_page_url=$NEW_URL" >> $GITHUB_OUTPUT
134+
135+ - uses : actions/github-script@v7
136+ name : Post comment
137+ if : ${{ github.event_name == 'pull_request_target' }}
138+ with :
139+ github-token : ${{ secrets.GITHUB_TOKEN }}
140+ script : |
141+ console.log('Commentaire sur la PR :');
142+ console.log(context.payload);
143+ context.payload.pull_request
144+ github.rest.issues.createComment({
145+ issue_number: context.payload.pull_request.number,
146+ owner: context.repo.owner,
147+ repo: context.repo.repo,
148+ body: `Website is published: [${{ steps.change-page-url.outputs.new_page_url }}](${{ steps.change-page-url.outputs.new_page_url }})`
149+ });
150+
151+ cleanup :
152+ if : github.event.action == 'closed' || github.event_name == 'workflow_dispatch'
153+ runs-on : ubuntu-latest
154+ permissions :
155+ pages : write
156+ id-token : write
157+ issues : write
158+ pull-requests : write
159+
160+ steps :
161+ - name : Checkout repository
162+ uses : actions/checkout@v4
163+
164+ - name : Get artifact ID from the latest successful run
165+ id : get_artifact
166+ uses : actions/github-script@v6
167+ with :
168+ script : |
169+ const { owner, repo } = context.repo;
170+ const workflow_id = 'doc-deploy.yml';
171+
172+ console.log('Récupération des derniers artefacts');
173+ try {
174+ const runs = await github.rest.actions.listWorkflowRuns({
175+ owner,
176+ repo,
177+ workflow_id: workflow_id,
178+ status: "success",
179+ per_page: 1
180+ });
181+
182+ if (runs.data.total_count === 0) {
183+ console.log("Aucun artefact trouvé. On continue quand même.");
184+ return;
185+ }
186+
187+ const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
188+ owner,
189+ repo,
190+ run_id: runs.data.workflow_runs[0].id
191+ });
192+
193+ const artifact = artifacts.data.artifacts.find(a => a.name === "github-pages");
194+ if (artifact) {
195+ console.log("Artifact trouvé avec ID :", artifact.id);
196+ const response = await github.rest.actions.downloadArtifact({
197+ owner,
198+ repo,
199+ artifact_id: artifact.id,
200+ archive_format: 'zip'
201+ });
202+ require('fs').writeFileSync("github-pages.zip", Buffer.from(response.data));
203+ 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/`);
204+ console.log("Artefact téléchargé et extrait");
205+ } else {
206+ console.log("Aucun artefact trouvé.");
207+ }
208+ } catch (error) {
209+ console.error("Erreur lors de la récupération de l'artefact :", error);
210+ }
211+
212+ - name : Lister les fichiers de l'artefact
213+ run : |
214+ echo "📂 Contenu de l'artefact après extraction :"
215+ ls -R /tmp/gh-artifact-extract/
216+
217+ - name : Déterminer la branche à nettoyer
218+ id : determine_branch
219+ run : |
220+ if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
221+ branch_name="${{ github.event.inputs.cleanup_branch }}"
222+ else
223+ branch_name="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
224+ fi
225+ echo "BRANCH_NAME=$branch_name" >> $GITHUB_ENV
226+ echo "🛠️ Branche ciblée pour le nettoyage : $branch_name"
227+
228+ - name : Supprimer le dossier associé à la PR fermée
229+ run : |
230+ echo "📂 Vérification du contenu :"
231+ ls -l docs/site/
232+
233+ folder_path="docs/site/${BRANCH_NAME}"
234+ echo "🛠️ Dossier ciblé : $folder_path"
235+
236+ if [ ! -d "$folder_path" ]; then
237+ echo "❌ Aucun dossier trouvé pour la branche ${BRANCH_NAME} !"
238+ else
239+ rm -rf "$folder_path"
240+ echo "✅ Dossier supprimé avec succès."
241+ fi
242+
243+ ls -l docs/site/
244+
245+ - name : Upload static files as artifact
246+ uses : actions/upload-pages-artifact@v3
247+ with :
248+ path : docs/site
249+
250+ - name : Deploy to GitHub Pages
251+ id : deployment
252+ uses : actions/deploy-pages@v4
253+
254+ - name : Post cleanup comment
255+ uses : actions/github-script@v7
256+ with :
257+ github-token : ${{ secrets.GITHUB_TOKEN }}
258+ script : |
259+ try {
260+ const response = await github.rest.issues.createComment({
261+ issue_number: context.payload.pull_request.number,
262+ owner: context.repo.owner,
263+ repo: context.repo.repo,
264+ body: `🚀 Cleanup completed for PR and associated branch: ${process.env.BRANCH_NAME}`
265+ });
266+ } catch (error) {
267+ console.error("Erreur lors de l'ajout du commentaire de nettoyage :", error);
268+ }
0 commit comments