If you’re using GitHub Actions, CI/CD pipelines, or deploying applications from GitHub, you’ll need to store sensitive information like API keys, passwords, tokens, or SSH keys.
Hardcoding these credentials in your code is a security risk—that’s where GitHub Secrets come in.
In this guide, we’ll cover:
- ✅ What GitHub Secrets are
- ✅ How to add GitHub Secrets via the Web UI (easiest method)
- ✅ How to set GitHub Secrets with the GitHub CLI (fastest for developers)
- ✅ How to use secrets in GitHub Actions workflows
🔍 What Are GitHub Secrets?
GitHub Secrets are encrypted environment variables that store sensitive data securely. They’re not visible to anyone browsing your repository and can be used in GitHub Actions workflows or other automation scripts.
Some common secrets include:
- 🔑 API keys (Stripe, Twilio, AWS, Google Cloud)
- 🗄️ Database credentials
- 🔐 OAuth tokens
- 🔑 SSH keys
🖥️ Method 1: Add GitHub Secrets Using the Web UI (Beginner-Friendly)
The fastest way for beginners is to use the GitHub web interface:
- Open your repository on GitHub.
- Click ⚙️ Settings (top navigation).
- Scroll down to Secrets and variables → Actions.
- Click New repository secret.
- Enter:
-
Name:
MY_SECRET_KEY
-
Value:
your-secret-value
- Click Add secret.
That’s it! Your secret is now securely stored.
🔹 Example: Using the Secret in a GitHub Actions Workflow
name: Deploy Application
on: [push]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Print secret (for demo purposes)
run: echo "Secret is $SECRET_KEY"
env:
SECRET_KEY: ${{ secrets.MY_SECRET_KEY }}
⚠️ Pro Tip: Never print actual secrets in logs—this is just for demonstration.
🖥️ Method 2: Add GitHub Secrets Using GitHub CLI (For Developers)
If you work with terminals and want a faster, scriptable option, use the GitHub CLI.
🔹 Step 1: Install GitHub CLI
# macOS
brew install gh
# Ubuntu/Debian
sudo apt install gh
# Windows
winget install --id GitHub.cli
🔹 Step 2: Authenticate GitHub CLI
gh auth login
- Choose GitHub.com.
- Log in via browser or token.
🔹 Step 3: Set a Secret
cd path/to/your/repo
gh secret set MY_SECRET_KEY -b"your-secret-value"
✅ Your secret is now securely stored and ready for use in GitHub Actions.
🚀 When to Use GitHub Secrets
You should store anything sensitive that shouldn’t live in your codebase, such as:
- 🔑 API keys and authentication tokens
- 🗄️ Database usernames and passwords
- 📦 Cloud service credentials (AWS, Azure, GCP)
- 🔐 Private keys for SSH or JWT signing
🏁 Quick Recap
Method | Best For |
---|---|
Web UI | Beginners and quick setup |
GitHub CLI | Advanced users, automation, scripting |
Both methods are secure and encrypted by GitHub.
🔥 SEO Keywords to Target
- How to add GitHub Secrets
- GitHub Actions secrets setup
- Store API keys securely GitHub
- GitHub CLI secrets command
- GitHub security best practices
Top comments (0)