From 4298bd6c19217fb4496b5d199952f5025d2012a6 Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Mon, 3 Nov 2025 12:39:34 +0100 Subject: [PATCH 1/4] use cli in action and update comment --- .github/workflows/pr-new-env.yml | 67 +++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/.github/workflows/pr-new-env.yml b/.github/workflows/pr-new-env.yml index 1374abf7..955f9913 100644 --- a/.github/workflows/pr-new-env.yml +++ b/.github/workflows/pr-new-env.yml @@ -103,6 +103,7 @@ jobs: environment: ${{ fromJSON(needs.detect-new-envs.outputs.new_envs_json) }} env: HF_TOKEN: ${{ secrets.HF_PR_TOKEN }} + HUGGINGFACEHUB_API_TOKEN: ${{ secrets.HF_PR_TOKEN }} HF_NAMESPACE: ${{ vars.HF_PR_NAMESPACE }} SPACE_SUFFIX: -pr-${{ github.event.number }} steps: @@ -127,18 +128,24 @@ jobs: exit 1 fi - - name: Install Hugging Face CLI + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - name: Install OpenEnv package shell: bash run: | - curl -LsSf https://hf.co/cli/install.sh | bash - echo "$HOME/.local/bin" >> "$GITHUB_PATH" + set -euo pipefail + python -m pip install --upgrade pip + pip install . - - name: Deploy environment to Hugging Face + - name: Deploy environment with OpenEnv CLI shell: bash run: | set -euo pipefail - chmod +x scripts/deploy_to_hf.sh - ./scripts/deploy_to_hf.sh --env "${{ matrix.environment }}" --space-suffix "${SPACE_SUFFIX}" --hub-tag "openenv-pr" + repo_id="${HF_NAMESPACE}/${{ matrix.environment }}${SPACE_SUFFIX}" + openenv push "${{ matrix.environment }}" --repo-id "$repo_id" - name: Wait for deployment to stabilize shell: bash @@ -161,6 +168,7 @@ jobs: health_url="https://${namespace_slug}-${space_slug}.hf.space/health" live_url="https://${namespace_slug}-${space_slug}.hf.space" space_repo_url="https://huggingface.co/spaces/${HF_NAMESPACE}/${space_name}" + space_repo_id="${HF_NAMESPACE}/${space_name}" echo "namespace_slug=${namespace_slug}" >> "$GITHUB_OUTPUT" echo "space_name=${space_name}" >> "$GITHUB_OUTPUT" @@ -168,6 +176,7 @@ jobs: echo "health_url=${health_url}" >> "$GITHUB_OUTPUT" echo "live_url=${live_url}" >> "$GITHUB_OUTPUT" echo "space_repo_url=${space_repo_url}" >> "$GITHUB_OUTPUT" + echo "space_repo_id=${space_repo_id}" >> "$GITHUB_OUTPUT" - name: Perform environment health check id: health_check @@ -216,7 +225,9 @@ jobs: SPACE_NAME: ${{ steps.urls.outputs.space_name }} LIVE_URL: ${{ steps.urls.outputs.live_url }} SPACE_REPO_URL: ${{ steps.urls.outputs.space_repo_url }} + SPACE_REPO_ID: ${{ steps.urls.outputs.space_repo_id }} ENV_NAME: ${{ matrix.environment }} + COMMENT_TAG: "" with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | @@ -224,7 +235,9 @@ jobs: const spaceName = process.env.SPACE_NAME; const liveUrl = process.env.LIVE_URL; const repoUrl = process.env.SPACE_REPO_URL; + const repoId = process.env.SPACE_REPO_ID; const envName = process.env.ENV_NAME; + const marker = process.env.COMMENT_TAG; const header = status === 'success' ? `✅ Deployment succeeded for \`${envName}\`` @@ -235,6 +248,8 @@ jobs: : 'Please resolve your environment.'; const body = [ + marker, + '', header, '', `- Space repo: [${repoUrl}](${repoUrl})`, @@ -242,15 +257,41 @@ jobs: '', summary, '', - 'You can iterate locally or validate fixes by running `scripts/deploy_to_hf.sh --env "' + envName + '"`.' + '- CLI: `openenv push ' + envName + ' --repo-id ' + repoId + '`', + '- Auth: export `HUGGINGFACEHUB_API_TOKEN` before running' ].join('\n'); - await github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.payload.pull_request.number, - body - }); + const {owner, repo} = context.repo; + const issue_number = context.payload.pull_request.number; + + const existing = await github.paginate( + github.rest.issues.listComments, + { owner, repo, issue_number, per_page: 100 }, + (response, done) => { + const match = response.data.find(comment => comment.body && comment.body.includes(marker)); + if (match) { + done(); + return [match]; + } + return []; + } + ); + + if (existing.length > 0) { + await github.rest.issues.updateComment({ + owner, + repo, + comment_id: existing[0].id, + body, + }); + } else { + await github.rest.issues.createComment({ + owner, + repo, + issue_number, + body, + }); + } - name: Fail job if health check failed if: steps.health_check.conclusion == 'failure' From a1a1b473f489a49058fefc3f09c56734d3fa2bb9 Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Mon, 3 Nov 2025 12:39:56 +0100 Subject: [PATCH 2/4] add delete space step --- .github/workflows/pr-new-env.yml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/.github/workflows/pr-new-env.yml b/.github/workflows/pr-new-env.yml index 955f9913..e9854303 100644 --- a/.github/workflows/pr-new-env.yml +++ b/.github/workflows/pr-new-env.yml @@ -293,6 +293,36 @@ jobs: }); } + - name: Delete preview space on Hugging Face + if: always() + continue-on-error: true + shell: bash + env: + SPACE_REPO_ID: ${{ steps.urls.outputs.space_repo_id }} + run: | + set -euo pipefail + if [ -z "${SPACE_REPO_ID:-}" ]; then + echo "No space repo id; skipping deletion" + exit 0 + fi + + TOKEN="${HF_TOKEN:-${HUGGINGFACEHUB_API_TOKEN:-}}" + if [ -z "$TOKEN" ]; then + echo "HF token not available; cannot delete space" + exit 0 + fi + + set +e + hf repo delete "$SPACE_REPO_ID" --repo-type space --yes --token "$TOKEN" + status=$? + set -e + + if [ $status -eq 0 ]; then + echo "Deleted preview space $SPACE_REPO_ID" + else + echo "Failed to delete space $SPACE_REPO_ID (exit $status)" + fi + - name: Fail job if health check failed if: steps.health_check.conclusion == 'failure' run: exit 1 From 1a7612812f95cfe38238fd71057e589f0fb029da Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Mon, 3 Nov 2025 12:45:52 +0100 Subject: [PATCH 3/4] streamline comment --- .github/workflows/pr-new-env.yml | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/.github/workflows/pr-new-env.yml b/.github/workflows/pr-new-env.yml index e9854303..e5187221 100644 --- a/.github/workflows/pr-new-env.yml +++ b/.github/workflows/pr-new-env.yml @@ -240,25 +240,22 @@ jobs: const marker = process.env.COMMENT_TAG; const header = status === 'success' - ? `✅ Deployment succeeded for \`${envName}\`` - : `⚠️ Deployment failed for \`${envName}\``; + ? `✅ Deployment to Hugging Face succeeded for \`${envName}\`` + : `⚠️ Deployment Hugging Face failed for \`${envName}\``; const summary = status === 'success' - ? 'Nice work! Wait for a code review and we\'re ready to go.' - : 'Please resolve your environment.'; + ? 'Nice work! Wait for a code review and we\'re ready to go. You can test it with the CLI:' + : 'Please resolve your environment and test it with the CLI:'; const body = [ marker, '', header, '', - `- Space repo: [${repoUrl}](${repoUrl})`, - `- Live URL: [${liveUrl}](${liveUrl})`, '', summary, '', - '- CLI: `openenv push ' + envName + ' --repo-id ' + repoId + '`', - '- Auth: export `HUGGINGFACEHUB_API_TOKEN` before running' + '- `openenv push ' + envName + ' --repo-id ' + repoId + '`', ].join('\n'); const {owner, repo} = context.repo; From 3da0b52ac70d73ca193658dc90622e7fa5cbb9ce Mon Sep 17 00:00:00 2001 From: burtenshaw Date: Mon, 3 Nov 2025 13:07:31 +0100 Subject: [PATCH 4/4] improve comment --- .github/workflows/pr-new-env.yml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pr-new-env.yml b/.github/workflows/pr-new-env.yml index e5187221..69c2b548 100644 --- a/.github/workflows/pr-new-env.yml +++ b/.github/workflows/pr-new-env.yml @@ -255,11 +255,21 @@ jobs: '', summary, '', - '- `openenv push ' + envName + ' --repo-id ' + repoId + '`', - ].join('\n'); + '- `openenv push ' + envName + ' --repo-id /' + envName + '`', + ]; const {owner, repo} = context.repo; const issue_number = context.payload.pull_request.number; + const serverUrl = process.env.GITHUB_SERVER_URL || 'https://github.com'; + const runUrl = `${serverUrl}/${owner}/${repo}/actions/runs/${context.runId}`; + + if (status !== 'success') { + body.push(`- Failed run: ${runUrl}`); + } else { + body.push(`- Success run: ${runUrl}`); + } + + const bodyText = body.join('\n'); const existing = await github.paginate( github.rest.issues.listComments,