@@ -103,6 +103,7 @@ jobs:
103103 environment : ${{ fromJSON(needs.detect-new-envs.outputs.new_envs_json) }}
104104 env :
105105 HF_TOKEN : ${{ secrets.HF_PR_TOKEN }}
106+ HF_PR_TOKEN : ${{ secrets.HF_PR_TOKEN }}
106107 HF_NAMESPACE : ${{ vars.HF_PR_NAMESPACE }}
107108 SPACE_SUFFIX : -pr-${{ github.event.number }}
108109 steps :
@@ -127,18 +128,31 @@ jobs:
127128 exit 1
128129 fi
129130
130- - name : Install Hugging Face CLI
131+ - name : Set up Python
132+ uses : actions/setup-python@v5
133+ with :
134+ python-version : ' 3.11'
135+
136+ - name : Install OpenEnv package
131137 shell : bash
132138 run : |
133- curl -LsSf https://hf.co/cli/install.sh | bash
134- echo "$HOME/.local/bin" >> "$GITHUB_PATH"
139+ set -euo pipefail
140+ python -m pip install --upgrade pip
141+ pip install .
135142
136- - name : Deploy environment to Hugging Face
143+ - name : Deploy environment with OpenEnv CLI
137144 shell : bash
138145 run : |
139146 set -euo pipefail
140- chmod +x scripts/deploy_to_hf.sh
141- ./scripts/deploy_to_hf.sh --env "${{ matrix.environment }}" --space-suffix "${SPACE_SUFFIX}" --hub-tag "openenv-pr"
147+ repo_id="${HF_NAMESPACE}/${{ matrix.environment }}${SPACE_SUFFIX}"
148+ env_dir="src/envs/${{ matrix.environment }}"
149+
150+ if [ ! -d "$env_dir" ]; then
151+ echo "Environment directory not found: $env_dir" >&2
152+ exit 1
153+ fi
154+
155+ openenv push --directory "$env_dir" --repo-id "$repo_id"
142156
143157 - name : Wait for deployment to stabilize
144158 shell : bash
@@ -161,13 +175,15 @@ jobs:
161175 health_url="https://${namespace_slug}-${space_slug}.hf.space/health"
162176 live_url="https://${namespace_slug}-${space_slug}.hf.space"
163177 space_repo_url="https://huggingface.co/spaces/${HF_NAMESPACE}/${space_name}"
178+ space_repo_id="${HF_NAMESPACE}/${space_name}"
164179
165180 echo "namespace_slug=${namespace_slug}" >> "$GITHUB_OUTPUT"
166181 echo "space_name=${space_name}" >> "$GITHUB_OUTPUT"
167182 echo "space_slug=${space_slug}" >> "$GITHUB_OUTPUT"
168183 echo "health_url=${health_url}" >> "$GITHUB_OUTPUT"
169184 echo "live_url=${live_url}" >> "$GITHUB_OUTPUT"
170185 echo "space_repo_url=${space_repo_url}" >> "$GITHUB_OUTPUT"
186+ echo "space_repo_id=${space_repo_id}" >> "$GITHUB_OUTPUT"
171187
172188 - name : Perform environment health check
173189 id : health_check
@@ -216,41 +232,108 @@ jobs:
216232 SPACE_NAME : ${{ steps.urls.outputs.space_name }}
217233 LIVE_URL : ${{ steps.urls.outputs.live_url }}
218234 SPACE_REPO_URL : ${{ steps.urls.outputs.space_repo_url }}
235+ SPACE_REPO_ID : ${{ steps.urls.outputs.space_repo_id }}
219236 ENV_NAME : ${{ matrix.environment }}
237+ COMMENT_TAG : " <!-- openenv-pr-preview -->"
220238 with :
221239 github-token : ${{ secrets.GITHUB_TOKEN }}
222240 script : |
223241 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;
227242 const envName = process.env.ENV_NAME;
243+ const marker = process.env.COMMENT_TAG;
228244
229245 const header = status === 'success'
230- ? `✅ Deployment succeeded for \`${envName}\``
231- : `⚠️ Deployment failed for \`${envName}\``;
246+ ? `✅ Deployment to Hugging Face succeeded for \`${envName}\``
247+ : `⚠️ Deployment Hugging Face failed for \`${envName}\``;
232248
233249 const summary = status === 'success'
234- ? 'Nice work! Wait for a code review and we\'re ready to go.'
235- : 'Please resolve your environment.';
250+ ? 'Nice work! Wait for a code review and we\'re ready to go. You can test it with the CLI:'
251+ : 'Please resolve your environment and test it with the CLI:';
252+
253+ const envDir = 'src/envs/' + envName;
236254
237- const body = [
255+ const bodyLines = [
256+ marker,
257+ '',
238258 header,
239259 '',
240- `- Space repo: [${repoUrl}](${repoUrl})`,
241- `- Live URL: [${liveUrl}](${liveUrl})`,
242260 '',
243261 summary,
244262 '',
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- });
263+ '- `openenv push --directory ' + envDir + ' --repo-id <your-username>/' + envName + '`',
264+ ];
265+
266+ const {owner, repo} = context.repo;
267+ const issue_number = context.payload.pull_request.number;
268+ const serverUrl = process.env.GITHUB_SERVER_URL || 'https://github.com';
269+ const runUrl = `${serverUrl}/${owner}/${repo}/actions/runs/${context.runId}`;
270+
271+ if (status !== 'success') {
272+ bodyLines.push(`- Failed run: ${runUrl}`);
273+ } else {
274+ bodyLines.push(`- Success run: ${runUrl}`);
275+ }
276+
277+ const bodyText = bodyLines.join('\n');
278+
279+ const existing = await github.paginate(
280+ github.rest.issues.listComments,
281+ { owner, repo, issue_number, per_page: 100 },
282+ (response, done) => {
283+ const match = response.data.find(comment => comment.body && comment.body.includes(marker));
284+ if (match) {
285+ done();
286+ return [match];
287+ }
288+ return [];
289+ }
290+ );
291+
292+ if (existing.length > 0) {
293+ await github.rest.issues.updateComment({
294+ owner,
295+ repo,
296+ comment_id: existing[0].id,
297+ body: bodyText,
298+ });
299+ } else {
300+ await github.rest.issues.createComment({
301+ owner,
302+ repo,
303+ issue_number,
304+ body: bodyText,
305+ });
306+ }
307+
308+ - name : Delete preview space on Hugging Face
309+ if : always()
310+ continue-on-error : true
311+ shell : bash
312+ env :
313+ SPACE_REPO_ID : ${{ steps.urls.outputs.space_repo_id }}
314+ run : |
315+ set -euo pipefail
316+ if [ -z "${SPACE_REPO_ID:-}" ]; then
317+ echo "No space repo id; skipping deletion"
318+ exit 0
319+ fi
320+
321+ TOKEN="${HF_TOKEN:-${HF_PR_TOKEN:-}}"
322+ if [ -z "$TOKEN" ]; then
323+ echo "HF token not available; cannot delete space"
324+ exit 0
325+ fi
326+
327+ set +e
328+ hf repo delete "$SPACE_REPO_ID" --repo-type space --yes --token "$TOKEN"
329+ status=$?
330+ set -e
331+
332+ if [ $status -eq 0 ]; then
333+ echo "Deleted preview space $SPACE_REPO_ID"
334+ else
335+ echo "Failed to delete space $SPACE_REPO_ID (exit $status)"
336+ fi
254337
255338 - name : Fail job if health check failed
256339 if : steps.health_check.conclusion == 'failure'
0 commit comments