-
Notifications
You must be signed in to change notification settings - Fork 4
158 lines (141 loc) · 5.76 KB
/
sync-worker-cors.yml
File metadata and controls
158 lines (141 loc) · 5.76 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
name: Sync Worker CORS
on:
push:
branches: [main, develop]
paths:
- 'src/assistants/*/config.yaml'
- 'scripts/sync_worker_cors.py'
- '.github/workflows/sync-worker-cors.yml'
- 'workers/osa-worker/**'
pull_request:
branches: [main, develop]
paths:
- 'src/assistants/*/config.yaml'
- 'workers/osa-worker/**'
workflow_dispatch:
permissions:
contents: write
pull-requests: write
jobs:
sync-cors:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.CI_ADMIN_TOKEN }}
fetch-depth: 0 # Fetch full history so git diff works
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install dependencies
run: |
pip install pyyaml
- name: Run CORS sync script
run: |
python scripts/sync_worker_cors.py
- name: Check for changes
id: check_changes
run: |
# Check if CORS sync made changes
if git diff --quiet workers/osa-worker/index.js; then
echo "No CORS changes detected"
CORS_CHANGED=false
else
echo "CORS changes detected"
CORS_CHANGED=true
fi
# Check if worker files were modified in the push that triggered this workflow
# Use the range from the push event (before -> after)
if [ "${{ github.event.before }}" != "0000000000000000000000000000000000000000" ]; then
# Normal push (not first commit)
if git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep -q "^workers/osa-worker/"; then
echo "Worker files changed in push"
WORKER_CHANGED=true
else
echo "No worker files in push"
WORKER_CHANGED=false
fi
else
# First commit to branch, check current files
if git ls-files workers/osa-worker/ | grep -q .; then
echo "Worker files exist (first commit)"
WORKER_CHANGED=true
else
WORKER_CHANGED=false
fi
fi
# Set outputs
echo "cors_changed=$CORS_CHANGED" >> $GITHUB_OUTPUT
# Deploy if either CORS sync changed files OR worker files were pushed
if [ "$CORS_CHANGED" = true ] || [ "$WORKER_CHANGED" = true ]; then
echo "changed=true" >> $GITHUB_OUTPUT
echo "✅ Deployment needed"
else
echo "changed=false" >> $GITHUB_OUTPUT
echo "No deployment needed"
fi
- name: Commit CORS changes (main/develop only)
if: steps.check_changes.outputs.cors_changed == 'true' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop') && github.event_name == 'push'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add workers/osa-worker/index.js
git commit -m "chore: sync worker CORS from community configs [skip ci]"
git pull --rebase origin ${{ github.ref_name }}
git push
- name: Deploy to Cloudflare Workers (production)
if: steps.check_changes.outputs.changed == 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: |
npm install -g wrangler
cd workers/osa-worker
wrangler deploy
echo "✅ Deployed to production worker"
- name: Deploy to Cloudflare Workers (dev)
if: steps.check_changes.outputs.changed == 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: |
npm install -g wrangler
cd workers/osa-worker
wrangler deploy --env=dev
echo "✅ Deployed to dev worker"
- name: Comment on PR
if: steps.check_changes.outputs.changed == 'true' && github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
let message = '<!-- worker-cors-sync -->\n';
message += '⚠️ **Worker Deployment Required**\n\n';
if ('${{ steps.check_changes.outputs.cors_changed }}' === 'true') {
message += 'This PR modifies community CORS origins. ';
}
message += 'Worker changes detected. After merging to `main` or `develop`, the workflow will automatically deploy the worker.\n\n';
message += '**Manual deployment (if needed):**\n```bash\ncd workers/osa-worker\nwrangler deploy --env dev # for develop branch\nwrangler deploy # for main branch\n```';
// Find existing comment from this workflow
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existing = comments.find(c =>
c.user.type === 'Bot' &&
c.body.includes('<!-- worker-cors-sync -->')
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: message
});
} else {
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: message
});
}