Sub topic 7
Storing credentials with secrets and environments
This chapter assumes you can write a basic workflow, know what a Salesforce org alias is, and have a Salesforce DX project in a GitHub repository. You will learn where a pipeline should keep its org credentials and how to limit which jobs can read them.
Why credentials need a special home
A pipeline needs to log in to an org. The login value, such as an SFDX auth URL, works like a password. If it sits in a YAML file or in the repository, anyone with read access can use it. Git history also keeps it forever. GitHub provides secrets for this purpose.
A secret is an encrypted value stored in GitHub and exposed to workflows only when a step asks for it. GitHub masks the value in logs, so it appears as ***.
Three places to store a secret
| Level | Visible to | Typical use |
|---|---|---|
| Repository secret | Every workflow in the repository | Values shared by all orgs, such as a token for a code scanner |
| Environment secret | Only jobs that name that environment | One org login per org: sandbox, staging, production |
| Organization secret | Repositories you choose | Values shared across many repositories |
Use environment secrets for org logins. Each environment holds a secret with the same name, SFDX_AUTH_URL, but a different value. The job picks the org by naming the environment.
Environments
An environment is a named deployment target, such as qa or production. It can hold its own secrets and carry protection rules:
- Required reviewers must approve before the job starts.
- A wait timer delays the job.
- Deployment branches restrict which branches may use the environment. Limit
productiontomain.
Create environments under Settings > Environments in the repository. Add the secret inside each environment, not at the repository level.
Using a secret in a workflow
The job below names the production environment. GitHub pauses it until a reviewer approves, then makes that environment's secrets available.
name: Deploy to production
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- name: Authenticate to production
env:
SFDX_AUTH_URL: ${{ secrets.SFDX_AUTH_URL }}
run: |
echo "$SFDX_AUTH_URL" > authurl.txt
sf org login sfdx-url --sfdx-url-file authurl.txt --alias target --set-default
rm authurl.txtThe secret is passed through env and read as a shell variable. Do not write ${{ secrets.SFDX_AUTH_URL }} directly inside the run script. GitHub would substitute the value into the script text before the shell runs, which is harder to quote safely.
Common mistakes
- Putting the secret at repository level, so a pull request workflow can reach the production login.
- Printing the auth URL while debugging. Masking can fail if the value is changed, for example by encoding it.
- Leaving
authurl.txton the runner. Delete it right after login, as the example does. - Forgetting that workflows from forked pull requests do not receive secrets.
Exercise
Your repository has qa and production environments. Both need a secret for the org login. Where should you store each value, and what name should the secrets have?
Show answer
Store each value as an environment secret inside its own environment, not at repository level. Use the same name, such as SFDXAUTHURL, in both. The job's environment: key then decides which value it receives, so the workflow file stays identical apart from the environment name.