Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 99 additions & 21 deletions .github/workflows/pr-new-env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -161,13 +168,15 @@ 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"
echo "space_slug=${space_slug}" >> "$GITHUB_OUTPUT"
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
Expand Down Expand Up @@ -216,41 +225,110 @@ 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: "<!-- openenv-pr-preview -->"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const status = process.env.HEALTH_CONCLUSION || 'failure';
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}\``
: `⚠️ 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,
'',
'You can iterate locally or validate fixes by running `scripts/deploy_to_hf.sh --env "' + envName + '"`.'
].join('\n');

await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body
});
'- `openenv push ' + envName + ' --repo-id <your-username>/' + 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,
{ 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: 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'
Expand Down