Sub topic 8
Authenticating the workflow to your org
This chapter assumes you can read a GitHub Actions workflow, you have the sf CLI installed on the runner, and you know that secrets are stored in GitHub and injected as environment variables. Here you will make the runner log in to a Salesforce org without a person typing a password.
What authentication means on a runner
A GitHub-hosted runner is a fresh virtual machine for every job. It has no saved org logins, and nobody can complete a browser login on it. Each run must therefore authenticate from scratch, non-interactively, using a credential stored as a secret.
The sf CLI supports two non-interactive methods that suit pipelines:
| Method | Command | Credential you store | Setup effort |
|---|---|---|---|
| SFDX auth URL | `sf org login sfdx-url` | One string containing a refresh token | Low |
| JWT bearer flow | `sf org login jwt` | A private key, a consumer key, and a username | Higher |
Both end in the same state. The CLI has an authorized org under an alias, and later sf commands use it through --target-org.
Method 1: the SFDX auth URL
An SFDX auth URL is a string that starts with force://. It encodes the connected app client ID, a refresh token, and the instance URL. Anyone holding it can act as the user who created it, so treat it like a password.
You create it once on your own machine, after logging in to the org normally:
sf org login web --alias my-prod --instance-url https://login.salesforce.com
sf org display --target-org my-prod --verboseThe --verbose output includes a Sfdx Auth Url row. Copy that value into a GitHub secret named SFDX_AUTH_URL. For a sandbox, log in with --instance-url https://test.salesforce.com so the URL points at the right host.
In the workflow, write the secret to a temporary file, log in, and delete the file:
- name: Authenticate with SFDX auth URL
env:
SFDX_AUTH_URL: ${{ secrets.SFDX_AUTH_URL }}
run: |
umask 077
echo "$SFDX_AUTH_URL" > "$RUNNER_TEMP/authurl.txt"
sf org login sfdx-url --sfdx-url-file "$RUNNER_TEMP/authurl.txt" --alias target --set-default
rm "$RUNNER_TEMP/authurl.txt"The secret goes through env rather than being written inline as ${{ secrets.X }} inside the script. This keeps the value out of the generated shell text. GitHub also masks secret values in logs, but masking is a safety net and not a control you should rely on.
Limits of the auth URL
- It represents one person's refresh token. If that user is deactivated, loses permissions, or has sessions revoked, the pipeline breaks.
- The refresh token is subject to the connected app's refresh token policy. A policy such as "expire refresh token after 90 days" will eventually stop the login.
- The user who created it may be a developer with broad access. Prefer a dedicated integration user.
Method 2: the JWT bearer flow
The JWT bearer flow avoids storing a refresh token. The runner proves its identity by signing a short-lived token with a private key. Salesforce checks the signature against a certificate you uploaded to a connected app.
The setup has four parts:
- Generate a key pair and certificate.
- Create a connected app (or external client app) in the org with "Use digital signatures" enabled, and upload the certificate.
- Give the app the OAuth scopes
apiandrefresh_token, offline_access, or at least the scopes your commands need. - Pre-authorize the integration user, for example by setting the app's policy to "Admin approved users are pre-authorized" and assigning a permission set or profile to the app.
Generate the key and certificate locally:
openssl req -x509 -nodes -newkey rsa:2048 -sha256 -days 365 \
-keyout server.key -out server.crt \
-subj "/CN=salesforce-ci"Upload server.crt to the connected app. Store the contents of server.key as a secret named SF_JWT_KEY. Store the app's consumer key as SF_CLIENT_ID and the integration username as SF_USERNAME. Never commit server.key.
A complete job using these values:
name: JWT login check
on:
workflow_dispatch:
jobs:
login:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate with JWT
env:
JWT_KEY: ${{ secrets.SF_JWT_KEY }}
CLIENT_ID: ${{ secrets.SF_CLIENT_ID }}
SF_USER: ${{ secrets.SF_USERNAME }}
run: |
umask 077
printf '%s\n' "$JWT_KEY" > "$RUNNER_TEMP/server.key"
sf org login jwt \
--client-id "$CLIENT_ID" \
--jwt-key-file "$RUNNER_TEMP/server.key" \
--username "$SF_USER" \
--instance-url https://login.salesforce.com \
--alias target \
--set-default
rm "$RUNNER_TEMP/server.key"
- name: Confirm the connection
run: sf org display --target-org targetThe --instance-url must match the org type. Use https://login.salesforce.com for production and developer orgs, and https://test.salesforce.com for sandboxes. A My Domain URL also works.
How the login flows
Choosing between them
Use the auth URL when you want a working pipeline quickly, for a sandbox or a small team. Use JWT for production and for any pipeline that must keep running unattended. JWT does not depend on a refresh token that can expire, and you can rotate the certificate on a schedule without anyone re-logging in.
Scope credentials to the environment they belong to. Store the production auth URL or key in the production GitHub environment, and the sandbox values in a separate environment. A job only sees an environment's secrets when it declares that environment, which is how an approval gate protects the production credential.
Common errors
| Symptom in the log | Likely cause | Fix |
|---|---|---|
| `invalid_grant: expired access/refresh token` | Auth URL refresh token revoked or expired | Generate a new auth URL and update the secret |
| `user hasn't approved this consumer` | JWT user is not pre-authorized for the connected app | Set the policy to admin approved and assign a permission set |
| `invalid_client_id` | Wrong consumer key, or the app was just created | Check `SF_CLIENT_ID`. Wait a few minutes after creating the app |
| `Cannot read the private key` or a signing error | Key stored with broken line breaks, or the wrong key | Re-paste the full PEM including the BEGIN and END lines |
| Login succeeds against the wrong org | Wrong `--instance-url` or secret used in the wrong environment | Verify with `sf org display` and check the job's `environment:` |
| `No authorization information found for target` | Alias misspelled in a later step | Use the same alias in `--alias` and `--target-org` |
When a login fails, add a step that runs sf org display --target-org target --json only after a successful login. Do not echo secret variables to debug. If you must inspect a value, compare its length or first characters, never the whole string.
Exercise
Your JWT login fails with "user hasn't approved this consumer", although the certificate and consumer key are correct. What do you change in Salesforce, and why does a correct key not fix it?
Show answer
Pre-authorize the integration user for the connected app. Set the app's OAuth policy to "Admin approved users are pre-authorized" and assign the user a profile or permission set that grants access to the app. The JWT flow has no interactive consent screen, so the key and certificate only prove who is calling. Salesforce also requires that the user was approved for that app in advance.
Exercise
Why does the workflow pass secrets through the step's env block and delete the temporary key or auth URL file after login?
Show answer
The env block keeps the secret value out of the script text, which reduces the chance it appears in logs or shell traces. Deleting the file removes the credential from the runner's disk, so later steps (including third-party actions) cannot read it from there.