Repository: gradio-app/gradio
File: gradio/cli/commands/deploy_space.py (Python)

Code before:

def check_gcloud_auth():
    """Check if user is logged in to Google Cloud and has a project selected."""
    try:
        auth_result = subprocess.run(
            ["gcloud", "auth", "list", "--filter=status:ACTIVE"],
            check=True,
            text=True,
            capture_output=True,
        )

        if not auth_result.stdout.strip():
            print("[bold yellow]You are not logged in to Google Cloud.[/bold yellow]")
            print("Running 'gcloud init' to set up authentication...")
            subprocess.run(["gcloud", "init"], check=True)

        project_result = subprocess.run(
            ["gcloud", "config", "get-value", "project"],
            check=True,
            text=True,
            capture_output=True,
        )

        if not project_result.stdout.strip():
            print("[bold yellow]No Google Cloud project is selected.[/bold yellow]")
            print("Running 'gcloud init' to select a project...")
            subprocess.run(["gcloud", "init"], check=True)
            return

        print(f"[green]✓ Authenticated as: {auth_result.stdout.strip()}[/green]")
        print(f"[green]✓ Project: {project_result.stdout.strip()}[/green]")

    except subprocess.CalledProcessError as e:
        print(f"[bold red]Error checking Google Cloud configuration: {e}[/bold red]")
        print("Running 'gcloud init' to set up configuration...")
        try:
            subprocess.run(["gcloud", "init"], check=True)
        except subprocess.CalledProcessError as init_error:
            print(f"[red]Failed to run 'gcloud init': {init_error}[/red]")
            return False
    except FileNotFoundError:
        print(
            "[bold red]gcloud CLI not found. Please install Google Cloud SDK.[/bold red]"
        )
        return False

    return True


def deploy_to_gcloud():
    """Deploy a Gradio app to Google Cloud Run. Always uses app.py as the entry point."""

Review comment (suggestion):
If no project is available, I suggest instead prompting for a project ID, instead of doing a full `gcloud init`.

Once we have the project ID from the user, we can pass it as `--project` argument to the `gcloud run deploy` command

Code after:

def check_gcloud_auth():
    """Check if user is logged in to Google Cloud and has a project selected."""
    try:
        auth_result = subprocess.run(
            ["gcloud", "auth", "list", "--filter=status:ACTIVE"],
            check=True,
            text=True,
            capture_output=True,
        )

        if not auth_result.stdout.strip():
            print("[bold yellow]You are not logged in to Google Cloud.[/bold yellow]")
            print("Running 'gcloud init' to set up authentication...")
            subprocess.run(["gcloud", "init"], check=True)

        project_result = subprocess.run(
            ["gcloud", "config", "get-value", "project"],
            check=True,
            text=True,
            capture_output=True,
        )

        if not project_result.stdout.strip():
            print("[bold yellow]No Google Cloud project is selected.[/bold yellow]")
            project_id = input("Enter your Google Cloud project ID: ")
            if not project_id:
                print("[red]Project ID is required for deployment.[/red]")
                return None
            return project_id

        print(f"[green]✓ Authenticated as: {auth_result.stdout.strip()}[/green]")
        print(f"[green]✓ Project: {project_result.stdout.strip()}[/green]")
        return project_result.stdout.strip()

    except subprocess.CalledProcessError as e:
        print(f"[bold red]Error checking Google Cloud configuration: {e}[/bold red]")
        print("Running 'gcloud init' to set up configuration...")
        try:
            subprocess.run(["gcloud", "init"], check=True)
        except subprocess.CalledProcessError as init_error:
            print(f"[red]Failed to run 'gcloud init': {init_error}[/red]")
            return False
    except FileNotFoundError:
        print(
            "[bold red]gcloud CLI not found. Please install Google Cloud SDK.[/bold red]"
        )
        return False

    return True