Skip to content

Commit 10518ed

Browse files
committed
make deploy script compatible with gh actions
1 parent f5a676a commit 10518ed

File tree

1 file changed

+52
-25
lines changed

1 file changed

+52
-25
lines changed

scripts/deploy_to_hf.sh

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ Required arguments:
1414
1515
Optional arguments:
1616
--base-sha <sha|tag> Override openenv-base image reference (defaults to :latest)
17-
--hf-namespace <user> Hugging Face username/organization (defaults to HF_USERNAME/HF_USER or meta-openenv)
17+
--hf-namespace <user> Hugging Face username/organization (defaults to HF_USERNAME or meta-openenv)
1818
--staging-dir <path> Output directory for staging (defaults to hf-staging)
19+
--space-suffix <suffix> Suffix to add to space name (e.g., "-test" for test spaces)
1920
--dry-run Prepare files without pushing to Hugging Face Spaces
2021
-h, --help Show this help message
2122
@@ -43,22 +44,27 @@ SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
4344
REPO_ROOT=$(cd "$SCRIPT_DIR/.." && pwd)
4445
cd "$REPO_ROOT"
4546

46-
if ! command -v hf >/dev/null 2>&1; then
47-
echo "Error: huggingface-hub CLI 'hf' not found in PATH. hf is required to deploy to Hugging Face Spaces." >&2
48-
echo "Install the HF CLI: curl -LsSf https://hf.co/cli/install.sh | sh" >&2
49-
exit 1
47+
# Detect if we're running in GitHub Actions
48+
IS_GITHUB_ACTIONS=false
49+
if [ -n "${GITHUB_ACTIONS:-}" ]; then
50+
IS_GITHUB_ACTIONS=true
5051
fi
5152

52-
HF_USERNAME=$(hf auth whoami | head -n1 | tr -d '\n')
53-
54-
if [ -z "${HF_NAMESPACE:-}" ]; then
55-
echo "🙋 Using default namespace: $HF_USERNAME. You can override the namespace with --hf-namespace"
56-
HF_NAMESPACE="${HF_USERNAME:-}"
53+
# Check for hf CLI - but allow dry-run mode to work without it
54+
if ! command -v hf >/dev/null 2>&1; then
55+
echo "Warning: huggingface-hub CLI 'hf' not found in PATH." >&2
56+
echo "Install the HF CLI: curl -LsSf https://hf.co/cli/install.sh | sh" >&2
57+
if [ "${1:-}" != "--dry-run" ] && [ "${2:-}" != "--dry-run" ]; then
58+
echo "Error: hf is required for deployment (use --dry-run to skip deployment)" >&2
59+
exit 1
60+
fi
5761
fi
5862

5963
ENV_NAME=""
6064
BASE_IMAGE_SHA=""
65+
HF_NAMESPACE="${HF_NAMESPACE:-}" # Initialize from env var if set, otherwise empty
6166
STAGING_DIR="hf-staging"
67+
SPACE_SUFFIX=""
6268
DRY_RUN=false
6369

6470
while [[ $# -gt 0 ]]; do
@@ -71,14 +77,18 @@ while [[ $# -gt 0 ]]; do
7177
BASE_IMAGE_SHA="$2"
7278
shift 2
7379
;;
74-
--hf-namespace|--hf-user|--hf-username)
80+
--namespace|--hf-namespace|--hf-user|--hf-username)
7581
HF_NAMESPACE="$2"
7682
shift 2
7783
;;
7884
--staging-dir)
7985
STAGING_DIR="$2"
8086
shift 2
8187
;;
88+
--suffix|--space-suffix)
89+
SPACE_SUFFIX="$2"
90+
shift 2
91+
;;
8292
--dry-run)
8393
DRY_RUN=true
8494
shift
@@ -127,16 +137,22 @@ if [ ! -d "src/envs/$ENV_NAME" ]; then
127137
exit 1
128138
fi
129139

140+
# Try to get HF_USERNAME, but handle failures gracefully (especially in CI before auth)
141+
if command -v hf >/dev/null 2>&1; then
142+
HF_USERNAME=$(hf auth whoami 2>/dev/null | head -n1 | tr -d '\n' || echo "")
143+
fi
144+
130145
if [ -z "$HF_NAMESPACE" ]; then
146+
# Check HF_USERNAME (env var or detected from CLI)
131147
if [ -n "${HF_USERNAME:-}" ]; then
132-
HF_NAMESPACE="$HF_USERNAME"
133-
elif [ -n "${HF_USER:-}" ]; then
134-
HF_NAMESPACE="$HF_USER"
148+
HF_NAMESPACE="${HF_USERNAME}"
135149
else
136150
HF_NAMESPACE="meta-openenv"
137151
fi
138152
fi
139153

154+
echo "🙋 Using namespace: $HF_NAMESPACE. You can override with --hf-namespace"
155+
140156
# Set base image reference (using GHCR)
141157
if [ -n "$BASE_IMAGE_SHA" ]; then
142158
BASE_IMAGE_REF="ghcr.io/meta-pytorch/openenv-base:$BASE_IMAGE_SHA"
@@ -483,30 +499,41 @@ fi
483499

484500
echo "🔑 Ensuring Hugging Face authentication..."
485501

486-
# just get the env token if it's set
487-
if [ -n "${HF_TOKEN:-}" ]; then
502+
# In GitHub Actions, always use HF_TOKEN if provided
503+
if [ "$IS_GITHUB_ACTIONS" = true ] && [ -n "${HF_TOKEN:-}" ]; then
504+
echo "Using HF_TOKEN from GitHub Actions environment"
505+
hf auth login --token "$HF_TOKEN" --add-to-git-credential >/dev/null 2>&1 || {
506+
echo "Warning: Failed to authenticate with HF_TOKEN, continuing anyway..." >&2
507+
}
508+
elif [ -n "${HF_TOKEN:-}" ]; then
509+
# In local environment, try to use HF_TOKEN if set
488510
hf auth login --token "$HF_TOKEN" --add-to-git-credential >/dev/null 2>&1 || true
489511
fi
490512

491-
# ask the user to login if they're not authenticated
492-
if ! hf auth whoami >/dev/null 2>&1; then
513+
# In interactive mode (not CI), ask user to login if needed
514+
if [ "$IS_GITHUB_ACTIONS" != true ] && ! hf auth whoami >/dev/null 2>&1; then
515+
echo "Not authenticated. Please login to Hugging Face..."
493516
hf auth login
494517
fi
495518

519+
# Verify authentication
496520
if ! hf auth whoami >/dev/null 2>&1; then
497521
echo "Error: Hugging Face authentication failed" >&2
522+
if [ "$IS_GITHUB_ACTIONS" = true ]; then
523+
echo "Ensure HF_TOKEN secret is set in GitHub Actions" >&2
524+
else
525+
echo "Run 'hf auth login' or set HF_TOKEN environment variable" >&2
526+
fi
498527
exit 1
499528
fi
500529

501-
# ensure hf authentication on the namespace
502-
# Check if the user has access to the target HF_NAMESPACE/org
503-
if ! hf auth whoami | grep -qw "$HF_NAMESPACE"; then
504-
echo "Error: Your account does not have access to the Hugging Face namespace '$HF_NAMESPACE'." >&2
530+
CURRENT_USER=$(hf auth whoami | head -n1 | tr -d '\n')
531+
if [ "$CURRENT_USER" != "$HF_NAMESPACE" ] && ! hf auth whoami | grep -qw "$HF_NAMESPACE" 2>/dev/null; then
532+
echo "Warning: Your account ($CURRENT_USER) may not have direct access to namespace '$HF_NAMESPACE'." >&2
505533
echo "Get the correct access token from https://huggingface.co/settings/tokens and set if with 'hf auth login' " >&2
506-
exit 1
507534
fi
508535

509-
SPACE_REPO="${HF_NAMESPACE}/${ENV_NAME}-test"
536+
SPACE_REPO="${HF_NAMESPACE}/${ENV_NAME}${SPACE_SUFFIX}"
510537
CURRENT_STAGING_DIR_ABS=$(cd "$CURRENT_STAGING_DIR" && pwd)
511538

512539
# create the space if it doesn't exist
@@ -520,7 +547,7 @@ fi
520547
# print the URL of the deployed space
521548
echo "✅ Upload completed for https://huggingface.co/spaces/$SPACE_REPO"
522549

523-
# safely cleanup the staging directory
550+
# Cleanup the staging directory after successful deployment
524551
if [ -d "$CURRENT_STAGING_DIR_ABS" ]; then
525552
rm -rf "$CURRENT_STAGING_DIR_ABS"
526553
fi

0 commit comments

Comments
 (0)