505505
506506echo " 🔑 Ensuring Hugging Face authentication..."
507507
508- # In GitHub Actions, explicitly authenticate with token
508+ # Set up authentication based on environment
509+ TOKEN_ARGS=()
509510if [ " $IS_GITHUB_ACTIONS " = true ]; then
510511 if [ -z " ${HF_TOKEN:- } " ]; then
511512 echo " Error: HF_TOKEN secret is required in GitHub Actions" >&2
512513 echo " Please set the HF_TOKEN secret in repository settings" >&2
513514 exit 1
514515 fi
515- echo " Authenticating with HF_TOKEN..."
516- LOGIN_OUTPUT=$( hf auth login --token " $HF_TOKEN " 2>&1 )
517- LOGIN_EXIT_CODE=$?
518- if [ $LOGIN_EXIT_CODE -ne 0 ]; then
519- echo " Error: Failed to authenticate with HF_TOKEN" >&2
520- echo " $LOGIN_OUTPUT " >&2
521- exit 1
522- fi
516+ echo " Using HF_TOKEN from GitHub Actions environment"
517+ # In CI, pass token directly to commands via --token flag
518+ TOKEN_ARGS=(--token " $HF_TOKEN " )
523519elif [ -n " ${HF_TOKEN:- } " ]; then
524- # In local environment, try to use HF_TOKEN if set
525- hf auth login --token " $HF_TOKEN " --add-to-git-credential > /dev/null 2>&1 || true
526- fi
527-
528- # In interactive mode (not CI), ask user to login if needed
529- if [ " $IS_GITHUB_ACTIONS " != true ] && ! hf auth whoami > /dev/null 2>&1 ; then
530- echo " Not authenticated. Please login to Hugging Face..."
531- hf auth login
520+ # If HF_TOKEN is set locally, use it
521+ echo " Using HF_TOKEN environment variable"
522+ TOKEN_ARGS=(--token " $HF_TOKEN " )
523+ else
524+ # Interactive mode: check if user is authenticated
525+ if ! hf auth whoami > /dev/null 2>&1 ; then
526+ echo " Not authenticated. Please login to Hugging Face..."
527+ hf auth login
528+ if ! hf auth whoami > /dev/null 2>&1 ; then
529+ echo " Error: Hugging Face authentication failed" >&2
530+ exit 1
531+ fi
532+ fi
532533fi
533534
534- # Verify authentication
535- if ! hf auth whoami > /dev/null 2>&1 ; then
536- echo " Error: Hugging Face authentication failed" >&2
537- if [ " $IS_GITHUB_ACTIONS " = true ]; then
538- echo " Ensure HF_TOKEN secret is set in GitHub Actions" >&2
539- else
535+ # Verify authentication works (skip in CI if using token directly)
536+ if [ ${# TOKEN_ARGS[@]} -eq 0 ]; then
537+ if ! hf auth whoami > /dev/null 2>&1 ; then
538+ echo " Error: Not authenticated with Hugging Face" >&2
540539 echo " Run 'hf auth login' or set HF_TOKEN environment variable" >&2
540+ exit 1
541541 fi
542- exit 1
543- fi
544-
545- CURRENT_USER= $( hf auth whoami | head -n1 | tr -d ' \n ' )
546- if [ " $CURRENT_USER " != " $HF_NAMESPACE " ] && ! hf auth whoami | grep -qw " $HF_NAMESPACE " 2> /dev/null ; then
547- echo " Warning: Your account ( $CURRENT_USER ) may not have direct access to namespace ' $HF_NAMESPACE '. " >&2
548- echo " Get the correct access token from https://huggingface.co/settings/tokens and set if with 'hf auth login' " >&2
542+ CURRENT_USER= $( hf auth whoami | head -n1 | tr -d ' \n ' )
543+ echo " ✅ Authenticated as: $CURRENT_USER "
544+ if [ " $CURRENT_USER " != " $HF_NAMESPACE " ] ; then
545+ echo " ⚠️ Deploying to namespace ' $HF_NAMESPACE ' (different from your user ' $CURRENT_USER ') "
546+ fi
547+ else
548+ echo " ✅ Token configured for deployment "
549549fi
550550
551551SPACE_REPO=" ${HF_NAMESPACE} /${ENV_NAME}${SPACE_SUFFIX} "
@@ -558,9 +558,10 @@ if [ "$PRIVATE" = true ]; then
558558fi
559559
560560# create the space if it doesn't exist
561- hf repo create " $SPACE_REPO " --repo-type space --space_sdk docker --exist-ok $PRIVATE_FLAG --quiet > /dev/null 2>&1 || true
561+ hf repo create " $SPACE_REPO " --repo-type space --space_sdk docker --exist-ok $PRIVATE_FLAG ${TOKEN_ARGS[@]+" ${TOKEN_ARGS[@]} " } --quiet > /dev/null 2>&1 || true
562+
562563# upload the staged content (if repo doesn't exist, it will be created with the privacy setting)
563- SPACE_UPLOAD_RESULT=$( hf upload --repo-type=space $PRIVATE_FLAG --quiet " $SPACE_REPO " " $CURRENT_STAGING_DIR_ABS " )
564+ SPACE_UPLOAD_RESULT=$( hf upload --repo-type=space $PRIVATE_FLAG ${TOKEN_ARGS[@]+ " ${TOKEN_ARGS[@]} " } --quiet " $SPACE_REPO " " $CURRENT_STAGING_DIR_ABS " 2>&1 )
564565if [ $? -ne 0 ]; then
565566 echo " ❌ Upload failed: $SPACE_UPLOAD_RESULT " >&2
566567 exit 1
0 commit comments