Repository navigation
Expand file tree
/
Copy pathJenkinsfile.sync
More file actions
128 lines (124 loc) · 5.04 KB
/
Copy pathJenkinsfile.sync
File metadata and controls
128 lines (124 loc) · 5.04 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
// Triggered by the WordPress webhook (via webhookrelay). Syncs a single
// post/author to the blog database (EU), or purges the banner cache.
pipeline {
agent {
dockerfile {
filename 'Dockerfile-deploy'
reuseNode true
}
}
options {
quietPeriod(0)
disableConcurrentBuilds()
timestamps()
}
triggers {
GenericTrigger(
genericVariables: [
[key: 'post_id', value: '$.post_id'],
[key: 'post_type', value: '$.post.post_type'],
[key: 'user_id', value: '$.data.ID'],
],
genericHeaderVariables: [
[key: 'X-Wp-Webhook-Name', regexpFilter: ''],
],
tokenCredentialId: 'blog-jobs-sync-webhook-token',
printContributedVariables: true,
printPostContent: false
)
}
environment {
AL_BASE_URL = 'https://angular.love'
// The sync mints its own short-lived write token: the worker's tokens are read-only.
TURSO_ORG = 'angular-love'
TURSO_DB_NAME = credentials('turso-eu-db')
WP_REST_API_BASE_URL = 'https://wp.angular.love'
WP_REST_API_TOKEN = credentials('al-wp-token')
CACHE_PURGE_SECRET = credentials('al_cache_purge_secret')
SLACK_CHANNEL = '#angular-love-db-sync'
}
stages {
stage('Extract data') {
steps {
script {
// Branch indexing and manual runs carry no webhook payload: nothing to sync.
// NOT_BUILT skips the remaining stages and the Slack notifications.
if (!env.X_Wp_Webhook_Name) {
echo 'Not triggered by the WordPress webhook, nothing to do'
env.RESOURCE = 'none'
currentBuild.result = 'NOT_BUILT'
return
}
def isBanner = env.post_type == 'banner'
def events = [
post_create: [resource: isBanner ? 'banner' : 'post', action: 'create', id: env.post_id],
post_update: [resource: isBanner ? 'banner' : 'post', action: 'update', id: env.post_id],
post_delete: [resource: isBanner ? 'banner' : 'post', action: 'delete', id: env.post_id],
create_user: [resource: 'author', action: 'create', id: env.user_id],
update_user: [resource: 'author', action: 'update', id: env.user_id],
deleted_user: [resource: 'author', action: 'delete', id: env.user_id],
]
def event = events[env.X_Wp_Webhook_Name]
if (!event) {
error("Unknown webhook: ${env.X_Wp_Webhook_Name}")
}
if (!(event.id ==~ /\d+/)) {
error("Invalid id: ${event.id}")
}
env.RESOURCE = event.resource
env.ACTION = event.action
env.ID = event.id
echo "resource: ${env.RESOURCE}, action: ${env.ACTION}, id: ${env.ID}"
}
}
}
stage('Install dependencies') {
when { expression { env.RESOURCE in ['post', 'author'] } }
steps {
sh '''
corepack enable && corepack prepare pnpm@10.6.2 --activate
pnpm install --frozen-lockfile
'''
}
}
stage('Sync database') {
when { expression { env.RESOURCE in ['post', 'author'] } }
steps {
withCredentials([string(credentialsId: 'tursor_api_token', variable: 'TURSO_API_TOKEN')]) {
sh 'npx nx run api-jobs:sync --no-tui --resource="$RESOURCE" --id="$ID" --action="$ACTION"'
}
}
}
stage('Purge banner cache') {
when { expression { env.RESOURCE == 'banner' } }
steps {
sh '''
curl --fail-with-body -sS -X POST \
-H "x-purge-secret: $CACHE_PURGE_SECRET" \
"$AL_BASE_URL/api/banners/invalidate"
'''
}
}
}
post {
always {
cleanWs()
}
failure {
script {
def msg = env.RESOURCE == 'banner'
? "<!here> Banner cache purge failed, post_id: ${env.ID}, build url: ${BUILD_URL}"
: "<!here> DB sync failed, ${env.RESOURCE} ${env.ID} ${env.ACTION}, build url: ${BUILD_URL}"
slackSend(channel: env.SLACK_CHANNEL, message: msg)
}
}
success {
script {
def msg = env.RESOURCE == 'banner'
? 'Banner cache purged successfully'
: "DB sync succeeded, ${env.RESOURCE} ${env.ID} ${env.ACTION}"
slackSend(channel: env.SLACK_CHANNEL, message: msg)
}
}
}
}