From d69cb6904575b90e0f0023b4d37d7566e62b40cb Mon Sep 17 00:00:00 2001
From: Chris Coley <chris@codingallnight.com>
Date: Sun, 30 Apr 2023 07:38:40 -0700
Subject: [PATCH] Move the cache purge to a child pipeline

---
 .gitlab-ci.yml            | 32 +++++++++++++++++---------------
 purge-cache.gitlab-ci.yml | 25 +++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 15 deletions(-)
 create mode 100644 purge-cache.gitlab-ci.yml

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index ed066fb..9210749 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -71,26 +71,28 @@ pages:
     - if: $CI_COMMIT_BRANCH == 'custom-domain'
 
 # Purge all of this site's URLs from the Cloudflare cache
-purge-cache:
-  stage: .post
+create-purge-json:
+  stage: deploy
+  needs: ['pages']
   tags:
     - docker
-  rules:
-    - if: $CF_PURGE_CACHE_ZONE && $CF_PURGE_CACHE_TOKEN
   script:
     - >-
-      FILES="{ \"files\": [\"$CI_PAGES_URL/\"";
-      for d in $(find public/* -type d); do FILES="$FILES,\"$CI_PAGES_URL/${d#public/}/\""; done;
-      for f in $(find public -type f); do FILES="$FILES,\"$CI_PAGES_URL/${f#public/}\""; done;
-      FILES="$FILES] }"
-    - echo $FILES
-    - sleep 180s # Give pages:deploy time to finish so the old pages don't get re-cached
-    - >-
-      wget -qO- "https://api.cloudflare.com/client/v4/zones/$CF_PURGE_CACHE_ZONE/purge_cache"
-      --header "Content-Type: application/json"
-      --header "Authorization: Bearer $CF_PURGE_CACHE_TOKEN"
-      --post-data "$FILES"
+      echo -en "{\n  \"files\": [\n    \"$CI_PAGES_URL/\"" > purge.json;
+      for d in $(find public/* -type d); do echo -en ",\n    \"$CI_PAGES_URL/${d#public/}/\"" >> purge.json; done;
+      for f in $(find public -type f); do echo -en ",\n    \"$CI_PAGES_URL/${f#public/}\"" >> purge.json; done;
+      echo -e "\n  ]\n}" >> purge.json
+    - cat purge.json
+  artifacts:
+    paths:
+      - purge.json
 
+trigger-cache-purge:
+  stage: .post
+  trigger:
+    include: purge-cache.gitlab-ci.yml
+  variables:
+    PARENT_PIPELINE_ID: $CI_PIPELINE_ID
 
 
 # vi: set ts=2 sw=2 et ft=yaml:
diff --git a/purge-cache.gitlab-ci.yml b/purge-cache.gitlab-ci.yml
new file mode 100644
index 0000000..cfe4a24
--- /dev/null
+++ b/purge-cache.gitlab-ci.yml
@@ -0,0 +1,25 @@
+# Purge the Cloudflare cache using the request body contained in purge.json
+#
+# We delay this job to give the pages:deploy job time to finish. If we don't
+# delay, then the cache might refill with old pages before the new pages are
+# finished deploying.
+purge-cache:
+  tags:
+    - docker
+  rules:
+    - if: $CF_PURGE_CACHE_ZONE && $CF_PURGE_CACHE_TOKEN
+      when: delayed
+      start_in: 3 minutes
+  needs:
+    - pipeline: $PARENT_PIPELINE_ID
+      job: create-purge-json
+  script:
+    - cat purge.json
+#    - >-
+#      wget -qO- "https://api.cloudflare.com/client/v4/zones/$CF_PURGE_CACHE_ZONE/purge_cache"
+#      --header "Content-Type: application/json"
+#      --header "Authorization: Bearer $CF_PURGE_CACHE_TOKEN"
+#      --post-file purge.json
+
+
+# vi: set ts=2 sw=2 et ft=yaml:
-- 
GitLab