Lesson 50 of 60 – GitHub Secrets
83%

GitHub Secrets

GitHub Secrets provide a secure way to store sensitive values that are needed by GitHub Actions workflows. Examples include API keys, passwords, access tokens, and deployment credentials.

Note: Sensitive credentials should not be written directly inside source code or workflow files. GitHub Secrets can be used to provide sensitive values to workflows when they are needed.

1. What Are GitHub Secrets?

GitHub Secrets are encrypted values that can be used by GitHub Actions workflows without placing the sensitive value directly in the workflow file.

Secret
  ↓
GitHub
  ↓
Workflow
  ↓
Application / Command

2. Why Use Secrets?

Secrets help keep sensitive credentials separate from normal project source code.

  • Protect API keys
  • Protect passwords
  • Store deployment credentials
  • Protect access tokens
  • Provide private configuration values
  • Reduce accidental exposure of credentials

3. Examples of Sensitive Values

API_KEY
DATABASE_PASSWORD
DEPLOY_TOKEN
ACCESS_TOKEN
SECRET_KEY

These types of values should normally be handled carefully and should not be committed as plain text into a repository.

4. Secret vs Normal Variable

Secret Normal Variable
Used for sensitive values Used for normal configuration
Should be protected Can often be visible in workflow configuration
Example: API token Example: application name

5. Creating a Repository Secret

Repository administrators can add secrets through the repository's GitHub settings.

Repository
    ↓
Settings
    ↓
Secrets and variables
    ↓
Actions
    ↓
New repository secret

The exact interface can change, but the purpose is to store a sensitive value securely for workflows.

6. Secret Name

A secret has a name that is used to reference it from a workflow.

API_KEY

Use clear and meaningful names for secrets so that workflow configuration remains understandable.

7. Secret Value

The secret value is the sensitive information associated with the secret name.

Name:
API_KEY

Value:
your-sensitive-api-key

The actual sensitive value should not be placed directly in the workflow source code.

8. Referencing a Secret

A GitHub Actions workflow can reference a secret using the secrets context.

${{ secrets.API_KEY }}

Here, API_KEY is the name of the stored secret.

9. Using Secrets as Environment Variables

A secret can be provided to a workflow step through an environment variable.

steps:

  - name: Run Application
    run: npm start
    env:
      API_KEY: ${{ secrets.API_KEY }}

The application can then read the environment variable according to its programming language.

10. Example with a Database Password

steps:

  - name: Run Migration
    run: php migrate.php
    env:
      DB_PASSWORD: ${{ secrets.DB_PASSWORD }}

The workflow can provide the password to the command without writing the password directly into the workflow file.

11. Repository Secrets

Repository secrets are associated with a particular repository and can be used by workflows in that repository when permitted.

Repository
     ↓
Repository Secret
     ↓
Workflow
     ↓
Job / Step

12. Organization Secrets

Organizations can manage secrets that can be made available to selected repositories according to the organization's configuration.

Organization
      ↓
Organization Secret
      ↓
Selected Repositories
      ↓
Workflows

This can be useful when multiple repositories need access to the same type of sensitive configuration.

13. Environment Secrets

GitHub environments can also have secrets associated with them. This can help separate values used for different environments.

Development
    ↓
Development Secret

Production
    ↓
Production Secret

This is useful when deployment environments require different credentials.

14. Development and Production Secrets

Development
DB_PASSWORD = Development Value

Production
DB_PASSWORD = Production Value

Using separate environment-specific values helps prevent accidentally using the wrong credentials in a deployment environment.

15. Secrets in Pull Request Workflows

Workflows triggered by pull requests can have restrictions around access to secrets, especially when code comes from outside the repository.

Pull Request
     ↓
Workflow
     ↓
Security Rules
     ↓
Secret Access

This is important because untrusted code should not automatically receive access to sensitive credentials.

16. Secrets and Forks

When workflows are triggered from forked repositories, secret access is restricted to help protect the secrets of the original repository.

Original Repository
       ↓
Secret
       ↓
Forked Code
       ↓
Restricted Secret Access

Always consider the security implications of running untrusted code in workflows.

17. Secret Masking

GitHub attempts to prevent recognized secret values from being displayed directly in workflow logs.

Secret:
my-secret-value

Log:
***

However, masking should not be treated as permission to intentionally print secrets in logs.

18. Never Print Secrets

A workflow should not intentionally print sensitive values.

# Avoid

- run: echo "${{ secrets.API_KEY }}"

Even when GitHub provides masking, exposing secrets unnecessarily is a poor security practice.

19. Secrets in Source Code

Never place real passwords or API keys directly in source code.

// Bad practice

const API_KEY = "real-secret-key";

Instead, use environment variables or a suitable secret-management solution.

20. Secrets in Workflow Files

Do not write real credentials directly into workflow YAML files.

# Bad

env:
  API_KEY: "real-secret-value"

Use the GitHub Secrets context instead:

env:
  API_KEY: ${{ secrets.API_KEY }}

21. Using a Secret in a Command

steps:

  - name: Deploy
    run: ./deploy.sh
    env:
      DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}

The deployment script can read the environment variable without the token being written directly into the workflow configuration.

22. API Key Example

name: API Test

on:
  workflow_dispatch:

jobs:

  test:
    runs-on: ubuntu-latest

    steps:

      - uses: actions/checkout@v4

      - name: Run API Test
        run: python test_api.py
        env:
          API_KEY: ${{ secrets.API_KEY }}

The API key is supplied to the step through an environment variable.

23. Rotating Secrets

Credentials should be replaced or rotated according to the security requirements of the service using them.

Old Secret
    ↓
Replace / Rotate
    ↓
New Secret
    ↓
Update GitHub Secret
    ↓
Workflow Uses New Value

24. If a Secret Is Exposed

If a sensitive credential is accidentally exposed, do not simply remove it from the visible file and assume the problem is solved.

  • Revoke or rotate the exposed credential.
  • Replace the secret with a new value.
  • Review where the secret was exposed.
  • Check relevant logs and repository history.
  • Investigate how the exposure happened.

25. Secret Management Best Practices

  • Never commit secrets to source code.
  • Use GitHub Secrets for workflow credentials.
  • Use meaningful secret names.
  • Give workflows only the access they need.
  • Do not print secrets in logs.
  • Review secret access regularly.
  • Rotate credentials when necessary.
  • Use separate values for different environments.
  • Be careful with workflows triggered by untrusted code.
  • Remove or replace exposed credentials immediately.

26. Common Secrets Mistakes

  • Hard-coding API keys.
  • Committing passwords.
  • Printing secrets in workflow logs.
  • Using production credentials unnecessarily.
  • Giving workflows excessive access.
  • Ignoring secrets exposed in commits.
  • Using the same credential everywhere without proper controls.
  • Forgetting to rotate exposed credentials.

27. Example Deployment Workflow

name: Deploy

on:
  workflow_dispatch:

jobs:

  deploy:
    runs-on: ubuntu-latest

    steps:

      - uses: actions/checkout@v4

      - name: Deploy Application
        run: ./deploy.sh
        env:
          DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}

The deployment token is supplied to the deployment step through the secret context.

28. Complete Secret Workflow

Create Secret
      ↓
Store in GitHub
      ↓
Workflow Starts
      ↓
Secret Referenced
      ↓
Environment Variable
      ↓
Command / Action
      ↓
Application Uses Credential
      ↓
Secret Not Stored in Source Code

29. GitHub Secrets Checklist

  • Identify sensitive values.
  • Create appropriate GitHub Secrets.
  • Use clear secret names.
  • Reference secrets through the secrets context.
  • Avoid printing secrets.
  • Do not commit credentials.
  • Review workflow access.
  • Protect production credentials.
  • Rotate credentials when necessary.
  • Replace exposed credentials immediately.

30. Summary of GitHub Secrets

GitHub Secrets provide a secure mechanism for storing sensitive values used by GitHub Actions workflows. They can be referenced through the secrets context and passed to workflow steps when required.

Secret
  ↓
GitHub
  ↓
Workflow
  ↓
Environment Variable
  ↓
Application / Deployment
  ↓
Secure Automation

Using secrets correctly helps prevent passwords, API keys, tokens, and other sensitive credentials from being stored directly in workflow files or application source code.

📌 Key Points

  • GitHub Secrets store sensitive values for workflows.
  • Secrets can contain API keys, passwords, tokens, and credentials.
  • Secrets should not be written directly into source code.
  • Secrets can be referenced using ${{ secrets.NAME }}.
  • Secrets can be passed to workflow steps through environment variables.
  • Repository, organization, and environment secrets can be used for different scopes.
  • Workflows triggered by untrusted code require careful secret handling.
  • Secrets should not be intentionally printed in workflow logs.
  • Exposed credentials should be revoked or rotated.
  • Production credentials should be protected carefully.
  • Least-privilege access should be used when possible.
  • Good secret management is an important part of secure CI/CD workflows.

🧠 Quick Quiz

Question: What is the main purpose of GitHub Secrets?