Sub topic 8
Authenticating the workflow to your org
A GitHub runner starts empty each time, so it has no login to your Salesforce org. This chapter covers the two ways to give it one without a person typing a password. It assumes you know GitHub secrets and environments from the previous chapter, and that you can run sf commands locally.
What the runner needs
The runner must log in non-interactively, and the credential must stay out of the repository and the logs. The Salesforce CLI supports two methods for this.
| Method | Credential | Setup effort | Best for |
|---|---|---|---|
| SFDX auth URL | One string containing a refresh token | Low | Getting started, sandboxes |
| JWT bearer flow | Private key plus Connected App consumer key | Higher | Production, teams |
Option 1: SFDX auth URL
An SFDX auth URL is a string that encodes the org's instance URL, client ID, and a refresh token. Anyone holding it can act as the user who created it.
Log in to the org locally, then print the URL:
sf org login web --alias target
sf org display --verbose --target-org targetCopy the value labelled Sfdx Auth Url into a secret named SFDX_AUTH_URL. The workflow step then looks like this:
- name: Authenticate to the target org
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 URL is read from a file because the CLI does not accept it as a plain argument. Deleting the file right after login keeps it from lingering on the runner.
Option 2: JWT bearer flow
With JWT, you create a Connected App in the org and upload a certificate. The runner signs a token with the matching private key, so no refresh token is stored. You keep three values as secrets: the private key, the consumer key, and the username.
- name: Authenticate with JWT
env:
SF_JWT_KEY: ${{ secrets.SF_JWT_KEY }}
SF_CLIENT_ID: ${{ secrets.SF_CLIENT_ID }}
SF_USERNAME: ${{ secrets.SF_USERNAME }}
run: |
echo "$SF_JWT_KEY" > server.key
sf org login jwt \
--client-id "$SF_CLIENT_ID" \
--jwt-key-file server.key \
--username "$SF_USERNAME" \
--instance-url https://login.salesforce.com \
--alias target \
--set-default
rm server.keyUse https://test.salesforce.com as the instance URL for sandboxes. The Connected App must pre-authorize the user, usually through a permission set.
Common errors
invalid_grantor expired token: the auth URL's refresh token was revoked, or the user's password was reset. Generate a new URL.user hasn't approved this consumer: the Connected App does not pre-authorize the user. Assign a permission set to the app.- Login works locally but not in CI: the secret has stray whitespace or is scoped to a different environment than the job uses.
Exercise
Why does the JWT flow suit production better than an auth URL, and which three values must you store as secrets?
Show answer
JWT stores no long-lived refresh token, and you can revoke access by changing the Connected App's settings or rotating the certificate. You store the private key, the Connected App consumer key, and the integration user's username.