๐Ÿ”ฅSave up to $132K/month in CI costs!Try Freeโ†’
Skip to main content

How to use GitHub Personal Access Tokens Securely

6 min read
Author: Nick Osborne
Co-Founder & CTO at CICube
Building the next generation of DevOps tools.

This article was last updated on January 28, 2025, to include advanced techniques for managing GitHub Personal Access Tokens, such as token rotation strategies, secure storage practices, automated management scripts, and troubleshooting common issues, along with practical examples and simplified explanations for better clarity.

What are GitHub Personal Access Tokens and Why Use Them?โ€‹

TL;DR

Think of them like special passwords for your GitHub account that you can have more fine-grained control over. You generate these tokens for explicit permissions to perform certain tasks, not using your password.

Key points:

  • More secure than using your password
  • Can be restricted to certain repositories
  • Can be given exact permissions you need
  • Can be revoked anytime without affecting your main account Two types: fine-grained (newer, more secure), classic

Debugging CI pipeline failures because of expired tokens and security incidents because of leaked credentials taught me that it is critical to manage tokens. Consider personal access tokens like keys to your house-you want different keys for different purposes, and you definitely don't want to give everybody a master key!

Steps we'll cover:

How to Choose Between Fine-grained and Classic Tokensโ€‹

Click to zoom

So, when I started off with the GitHub automation, the most frequent rookie mistake that everybody does-and which I did, by the way-was using your account password everywhere. That would be like using your house key for your car, your garage, and your office: not secure! This is solved with Personal Access Tokens that provide specific "keys" for specific "doors."

Interactive Guide: Choose the Right Token Permissionsโ€‹

Not sure which permissions your token should have? Try our interactive permission calculator:

Token Permission Calculator

1. Choose Your Use Case

CI/CD Pipeline
Recommended

Access for automated builds and deployments

Repository Management
Recommended

Basic repository operations like clone, push, pull

Package Publishing

Publishing packages to GitHub Packages

Issue Management

Managing issues and pull requests

Organization Management

Managing organization settings and teams

Custom Configuration

Select individual permissions for your specific needs

Security Tips:

  • Always use the minimum required permissions
  • Set an expiration date for your tokens
  • Use fine-grained tokens when possible
  • Never commit tokens to version control

Step-by-Step Guide: Creating Your First GitHub Tokenโ€‹

It's time I showed you how to actually do a token like a proper developer, as I might a junior developer.

Here's what each field means:

Token name: "CI Pipeline Token"  # Like labeling your keys
Expiration: "30 days" # When the key expires
Repository access: "Selected" # Which doors it can open
Permissions: "Read-only" # What it can do behind those doors

Essential Security Best Practices for GitHub Tokensโ€‹

After one too many close calls, including one where a token was accidentally committed to a public repository - yes, I've been there - I came up with these security rules:

Token Scope

# โŒ Bad: Too many permissions
permissions: "repo, admin:org, delete_repo"

# โœ… Good: Minimal required permissions
permissions: "repo:status, repo:deployment"

Token Storage

# โŒ Bad: Hardcoding tokens
git clone https://[email protected]/repo.git

# โœ… Good: Using environment variables
git clone https://${GITHUB_TOKEN}@github.com/repo.git

end

Troubleshooting Common GitHub Token Issuesโ€‹

Let me share some war stories and their solutions:

How to Fix Token Expiration Issuesโ€‹

# โŒ Error you might see:
git push
> fatal: Authentication failed

# โœ… Solution: Check token expiration
gh auth status

Solving Token Permission Issues

# โœ— Error:
curl -H "Authorization: token $TOKEN" https://api.github.com/repos/org/repo > {"message": "Not Found"}

# โœ… Solution: Verify token permissions
gh auth token -s

Enterprise Guide: Managing GitHub Tokens at Scaleโ€‹

When you're managing several teams and projects, GitHub tokens get out of hand. Simple system I'm using:

1. Use Clear Names

team_project_environment
# Examples:
frontend_deploy_prod
backend_ci_dev

2. General Rules

  • Assign each token an owner
  • Set expiration dates: 30 days in production, 90 days in development
  • Keep a backup token for critical services
  • Document who uses what

3. Simple Token Management Script

#!/bin/bash
# github-token-manager.sh
# Save this script and make it executable: chmod +x github-token-manager.sh

# Configuration
TOKEN_FILE="tokens.txt" # Format: token_name,expiry_date,owner_email
GITHUB_TOKEN="your-github-token" # Token with admin rights

# Check for expired tokens
check_expired() {
echo "Checking for expired tokens..."
while IFS=, read -r name expiry email; do
if [[ $(date +%s) -gt $(date -j -f "%Y-%m-%d" "$expiry" +%s) ]]; then
echo "โš ๏ธ Token expired: $name (Owner: $email)"
notify_owner "$email" "$name"
fi
done < "$TOKEN_FILE"
}

# Send email notification
notify_owner() {
email=$1
token_name=$2
echo "๐Ÿ“ง Notifying $email about expired token: $token_name"
# Add your email sending logic here
# Example: mail -s "Token Expired" "$email" <<< "Your token $token_name has expired"
}

# List all tokens
list_tokens() {
echo "Current tokens:"
while IFS=, read -r name expiry email; do
echo "- $name (Expires: $expiry, Owner: $email)"
done < "$TOKEN_FILE"
}

# Main menu
case "$1" in
"check")
check_expired
;;
"list")
list_tokens
;;
*)
echo "Usage: $0 {check|list}"
echo " check - Check for expired tokens"
echo " list - List all tokens"
;;
esac

How to Use the Script:

# Check for expired tokens
./github-token-manager.sh check

# List all tokens
./github-token-manager.sh list

This simple system helps you:

  • Kept track of who owns which token - Be notified when tokens expire

  • Keep an overview of all tokens Remember: Start simple and add more features as your needs grow. You don't need a complex system when you're just getting started with team token management.

Conclusionโ€‹

Personal Access Tokens are a sort of keys to your kingdom in GitHub, and you'll want to be rather discerning about their creation, usage, and management. Start with fine-grained tokens, apply the principle of least privilege, and always prepare a rotation and management plan.

Remember: It's much easier to start with less and add permissions than have to clean up after a stolen token that had way too much access. Trust me, I learned this the hard way!