Sub topic 15
Authorizing a Dev Hub in automation
This chapter assumes you have already enabled Dev Hub, authorized it once with sf org login web, and created a scratch org by hand. It also assumes you know how to store secrets in your CI system.
Why the browser login does not work in CI
sf org login web opens a browser and waits for a person to sign in. A CI job has no browser and no person. It needs a login that runs without prompts and uses a credential kept in the CI system's secret store.
Choosing a method
| Method | Command | Credential | Best for |
|---|---|---|---|
| JWT bearer flow | `sf org login jwt` | Private key plus a connected app | Shared team pipelines |
| SFDX auth URL | `sf org login sfdx-url` | A URL containing a refresh token | Quick setups and personal projects |
JWT is the usual choice for teams. The key is not tied to one person's session, and you can revoke access from the connected app.
Setting up the JWT flow
Do this once, as an administrator.
- Generate a private key and a certificate:
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt. - In the Dev Hub org, create a connected app. Turn on OAuth, choose "Use digital signatures", and upload
server.crt. - Add the scopes
apiandrefresh_token, offline_access. - Set the app to "Admin approved users are pre-authorized". Then assign it to the CI user through a permission set or profile.
- Copy the app's consumer key. Store it, the key file contents, and the CI username as secrets.
Never commit server.key. Anyone who holds it and the consumer key can log in as the CI user.
A CI script
The script writes the key to a temporary file and logs in. It creates a short-lived scratch org, then deletes that org when the script exits.
#!/usr/bin/env bash
set -euo pipefail
KEY_FILE="$(mktemp)"
trap 'rm -f "$KEY_FILE"; sf org delete scratch --target-org ci-scratch --no-prompt || true' EXIT
printf '%s' "$SF_JWT_KEY" > "$KEY_FILE"
sf org login jwt \
--client-id "$SF_CLIENT_ID" \
--jwt-key-file "$KEY_FILE" \
--username "$SF_DEVHUB_USERNAME" \
--instance-url "https://login.salesforce.com" \
--set-default-dev-hub \
--alias ci-hub
sf org create scratch \
--definition-file config/project-scratch-def.json \
--target-dev-hub ci-hub \
--alias ci-scratch \
--duration-days 1 \
--wait 10
sf project deploy start --target-org ci-scratchThe trap runs on success and on failure, so a failed build does not leave an orphaned scratch org using one of your active slots. The || true keeps cleanup from hiding the real error if creation never succeeded.
Using an auth URL instead
On a machine where you are already logged in, run sf org display --verbose --target-org my-hub. The output includes an "Sfdx Auth Url". Save that value as a secret. In CI, write it to a file and run sf org login sfdx-url --sfdx-url-file authfile.txt --set-default-dev-hub --alias ci-hub.
Common errors
invalid_grant: user hasn't approved this consumer: the CI user is not assigned to the connected app. Add the permission set or profile.invalid_client_id: the consumer key is wrong, or the app has not finished propagating. Wait a few minutes and retry.--instance-urlmismatch: usehttps://test.salesforce.comfor sandboxes, or your My Domain URL if login is restricted to it.- Limit errors on create: failed jobs that skipped cleanup have used up your active scratch orgs. Check
sf limits api display --target-org ci-hub.
Exercise
Your pipeline fails during scratch org creation, and the next run reports that the active scratch org limit is reached. What in the script prevents this, and why does it work even when the job fails?
Show answer
The trap ... EXIT line runs sf org delete scratch --no-prompt whenever the script ends, whether it succeeded or failed. This frees the Dev Hub's active scratch org slot. The || true stops the cleanup command from masking the original error when the org was never created.