Skip to main content
In this guide, you’ll learn how to manage encrypted variables in GitHub—commonly known as GitHub Secrets—to keep API keys, tokens, and credentials safe. We’ll cover what secrets are, how to set them up, use them in workflows, and follow best practices for secure automation.

What Are GitHub Secrets?

GitHub Secrets are encrypted environment variables stored at the repository, environment, or organization level. They enable you to reference sensitive data in your Actions workflows without exposing them in code.

Viewing Secrets and Variables

To inspect secrets in a repository:
  1. Navigate to SettingsSecrets and variables.
  2. Choose Actions, Codespaces, or Dependabot.
The image shows a GitHub repository settings page for "Actions secrets and variables," displaying a repository secret named "AZURE_WEBAPP_PUBLISH_PROFILE."

Variables vs. Secrets

  • Secrets are encrypted and masked in logs.
  • Variables hold non-sensitive data (e.g., server names) and can be updated centrally.
The image shows a GitHub repository settings page for "Actions secrets and variables," with options to manage secrets and variables. The "Variables" tab is selected, and there are no repository variables currently set.

Creating and Updating Repository Secrets

  1. Go to SettingsSecrets and variablesActions.
  2. Click New repository secret.
  3. Enter a Name (e.g., API_KEY) and the secret Value.
  4. Click Add secret.
The image shows a GitHub interface where a user is adding a new secret under "Actions secrets" in the settings of a project. The fields for "Name" and "Secret" are being filled out.
Once created, the secret appears in the list—its value remains hidden:
The image shows a GitHub repository settings page for managing "Actions secrets and variables," with an "API_KEY" listed as a repository secret.
To update a secret, click Edit, provide a new value, and re-authenticate if prompted.

Using Secrets in a Workflow

Add secrets to your workflow YAML to inject them at runtime. Create a file like .github/workflows/hello.yml:
Here, ${{ secrets.API_KEY }} retrieves the value securely.
The image shows a GitHub Actions setup page for a repository, offering options to configure workflows such as a simple workflow or deployment to various cloud services.
Commit the workflow and trigger it manually or on push. GitHub masks the secret in logs, replacing characters with ***, while your external endpoint receives the correct token.
The image shows a GitHub Actions interface with a workflow file named main.yml and two recent workflow runs. The interface includes options for managing workflows and running them manually.
Secrets are not exposed to workflows triggered by pull requests from forks. This prevents unauthorized access to your credentials.

Advanced GitHub Secrets Usage

Deploying to Azure with JSON Credentials

Store full JSON service principals in a secret and use them:
GitHub automatically masks secrets in Action logs, so your credentials never appear in plaintext.

Automating Secret Rotation

Use a scheduled workflow to rotate keys monthly:

Auditing Secret Usage

Log each secret access for compliance:

Best Practices for GitHub Secrets

The image shows a list of best practices for managing secrets in code, including limiting exposure, using short-lived tokens, and enabling secret scanning.
  • Limit access with fine-grained permissions.
  • Use short-lived tokens or ephemeral credentials.
  • Never commit secrets to code or configuration files.
  • Require approvals for environment secrets in production.
  • Rotate and audit secrets regularly.
  • Enable GitHub Secret Scanning.
  • Train your team on secure secret handling.

References

Watch Video