Skip to content

Commit 0c74d5c

Browse files
committed
add push step and simplify logging
1 parent 935e95e commit 0c74d5c

File tree

1 file changed

+80
-14
lines changed

1 file changed

+80
-14
lines changed

scripts/deploy_to_hf.sh

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set -euo pipefail
77

88
usage() {
99
cat <<'EOF'
10-
Usage: scripts/prepare_hf_deployment.sh --env <environment_name> [options]
10+
Usage: scripts/deploy_to_hf.sh --env <environment_name> [options]
1111
1212
Required arguments:
1313
--env <name> Environment name under src/envs (e.g. textarena_env)
@@ -16,15 +16,16 @@ Optional arguments:
1616
--base-sha <sha|tag> Override openenv-base image reference (defaults to :latest)
1717
--hf-namespace <user> Hugging Face username/organization (defaults to HF_USERNAME/HF_USER or meta-openenv)
1818
--staging-dir <path> Output directory for staging (defaults to hf-staging)
19+
--dry-run Prepare files without pushing to Hugging Face Spaces
1920
-h, --help Show this help message
2021
2122
Positional compatibility:
2223
You can also call the script as:
23-
scripts/prepare_hf_deployment.sh <env_name> [base_image_sha]
24+
scripts/deploy_to_hf.sh <env_name> [base_image_sha]
2425
2526
Examples:
26-
scripts/prepare_hf_deployment.sh --env textarena_env --hf-namespace my-team
27-
scripts/prepare_hf_deployment.sh chat_env sha-0123456789abcdef
27+
scripts/deploy_to_hf.sh --env textarena_env --hf-namespace my-team
28+
scripts/deploy_to_hf.sh chat_env sha-0123456789abcdef
2829
EOF
2930
}
3031

@@ -42,10 +43,29 @@ SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
4243
REPO_ROOT=$(cd "$SCRIPT_DIR/.." && pwd)
4344
cd "$REPO_ROOT"
4445

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+
49+
if [ "$OSTYPE" == "linux-gnu"* || "$OSTYPE" == "darwin"* ]; then
50+
echo "Install the HF CLI: curl -LsSf https://hf.co/cli/install.sh | sh" >&2
51+
elif [ "$OSTYPE" == "windows"* ]; then
52+
echo "Install the HF CLI: powershell -ExecutionPolicy ByPass -c \"irm https://hf.co/cli/install.ps1 | iex\"" >&2
53+
fi
54+
55+
exit 1
56+
fi
57+
58+
HF_USERNAME=$(hf auth whoami | head -n1 | tr -d '\n')
59+
60+
if [ -z "${HF_NAMESPACE:-}" ]; then
61+
echo "🙋 Using default namespace: $HF_USERNAME. You can override the namespace with --hf-namespace"
62+
HF_NAMESPACE="${HF_USERNAME:-}"
63+
fi
64+
4565
ENV_NAME=""
4666
BASE_IMAGE_SHA=""
47-
HF_NAMESPACE="${HF_NAMESPACE:-}"
4867
STAGING_DIR="hf-staging"
68+
DRY_RUN=false
4969

5070
while [[ $# -gt 0 ]]; do
5171
case "$1" in
@@ -65,6 +85,10 @@ while [[ $# -gt 0 ]]; do
6585
STAGING_DIR="$2"
6686
shift 2
6787
;;
88+
--dry-run)
89+
DRY_RUN=true
90+
shift
91+
;;
6892
-h|--help)
6993
usage
7094
exit 0
@@ -125,23 +149,18 @@ if [ -n "$BASE_IMAGE_SHA" ]; then
125149
echo "Using specific SHA for openenv-base: $BASE_IMAGE_SHA"
126150
else
127151
BASE_IMAGE_REF="ghcr.io/meta-pytorch/openenv-base:latest"
128-
echo "Using latest tag for openenv-base"
129152
fi
130153

131-
echo "Preparing $ENV_NAME environment for deployment..."
132-
133154
# Create staging directory
134155
CURRENT_STAGING_DIR="${STAGING_DIR}/${HF_NAMESPACE}/${ENV_NAME}"
135156
mkdir -p "$CURRENT_STAGING_DIR/src/core"
136157
mkdir -p "$CURRENT_STAGING_DIR/src/envs/$ENV_NAME"
137158

138159
# Copy core files
139160
cp -R src/core/ "$CURRENT_STAGING_DIR/src/core/"
140-
echo "Copied core files"
141-
142161
# Copy environment files
143162
cp -R "src/envs/$ENV_NAME/" "$CURRENT_STAGING_DIR/src/envs/$ENV_NAME/"
144-
echo "Copied $ENV_NAME environment files"
163+
echo "📁 Copied core and $ENV_NAME environment files to $CURRENT_STAGING_DIR"
145164

146165
# Create environment-specific multi-stage Dockerfile
147166
create_environment_dockerfile() {
@@ -271,7 +290,6 @@ create_environment_dockerfile "$ENV_NAME"
271290

272291
# Add web interface support
273292
echo "ENV ENABLE_WEB_INTERFACE=true" >> $CURRENT_STAGING_DIR/Dockerfile
274-
echo "Added web interface support"
275293

276294
# Create environment-specific README
277295
create_readme() {
@@ -462,5 +480,53 @@ README_EOF
462480
}
463481

464482
create_readme "$ENV_NAME"
465-
echo "Created README for HF Space"
466-
echo "Completed preparation for $ENV_NAME environment"
483+
echo "📝 Created README and web interface support for HF Space"
484+
485+
if $DRY_RUN; then
486+
echo "👀 Dry run enabled; skipping Hugging Face upload."
487+
exit 0
488+
fi
489+
490+
echo "🔑 Ensuring Hugging Face authentication..."
491+
492+
# just get the env token if it's set
493+
if [ -n "${HF_TOKEN:-}" ]; then
494+
hf auth login --token "$HF_TOKEN" --add-to-git-credential >/dev/null 2>&1 || true
495+
fi
496+
497+
# ask the user to login if they're not authenticated
498+
if ! hf auth whoami >/dev/null 2>&1; then
499+
hf auth login
500+
fi
501+
502+
if ! hf auth whoami >/dev/null 2>&1; then
503+
echo "Error: Hugging Face authentication failed" >&2
504+
exit 1
505+
fi
506+
507+
# ensure hf authentication on the namespace
508+
# Check if the user has access to the target HF_NAMESPACE/org
509+
if ! hf auth whoami | grep -qw "$HF_NAMESPACE"; then
510+
echo "Error: Your account does not have access to the Hugging Face namespace '$HF_NAMESPACE'." >&2
511+
echo "Get the correct access token from https://huggingface.co/settings/tokens and set if with 'hf auth login' " >&2
512+
exit 1
513+
fi
514+
515+
SPACE_REPO="${HF_NAMESPACE}/${ENV_NAME}-test"
516+
CURRENT_STAGING_DIR_ABS=$(cd "$CURRENT_STAGING_DIR" && pwd)
517+
518+
# create the space if it doesn't exist
519+
hf repo create "$SPACE_REPO" --repo-type space --space_sdk docker --exist-ok --quiet >/dev/null 2>&1 || true
520+
# upload the staged content
521+
SPACE_UPLOAD_RESULT=$(hf upload --repo-type=space --quiet "$SPACE_REPO" "$CURRENT_STAGING_DIR_ABS")
522+
if [ $? -ne 0 ]; then
523+
echo "❌ Upload failed: $SPACE_UPLOAD_RESULT" >&2
524+
exit 1
525+
fi
526+
# print the URL of the deployed space
527+
echo "✅ Upload completed for https://huggingface.co/spaces/$SPACE_REPO"
528+
529+
# safely cleanup the staging directory
530+
if [ -d "$CURRENT_STAGING_DIR_ABS" ]; then
531+
rm -rf "$CURRENT_STAGING_DIR_ABS"
532+
fi

0 commit comments

Comments
 (0)