Skip to content

Commit 968176a

Browse files
committed
add workflow to main
1 parent f9eb23c commit 968176a

File tree

1 file changed

+83
-10
lines changed

1 file changed

+83
-10
lines changed

.github/workflows/pr-new-env-health-check.yml

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on:
1111

1212
permissions:
1313
contents: read
14-
pull-requests: read
14+
pull-requests: write
1515

1616
jobs:
1717
detect-new-envs:
@@ -138,34 +138,59 @@ jobs:
138138
run: |
139139
set -euo pipefail
140140
chmod +x scripts/deploy_to_hf.sh
141-
./scripts/deploy_to_hf.sh --env "${{ matrix.environment }}" --space-suffix "${SPACE_SUFFIX}" --private
141+
./scripts/deploy_to_hf.sh --env "${{ matrix.environment }}" --space-suffix "${SPACE_SUFFIX}"
142142
143143
- name: Wait for deployment to stabilize
144144
shell: bash
145-
run: sleep 300
145+
run: sleep 180
146146

147-
- name: Perform environment health check
147+
- name: Compute Space URLs
148+
id: urls
148149
shell: bash
149150
run: |
150151
set -euo pipefail
151152
152153
if [ -z "${HF_NAMESPACE:-}" ]; then
153-
echo "HF_NAMESPACE is not configured; unable to compute health check URL." >&2
154+
echo "HF_NAMESPACE is not configured; unable to compute space URLs." >&2
154155
exit 1
155156
fi
156157
157158
namespace_slug=$(echo "${HF_NAMESPACE}" | tr '[:upper:]' '[:lower:]' | tr '_' '-')
158159
space_name="${{ matrix.environment }}${SPACE_SUFFIX}"
159160
space_slug=$(echo "${space_name}" | tr '[:upper:]' '[:lower:]' | tr '_' '-')
160161
health_url="https://${namespace_slug}-${space_slug}.hf.space/health"
162+
live_url="https://${namespace_slug}-${space_slug}.hf.space"
163+
space_repo_url="https://huggingface.co/spaces/${HF_NAMESPACE}/${space_name}"
164+
165+
echo "namespace_slug=${namespace_slug}" >> "$GITHUB_OUTPUT"
166+
echo "space_name=${space_name}" >> "$GITHUB_OUTPUT"
167+
echo "space_slug=${space_slug}" >> "$GITHUB_OUTPUT"
168+
echo "health_url=${health_url}" >> "$GITHUB_OUTPUT"
169+
echo "live_url=${live_url}" >> "$GITHUB_OUTPUT"
170+
echo "space_repo_url=${space_repo_url}" >> "$GITHUB_OUTPUT"
171+
172+
- name: Perform environment health check
173+
id: health_check
174+
continue-on-error: true
175+
shell: bash
176+
env:
177+
HEALTH_URL: ${{ steps.urls.outputs.health_url }}
178+
SPACE_NAME: ${{ steps.urls.outputs.space_name }}
179+
run: |
180+
set -euo pipefail
181+
182+
if [ -z "${HEALTH_URL:-}" ]; then
183+
echo "HEALTH_URL not provided; cannot perform health check." >&2
184+
exit 1
185+
fi
161186
162-
echo "Checking health for ${space_name} at ${health_url}"
187+
echo "Checking health for ${SPACE_NAME} at ${HEALTH_URL}"
163188
164189
success=0
165190
for attempt in {1..5}; do
166-
status=$(curl -sS -o response.json -w "%{http_code}" "$health_url" || echo "000")
191+
status=$(curl -sS -o response.json -w "%{http_code}" "$HEALTH_URL" || echo "000")
167192
if [ "$status" = "200" ]; then
168-
echo "Health check passed for ${space_name}"
193+
echo "Health check passed for ${SPACE_NAME}"
169194
cat response.json
170195
success=1
171196
break
@@ -175,10 +200,58 @@ jobs:
175200
done
176201
177202
if [ $success -ne 1 ]; then
178-
echo "Health check failed for ${space_name}" >&2
203+
echo "Health check failed for ${SPACE_NAME}" >&2
179204
if [ -f response.json ]; then
180205
echo "Last response payload:"
181206
cat response.json
182207
fi
183208
exit 1
184-
fi
209+
fi
210+
211+
- name: Comment on PR with deployment status
212+
if: always()
213+
uses: actions/github-script@v7
214+
env:
215+
HEALTH_CONCLUSION: ${{ steps.health_check.conclusion }}
216+
SPACE_NAME: ${{ steps.urls.outputs.space_name }}
217+
LIVE_URL: ${{ steps.urls.outputs.live_url }}
218+
SPACE_REPO_URL: ${{ steps.urls.outputs.space_repo_url }}
219+
ENV_NAME: ${{ matrix.environment }}
220+
with:
221+
github-token: ${{ secrets.GITHUB_TOKEN }}
222+
script: |
223+
const status = process.env.HEALTH_CONCLUSION || 'failure';
224+
const spaceName = process.env.SPACE_NAME;
225+
const liveUrl = process.env.LIVE_URL;
226+
const repoUrl = process.env.SPACE_REPO_URL;
227+
const envName = process.env.ENV_NAME;
228+
229+
const header = status === 'success'
230+
? `✅ Deployment succeeded for \`${envName}\``
231+
: `⚠️ Deployment failed for \`${envName}\``;
232+
233+
const summary = status === 'success'
234+
? 'Nice work! Wait for a code review and we\'re ready to go.'
235+
: 'Please resolve your environment.';
236+
237+
const body = [
238+
header,
239+
'',
240+
`- Space repo: [${repoUrl}](${repoUrl})`,
241+
`- Live URL: [${liveUrl}](${liveUrl})`,
242+
'',
243+
summary,
244+
'',
245+
'You can iterate locally or validate fixes by running `scripts/deploy_to_hf.sh --env "' + envName + '"`.'
246+
].join('\n');
247+
248+
await github.rest.issues.createComment({
249+
owner: context.repo.owner,
250+
repo: context.repo.repo,
251+
issue_number: context.payload.pull_request.number,
252+
body
253+
});
254+
255+
- name: Fail job if health check failed
256+
if: steps.health_check.conclusion == 'failure'
257+
run: exit 1

0 commit comments

Comments
 (0)