Properly managing secrets like API keys, access tokens, and other credentials is a critical aspect of security. This guide explains the best practice of using GitHub Actions secrets to handle sensitive information.
Never commit secrets directly to your repository. Once a secret is in your git history, it should be considered compromised, even if you later remove it from the current version of the code. A public repository is visible to everyone, and malicious actors actively scan for exposed credentials.
Leaked secrets can lead to:
GitHub provides a secure way to store and use secrets within your repository for use in GitHub Actions workflows.
NPM_TOKEN
, AWS_ACCESS_KEY_ID
).
This name will be used to reference the secret in your workflow files.Once a secret is stored, you can access it in your workflow .yml
files using
the secrets
context.
Here’s an example of a workflow step that uses a secret to publish a package to the npm registry:
- name: Publish to npm
uses: actions/setup-node@v3
with:
node-version: "18"
registry-url: "https://registry.npmjs.org"
- run: npm publish
env:
NODE_AUTH_TOKEN: $
In this example:
secrets.NPM_TOKEN
refers to a repository secret named NPM_TOKEN
.NODE_AUTH_TOKEN
environment
variable, which is used by the npm publish
command for authentication.By using GitHub Actions secrets, you can automate workflows that require access to sensitive information without compromising on security. It’s a fundamental practice for any project that interacts with external services.