Skip to content

Commit e92123d

Browse files
authored
feat: cloudflare workers webhook (#5)
1 parent b3ed6a2 commit e92123d

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Deploy Cloudflare Worker
2+
3+
on:
4+
push:
5+
branches: [main]
6+
workflow_dispatch:
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
name: Deploy to Cloudflare Workers
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Setup Node.js
18+
uses: actions/setup-node@v4
19+
with:
20+
node-version: '20'
21+
cache: 'npm'
22+
23+
- name: Install Wrangler CLI
24+
run: npm install -g wrangler
25+
26+
- name: Set up Worker secrets
27+
env:
28+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
29+
CIRCLECI_WEBHOOK_URL: ${{ secrets.CIRCLECI_WEBHOOK_URL }}
30+
run: |
31+
if [ -n "$CIRCLECI_WEBHOOK_URL" ]; then
32+
echo '$CIRCLECI_WEBHOOK_URL' | wrangler secret put CIRCLECI_WEBHOOK_URL
33+
else
34+
echo "CIRCLECI_WEBHOOK_URL not provided!"
35+
exit 1
36+
fi
37+
38+
- name: Deploy to Cloudflare Workers
39+
run: wrangler deploy
40+
env:
41+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}

worker.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export default {
2+
async scheduled(event, env, ctx) {
3+
try {
4+
const WEBHOOK_URL = env.CIRCLECI_WEBHOOK_URL;
5+
const response = await fetch(WEBHOOK_URL, {
6+
method: 'POST',
7+
headers: {
8+
'Content-Type': 'application/json',
9+
},
10+
});
11+
12+
const timestamp = new Date().toISOString();
13+
console.log(`[${timestamp}] Scheduled request executed`);
14+
console.log(`Response status: ${response.status}`);
15+
16+
if (!response.ok) {
17+
const errorText = await response.text();
18+
console.error(`HTTP error! status: ${response.status}, body: ${errorText}`);
19+
} else {
20+
const result = await response.text();
21+
console.log('Response body:', result);
22+
}
23+
24+
} catch (error) {
25+
console.error('Scheduled request failed:', error);
26+
}
27+
},
28+
29+
// async fetch(request, env, ctx) {
30+
// // Test endpoint
31+
// if (request.url.includes('/test')) {
32+
// try {
33+
// const WEBHOOK_URL = env.CIRCLECI_WEBHOOK_URL;
34+
// const response = await fetch(WEBHOOK_URL, {
35+
// method: 'POST',
36+
// headers: {
37+
// 'Content-Type': 'application/json',
38+
// },
39+
// });
40+
41+
// const responseText = await response.text();
42+
43+
// return new Response(`Test request completed. Status: ${response.status}\nResponse: ${responseText}`, {
44+
// status: 200,
45+
// headers: { 'Content-Type': 'text/plain' }
46+
// });
47+
// } catch (error) {
48+
// return new Response(`Test failed: ${error.message}`, {
49+
// status: 500,
50+
// headers: { 'Content-Type': 'text/plain' }
51+
// });
52+
// }
53+
// }
54+
55+
// return new Response('CircleCI Scheduler Worker is running', {
56+
// status: 200,
57+
// headers: { 'Content-Type': 'text/plain' }
58+
// });
59+
// }
60+
};

wrangler.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name = "circleci-scheduler"
2+
main = "worker.js"
3+
compatibility_date = "2024-01-15"
4+
5+
[triggers]
6+
crons = ["0 2 * * *"] # UTC 2:00 = (Beijing) UTC+8 10:00

0 commit comments

Comments
 (0)