Skip to content

Commit 1df3c62

Browse files
feat: purge kv job (#338)
1 parent e8357d0 commit 1df3c62

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

Dockerfile-deploy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ ENV PNPM_HOME="/pnpm"
33
ENV PATH="$PNPM_HOME:$PATH"
44
RUN corepack enable
55

6+
RUN apt-get update && apt-get install -y jq
7+
68
COPY . /app
79
WORKDIR /app

Jenkinsfile.purge_cache

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
pipeline {
2+
agent any
3+
4+
environment {
5+
CLOUDFLARE_ZONE_ID = credentials('cloudflare-angular-love-zone-id')
6+
}
7+
8+
stages {
9+
stage('Purge Cache') {
10+
steps {
11+
withCredentials([
12+
usernamePassword(credentialsId: 'cf-workers-creds', usernameVariable: 'CLOUDFLARE_ACCOUNT_ID', passwordVariable: 'CLOUDFLARE_API_TOKEN'),
13+
]) {
14+
script {
15+
def response = sh(script: """
16+
curl -X POST "https://api.cloudflare.com/client/v4/zones/${env.CLOUDFLARE_ZONE_ID}/purge_cache" \
17+
-H "Authorization: Bearer ${env.CLOUDFLARE_API_TOKEN}" \
18+
-H "Content-Type: application/json" \
19+
-d '{"purge_everything": true}'
20+
""", returnStdout: true)
21+
22+
// Check if purge was successful
23+
def jsonResponse = readJSON text: response
24+
if (!jsonResponse.success) {
25+
error "Failed to purge Cloudflare cache: ${jsonResponse.errors}"
26+
}
27+
echo "Successfully purged Cloudflare cache"
28+
}
29+
}
30+
}
31+
}
32+
}
33+
34+
post {
35+
failure {
36+
echo "Failed to purge Cloudflare cache"
37+
}
38+
}
39+
}

Jenkinsfile.purge_kv

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
pipeline {
2+
agent {
3+
dockerfile {
4+
filename 'Dockerfile-deploy'
5+
reuseNode true
6+
}
7+
}
8+
9+
options {
10+
quietPeriod(0)
11+
timeout(time: 1, unit: 'HOURS')
12+
timestamps()
13+
buildDiscarder(logRotator(numToKeepStr: '20'))
14+
disableConcurrentBuilds()
15+
}
16+
17+
parameters {
18+
booleanParam(name: 'RUN_ON_DEV', defaultValue: false, description: 'Run the purge on the development environment')
19+
booleanParam(name: 'RUN_ON_PROD', defaultValue: false, description: 'Run the purge on the production environment')
20+
}
21+
22+
environment {
23+
KV_DEV = credentials('cf-kv-dev')
24+
KV_PROD = credentials('cf-kv-prod')
25+
}
26+
27+
stages {
28+
stage('Run purge dev') {
29+
when {
30+
expression { params.RUN_ON_DEV }
31+
}
32+
steps {
33+
withCredentials([
34+
usernamePassword(credentialsId: 'cf-workers-creds', usernameVariable: 'CLOUDFLARE_ACCOUNT_ID', passwordVariable: 'CLOUDFLARE_API_TOKEN'),
35+
]) {
36+
sh """
37+
./purge_kv.sh $KV_DEV
38+
"""
39+
}
40+
}
41+
}
42+
stage('Run purge prod') {
43+
when {
44+
expression { params.RUN_ON_PROD }
45+
}
46+
steps {
47+
withCredentials([
48+
usernamePassword(credentialsId: 'cf-workers-creds', usernameVariable: 'CLOUDFLARE_ACCOUNT_ID', passwordVariable: 'CLOUDFLARE_API_TOKEN'),
49+
]) {
50+
sh """
51+
./purge_kv.sh $KV_PROD
52+
"""
53+
}
54+
}
55+
}
56+
}
57+
58+
post {
59+
always {
60+
cleanWs()
61+
}
62+
}
63+
}

purge_kv.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
# Help function
4+
show_help() {
5+
echo "Usage: ./purge_kv.sh <namespace_id>"
6+
echo
7+
echo "Note: Make sure you're logged in with 'wrangler login' first"
8+
exit 1
9+
}
10+
11+
12+
if [ -z "$1" ]; then
13+
show_help
14+
fi
15+
16+
NAMESPACE_ID=$1
17+
TEMP_FILE="keys_to_delete.json"
18+
19+
echo "Listing all keys from namespace: $NAMESPACE_ID"
20+
21+
npx wrangler kv key list --namespace-id="$NAMESPACE_ID" | \
22+
jq -r '.[].name' | \
23+
jq -R -s 'split("\n") | map(select(length > 0))' > "$TEMP_FILE"
24+
25+
# Check if we got any keys
26+
if [ ! -s "$TEMP_FILE" ]; then
27+
echo "No keys found in namespace"
28+
rm "$TEMP_FILE"
29+
exit 0
30+
fi
31+
32+
echo "Created list of keys to delete"
33+
echo "Performing bulk delete..."
34+
35+
yes | npx wrangler kv bulk delete --namespace-id="$NAMESPACE_ID" "$TEMP_FILE"
36+
37+
# Clean up
38+
rm "$TEMP_FILE"
39+
40+
echo "Bulk delete complete!"

0 commit comments

Comments
 (0)