[dev] Automatically authorize gh (#20971)

Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
Gero Posmyk-Leinemann 2025-07-23 13:46:34 +02:00 committed by GitHub
parent 7137b3a8ba
commit 6c6f43fa40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 46 additions and 0 deletions

View File

@ -12,6 +12,7 @@
"mounts": [
"source=/usr/local/gitpod/config/,target=/usr/local/gitpod/config/,type=bind"
],
"onCreateCommand": "bash /workspace/gitpod/dev/setup-github-auth.sh",
"remoteEnv": {
"GIT_EDITOR": "code --wait",
"KUBE_EDITOR": "code --wait"

View File

@ -47,6 +47,10 @@ tasks:
leeway run components/local-app:install-cli
leeway run components/local-app:cli-completion
exit 0
- name: Setup GitHub CLI Auth
init: |
bash /workspace/gitpod/dev/setup-github-auth.sh
exit 0
# This task takes care of configuring your workspace so it can manage and interact
# with preview environments.
- name: Preview environment configuration

18
dev/github-token.sh Executable file
View File

@ -0,0 +1,18 @@
#!/bin/bash
# GitHub Token Helper
# Dynamically retrieves GitHub token from git credentials
# Safe to source - will not error if git credentials are unavailable
# Only set GH_TOKEN if not already set and git credential is available
if [ -z "$GH_TOKEN" ] && command -v git >/dev/null 2>&1; then
# Attempt to get token from git credentials, suppress errors
TOKEN=$(printf 'protocol=https\nhost=github.com\n' | git credential fill 2>/dev/null | awk -F= '/password/ {print $2}' 2>/dev/null)
# Only export if we got a non-empty token
if [ -n "$TOKEN" ]; then
export GH_TOKEN="$TOKEN"
fi
unset TOKEN
fi

23
dev/setup-github-auth.sh Executable file
View File

@ -0,0 +1,23 @@
#!/bin/bash
# GitHub CLI Authentication Setup
# Adds sourcing of GitHub token helper to shell profiles
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GITHUB_TOKEN_HELPER="$SCRIPT_DIR/github-token.sh"
{
echo ""
echo "# GitHub token helper"
echo "source \"$GITHUB_TOKEN_HELPER\""
} >> ~/.bashrc
{
echo ""
echo "# GitHub token helper"
echo "source \"$GITHUB_TOKEN_HELPER\""
} >> ~/.zshrc
source "$GITHUB_TOKEN_HELPER"
echo "✅ GitHub CLI configured"