-
Notifications
You must be signed in to change notification settings - Fork 0
165 lines (138 loc) · 5.49 KB
/
index-after-pages.yml
File metadata and controls
165 lines (138 loc) · 5.49 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
name: Index docs after Cloudflare Pages deploy
on:
push:
branches: [ main ]
workflow_dispatch: {}
concurrency:
group: pages-index-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
jobs:
wait-and-index:
runs-on: ubuntu-latest
env:
# Cloudflare
CF_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
CF_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
CF_PAGES_PROJECT_NAME: ${{ secrets.CF_PAGES_PROJECT_NAME }}
SITE_BASE: ${{ secrets.SITE_BASE }}
# Your indexer config
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
PINECONE_API_KEY: ${{ secrets.PINECONE_API_KEY }}
PINECONE_INDEX: ${{ secrets.PINECONE_INDEX }}
PINECONE_REGION: ${{ secrets.PINECONE_REGION }}
PINECONE_CLOUD: ${{ secrets.PINECONE_CLOUD }}
# Used to match the deployment to this commit
GIT_SHA: ${{ github.sha }}
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Install jq (for JSON parsing)
run: |
sudo apt-get update
sudo apt-get install -y jq
# 🔎 Find the previous successful run of THIS workflow on main
- name: Find previous successful run id
id: prev
shell: bash
env:
GH_REPO: ${{ github.repository }}
GH_TOKEN: ${{ github.token }}
CUR_RUN_NUMBER: ${{ github.run_number }}
run: |
set -euo pipefail
# Use the workflow file name to query runs
WF_FILE=".github/workflows/index-after-pages.yml"
API="https://api.github.com/repos/${GH_REPO}/actions/workflows/$(basename "$WF_FILE")/runs?branch=main&status=success&per_page=50"
resp="$(curl -fsSL -H "Authorization: Bearer ${GH_TOKEN}" "$API")"
# Pick the latest successful run with run_number < current
prev_run_id=$(echo "$resp" \
| jq -r --argjson cur "${CUR_RUN_NUMBER}" '
.workflow_runs
| map(select(.run_number < $cur))
| sort_by(.run_number) | last // empty
| .id // empty')
if [[ -n "$prev_run_id" && "$prev_run_id" != "null" ]]; then
echo "Previous run id: $prev_run_id"
echo "run_id=$prev_run_id" >> "$GITHUB_OUTPUT"
else
echo "No previous successful run found."
echo "run_id=" >> "$GITHUB_OUTPUT"
fi
# ⬇️ Download pinecone-state from that previous run (if it exists)
- name: Download pinecone-state artifact from previous run
if: steps.prev.outputs.run_id != ''
uses: actions/download-artifact@v4
with:
name: pinecone-state
path: pinecone-state
run-id: ${{ steps.prev.outputs.run_id }}
github-token: ${{ github.token }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
# if your repo has a requirements.txt, install it:
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Wait for Cloudflare Pages to deploy this commit
shell: bash
run: |
set -euo pipefail
API="https://api.cloudflare.com/client/v4/accounts/${CF_ACCOUNT_ID}/pages/projects/${CF_PAGES_PROJECT_NAME}/deployments"
echo "Waiting for CF Pages deployment of commit ${GIT_SHA}…"
attempt=0
max_attempts=60 # ~10 minutes total at 10s intervals
sleep_sec=10
while (( attempt < max_attempts )); do
attempt=$((attempt+1))
resp="$(curl -fsS -G \
-H "Authorization: Bearer ${CF_API_TOKEN}" \
-H "Content-Type: application/json" \
--data-urlencode "per_page=20" \
"${API}")"
status=$(echo "$resp" \
| jq -r --arg sha "$GIT_SHA" '
.result
| map(select(.deployment_trigger.metadata.commit_hash==$sha))
| first
| if .==null then "notfound" else .latest_stage.status end')
url=$(echo "$resp" \
| jq -r --arg sha "$GIT_SHA" '
.result
| map(select(.deployment_trigger.metadata.commit_hash==$sha))
| first
| if .==null then "" else .url end')
echo "Attempt ${attempt}: status=${status} ${url:+url=${url}}"
if [[ "$status" == "success" ]]; then
echo "✅ Deployed at: ${url}"
break
elif [[ "$status" == "failure" ]]; then
echo "❌ Cloudflare Pages marked this deployment as failed."
exit 1
fi
sleep "$sleep_sec"
done
if (( attempt == max_attempts )); then
echo "Timed out waiting for Cloudflare Pages to deploy commit ${GIT_SHA}"
exit 1
fi
# 👉 Run the script with the persisted state dir
- name: Run sitemap_to_pinecone.py (stateful)
run: |
python scripts/sitemap_to_pinecone.py \
"${SITE_BASE%/}/sitemap.xml" \
--state-dir pinecone-state
# ⬆️ Re-upload the updated state for the next run
- name: Upload pinecone-state artifact
if: always()
uses: actions/upload-artifact@v4
with:
name: pinecone-state
path: pinecone-state
if-no-files-found: warn
retention-days: 90