DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on

How to Add GitHub Secrets Easily (Step-by-Step Guide)

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:

  1. Open your repository on GitHub.
  2. Click ⚙️ Settings (top navigation).
  3. Scroll down to Secrets and variables → Actions.
  4. Click New repository secret.
  5. Enter:
  • Name: MY_SECRET_KEY
  • Value: your-secret-value
    1. 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 }}
Enter fullscreen mode Exit fullscreen mode

⚠️ 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
Enter fullscreen mode Exit fullscreen mode

🔹 Step 2: Authenticate GitHub CLI

gh auth login
Enter fullscreen mode Exit fullscreen mode
  • 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"
Enter fullscreen mode Exit fullscreen mode

✅ 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)