Sub topic 7
Storing credentials with secrets and environments
This chapter assumes you know basic Git and GitHub usage, can read YAML, and have a Salesforce DX project. It also assumes you have seen a workflow file with jobs and steps. Authentication itself comes in the next chapter. Here the focus is where the credential lives and who can use it.
Why credentials need a managed home
A pipeline that deploys to Salesforce must log in. The login material, such as an SFDX auth URL, is as powerful as a password. Anyone who holds it can act as the integration user in that org.
Never put it in the repository. Git history is permanent, forks copy it, and every collaborator can read it. GitHub provides two features for this problem:
- Secrets are encrypted values stored in GitHub. A workflow reads them at run time.
- Environments are named deployment targets, such as
qaorproduction. An environment can hold its own secrets and can add protection rules.
Secret scopes
A secret can be stored at three levels. The level decides which workflows can read it.
| Scope | Where you set it | Who can read it | Typical use |
|---|---|---|---|
| Repository | Settings, Secrets and variables, Actions | Every workflow in the repository | Values shared across all jobs, such as a tool token |
| Environment | Settings, Environments, then the environment | Only jobs that declare that environment | Org credentials, one per org |
| Organization | Organization settings | Repositories you choose | Values shared by many repositories |
For Salesforce, environment secrets are usually the best choice for org credentials. Every environment can use the same secret name, for example SFDX_AUTH_URL, while the value differs. The workflow file then stays identical across orgs. Only the environment name changes.
A repository secret and an environment secret with the same name can both exist. In that case the environment secret takes precedence for jobs that declare the environment.
Creating a secret
You can add secrets in the web interface. You can also use the GitHub CLI, which keeps the value out of your browser history.
First, get the auth URL from an org you have already logged in to. The --verbose flag is required so the URL is shown.
sf org display --target-org my-sandbox --verbose --json > org-info.jsonThe sfdxAuthUrl field in that file holds the value. Treat the file as sensitive and delete it after use. Then store the value as an environment secret.
gh secret set SFDX_AUTH_URL --env qa < authurl.txt
rm authurl.txtReading from standard input avoids placing the value in your shell history. To store a repository-level secret instead, omit --env qa.
Using a secret in a workflow
A workflow reads a secret through the secrets context. The safest pattern is to map it into an environment variable for one step only, instead of exposing it for the whole job.
name: Validate and deploy to QA
on:
push:
branches: [develop]
jobs:
deploy-qa:
runs-on: ubuntu-latest
environment: qa
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate to QA
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.txtTwo details matter here.
- The line
environment: qamakes the job use theqaenvironment. Without it, the job cannot see environment secrets, andsecrets.SFDX_AUTH_URLresolves to an empty string. - The secret is mapped through
env:and read as$SFDX_AUTH_URLin the shell. Writing${{ secrets.SFDX_AUTH_URL }}directly inside therun:text pastes the value into the script before the shell runs. That works, but it is easier to leak through quoting mistakes and shell tracing.
How GitHub protects secret values
GitHub encrypts secrets at rest. In logs, it replaces any exact match of a secret value with ***. This masking has limits:
- It matches the exact string. If your script transforms the value, for example by encoding it in Base64 or splitting it, the result is not masked.
- Multi-line values are masked line by line in some cases. Registering derived values with
::add-mask::helps. - Masking is a safety net. It does not replace careful handling.
Avoid commands such as set -x or env in steps that hold secrets. Both print variables.
You can mask a derived value explicitly:
TOKEN_B64=$(echo -n "$SFDX_AUTH_URL" | base64 -w0)
echo "::add-mask::$TOKEN_B64"Secrets and pull requests from forks
By default, workflows triggered by a pull request from a fork do not receive repository secrets. This is intentional. Otherwise, anyone could open a pull request that changes the workflow to print the secret.
This affects Salesforce pipelines in practice. A check-only validation job that needs org access will fail on fork pull requests, because the auth URL is empty. For private team repositories this rarely matters. For public repositories, choose one of these approaches:
- Run only static checks, which need no secrets, on fork pull requests.
- Have a maintainer review the code and then push it to a branch inside the repository, where validation runs with secrets.
Avoid the pull_request_target trigger as a shortcut. It runs with secrets and in the context of the base branch. If the workflow then checks out and runs code from the fork, an attacker can run arbitrary commands with your credentials.
Environments and protection rules
An environment is more than a place to keep secrets. You configure these options in Settings, then Environments.
- Required reviewers. The job pauses until a listed person approves it. Use this for production.
- Wait timer. The job waits a set number of minutes before starting.
- Deployment branches. Only chosen branches, such as
main, can deploy to this environment. A feature branch cannot reach production secrets even if someone edits the workflow. - Environment variables. Non-secret settings, such as an org alias or a test level, can be stored with the environment.
Secrets for an environment are released to the job only after its protection rules pass. That ordering is the control. A job targeting production does not receive the production auth URL until a reviewer approves.
One environment per org
A common layout maps one environment to each org in your path to production.
| Environment | Org | Protection |
|---|---|---|
| `ci` | Integration sandbox | Branch rule for `develop` and pull request branches |
| `qa` | QA sandbox | Branch rule for `develop` |
| `production` | Production | Branch rule for `main`, required reviewers |
Each holds its own SFDX_AUTH_URL. Later chapters use this layout for promotion across orgs.
Least privilege for the integration user
The secret is only as safe as the account behind it. Create a dedicated Salesforce user for the pipeline instead of using a person's login.
- Give it a profile or permission set that allows deployment, such as the API Enabled and Modify Metadata permissions, and nothing broader than needed.
- Use a separate user for production and for each sandbox, so a leaked sandbox credential cannot touch production.
- Set login IP ranges or connected app policies where your organization supports them. GitHub-hosted runners use wide, changing IP ranges, so IP restrictions often need self-hosted runners.
- Plan rotation. Sandbox refreshes invalidate stored auth URLs, which makes the pipeline fail with an authentication error until you replace the secret.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Authentication step fails and the secret appears empty | The job has no `environment:` line, or the secret was saved in a different scope | Add `environment:` or move the secret to the right scope |
| Works on push, fails on pull request from a fork | Fork pull requests receive no secrets | Use a branch inside the repository |
| `invalid_grant` or expired token after a sandbox refresh | The stored auth URL points at a replaced org | Log in again and update the secret |
| Job waits indefinitely | Required reviewer has not approved | Approve in the run page, or review the rule |
| Deployment rejected for branch | Environment deployment branch rule excludes the branch | Run from an allowed branch, or adjust the rule deliberately |
Exercise
Your workflow has a job named deploy that runs sf org login sfdx-url with ${{ secrets.SFDX_AUTH_URL }}. The secret exists only as an environment secret on production. The step fails because the file is empty. What is the most likely problem, and how do you fix it? Also name one protection rule you would add to production.
Show answer
The job does not declare environment: production, so it cannot read environment secrets and the value is empty. Add environment: production to the job. A good protection rule is required reviewers, so the secret is released only after a person approves. A deployment branch rule limited to main is also appropriate.
Exercise
A teammate proposes storing the production auth URL as a repository secret so every workflow can use it. Give two reasons environment secrets are the better choice.
Show answer
First, an environment secret is available only to jobs that declare that environment, so feature-branch workflows cannot read production credentials. Second, environment protection rules such as required reviewers and deployment branch restrictions apply before the secret is released, which a repository secret cannot enforce.