forked from cloudflare/cloudflared
-
Notifications
You must be signed in to change notification settings - Fork 3
358 lines (311 loc) · 11.5 KB
/
Copy pathupdate-cloudflared.yml
File metadata and controls
358 lines (311 loc) · 11.5 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
name: New cloudflared Release
permissions:
contents: write
pull-requests: write
actions: write
on:
schedule:
- cron: '0 */12 * * *'
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: true
jobs:
prepare:
runs-on: ubuntu-latest
outputs:
needs_update: ${{ steps.update_guard.outputs.needs_update }}
tag: ${{ steps.update_guard.outputs.tag }}
title: ${{ steps.update_guard.outputs.title }}
release_id: ${{ steps.draft_release.outputs.release_id }}
steps:
- name: Get latest upstream release info
id: upstream_release
uses: actions/github-script@v8
with:
script: |
const { data } = await github.rest.repos.getLatestRelease({
owner: 'cloudflare',
repo: 'cloudflared'
});
core.setOutput('tag_name', data.tag_name);
core.setOutput('name', data.name ?? data.tag_name);
- name: Get latest release in this repo (if any)
id: current_release
uses: actions/github-script@v8
with:
script: |
try {
const { data } = await github.rest.repos.getLatestRelease({
owner: context.repo.owner,
repo: context.repo.repo
});
core.setOutput('tag_name', data.tag_name);
} catch (error) {
if (error.status === 404) {
core.info('No existing release found in this repository.');
} else {
throw error;
}
}
- name: Decide whether an update is required
id: update_guard
env:
UPSTREAM_TAG: ${{ steps.upstream_release.outputs.tag_name }}
UPSTREAM_TITLE: ${{ steps.upstream_release.outputs.name }}
CURRENT_TAG: ${{ steps.current_release.outputs.tag_name }}
run: |
if [ -z "${UPSTREAM_TAG}" ]; then
echo "Failed to determine upstream tag" >&2
exit 1
fi
if [ "${UPSTREAM_TAG}" = "${CURRENT_TAG}" ]; then
echo 'needs_update=false' >> "$GITHUB_OUTPUT"
exit 0
fi
echo "needs_update=true" >> "$GITHUB_OUTPUT"
echo "tag=${UPSTREAM_TAG}" >> "$GITHUB_OUTPUT"
{
echo 'title<<EOF'
echo "${UPSTREAM_TITLE}"
echo 'EOF'
} >> "$GITHUB_OUTPUT"
- name: Checkout customizations branch
if: steps.update_guard.outputs.needs_update == 'true'
uses: actions/checkout@v6
with:
ref: customizations
fetch-depth: 0
- name: Prepare release branch
if: steps.update_guard.outputs.needs_update == 'true'
env:
TAG: ${{ steps.update_guard.outputs.tag }}
run: |
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
branch="release-${TAG}"
# 1) Add upstream and fetch the tag
git remote add upstream https://github.com/cloudflare/cloudflared.git
git fetch upstream --depth=1 tag "${TAG}"
# 2) Create new branch off the upstream tag
git checkout -b "${branch}" "FETCH_HEAD"
# 3) Merge in your customizations
git fetch origin customizations
git merge --no-commit --no-ff origin/customizations || true
# 4) Re-apply BSD-portability file overlays (these shims rarely drift in upstream)
rm -rf .github/workflows
git checkout origin/customizations -- \
cmd/cloudflared/generic_service.go \
diagnostic/network/collector_unix_test.go \
diagnostic/network/collector_unix.go \
diagnostic/system_collector_unix.go \
ingress/icmp_posix.go \
ingress/icmp_posix_test.go \
Makefile \
patches
# 5) Reset QUIC-related files to pristine upstream — the customizations
# branch still carries stale full-file copies of these from an older
# upstream base, which the step-3 merge leaves with conflict markers.
# The .patch in step 6 replaces them with the surgical delta we care about.
# (connection/errors_test.go from customizations is added net-new and
# has no upstream counterpart; we leave it where step 3 placed it.)
git checkout "refs/tags/${TAG}" -- \
connection/errors.go \
connection/quic_connection.go \
supervisor/tunnel.go \
supervisor/tunnel_test.go
# 6) Apply surgical patches for QUIC behavior changes.
# Patches express *intent* and survive surrounding upstream churn.
# If a patch fails to apply, this fails the workflow loudly so a
# human can re-derive the patch against the new upstream state.
for patch in patches/*.patch; do
echo "→ Applying ${patch}"
git apply --3way "${patch}"
done
# 7) Finalise and push
git add -A
git commit -m "chore: apply customizations on ${TAG}"
git push --force-with-lease origin "${branch}"
- name: Create or update draft release
id: draft_release
if: steps.update_guard.outputs.needs_update == 'true'
uses: actions/github-script@v8
env:
TAG: ${{ steps.update_guard.outputs.tag }}
TITLE: ${{ steps.update_guard.outputs.title }}
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const tag = process.env.TAG;
const title = process.env.TITLE || tag;
const targetCommitish = `release-${tag}`;
const body = [
`Automated cloudflared release of version **${tag}**`,
'Stock upstream cloudflared with BSD tweaks from `customizations` branch in this repo.'
].join('\n');
const findReleaseByTag = async () => {
let page = 1;
while (true) {
const { data } = await github.rest.repos.listReleases({
owner,
repo,
per_page: 100,
page
});
const match = data.find((release) => release.tag_name === tag);
if (match) {
return match;
}
if (data.length < 100) {
return null;
}
page += 1;
}
};
const existing = await findReleaseByTag();
if (existing) {
await github.rest.repos.updateRelease({
owner,
repo,
release_id: existing.id,
tag_name: tag,
target_commitish: targetCommitish,
name: title,
body,
draft: true,
prerelease: false
});
core.setOutput('release_id', String(existing.id));
core.info(`Updated existing draft release for ${tag}.`);
return;
}
const { data: created } = await github.rest.repos.createRelease({
owner,
repo,
tag_name: tag,
target_commitish: targetCommitish,
name: title,
body,
draft: true,
prerelease: false
});
core.setOutput('release_id', String(created.id));
core.info(`Created draft release for ${tag}.`);
build_native:
if: needs.prepare.outputs.needs_update == 'true'
needs: prepare
permissions:
contents: write
actions: write
uses: ./.github/workflows/build_unix.yaml
with:
tag: ${{ needs.prepare.outputs.tag }}
secrets: inherit
publish_release:
if: needs.prepare.outputs.needs_update == 'true' && needs.build_native.result == 'success'
needs:
- prepare
- build_native
runs-on: ubuntu-latest
steps:
- name: Publish draft release
uses: actions/github-script@v8
env:
TAG: ${{ needs.prepare.outputs.tag }}
TITLE: ${{ needs.prepare.outputs.title }}
RELEASE_ID: ${{ needs.prepare.outputs.release_id }}
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const tag = process.env.TAG;
const title = process.env.TITLE || tag;
const releaseId = Number(process.env.RELEASE_ID);
if (!releaseId) {
throw new Error(`Missing release ID for tag ${tag}.`);
}
const { data: release } = await github.rest.repos.getRelease({
owner,
repo,
release_id: releaseId
});
if (release.tag_name !== tag) {
throw new Error(`Release ${releaseId} belongs to tag ${release.tag_name}, expected ${tag}.`);
}
if (!release.draft) {
core.info(`Release ${tag} is already published.`);
return;
}
await github.rest.repos.updateRelease({
owner,
repo,
release_id: releaseId,
name: title,
draft: false,
prerelease: false
});
core.info(`Published release ${tag}.`);
- name: Prune older successful runs
if: success()
uses: actions/github-script@v8
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const keepSuccessfulRuns = 7;
const workflowRef = process.env.GITHUB_WORKFLOW_REF || '';
const workflowFile = workflowRef
? workflowRef.split('@')[0].split('/').pop()
: 'update-cloudflared.yml';
let kept = 0;
let page = 1;
let permissionDenied = false;
while (true) {
const { data } = await github.rest.actions.listWorkflowRuns({
owner,
repo,
workflow_id: workflowFile,
status: 'completed',
per_page: 100,
page
});
const runs = data.workflow_runs;
if (!runs.length) {
break;
}
for (const run of runs) {
if (permissionDenied) {
break;
}
if (run.conclusion !== 'success') {
continue;
}
kept += 1;
if (kept <= keepSuccessfulRuns) {
continue;
}
core.info(`Deleting run ${run.id} from ${run.created_at}`);
try {
await github.rest.actions.deleteWorkflowRun({
owner,
repo,
run_id: run.id
});
} catch (error) {
if (error.status === 403) {
core.warning('GITHUB_TOKEN lacks permission to delete workflow runs; skipping further pruning.');
permissionDenied = true;
break;
}
throw error;
}
}
if (permissionDenied) {
break;
}
if (runs.length < 100) {
break;
}
page += 1;
}