How can I quickly fetch or grab sfdx auth url?

When we need to know sfdx auth url to transfer it to another machine or use as CI/CD Secret key in Github or Gitlab, and we have multiple environments, like 10+ different Developer Edition, Sandboxes and Devhub environments which are needed in CI/CD process, this requires a long time to manually execute sfdx force:org:display --verbose command for every environment and then manually grab and find SFDX Auth URL from the response to copy and paste it into Secrets.

Is there a way to automate this or make it more easy to grab the SFDX Auth URL from the command response?

Yes! Actually we can leverage JQ tool to quickly grab the SFDX Auth URL from the command using --json switch. I have posted this solution into SSE.

Still we need to authorize the env

sfdx force:auth:web:login -a al -r https://test.salesforce.com for sandbox or skip -r parameter for production/Developer Edition environment and substitute al with actual desired alias for the org; and then use --json parameter and pass the result to jq command

sfdx force:org:display -u al --verbose --json | jq '.result.sfdxAuthUrl' -r

To install jq, use brew install jq on MacOS.

Sample sh script to be used for that

echo "$(sfdx force:org:display -u $1 –verbose –json | jq '.result.sfdxAuthUrl' -r)"
view raw grab.sh hosted with ❤ by GitHub

and can be used like ./grab.sh al

where al is your alias.

This entry was posted in CI/CD, sfdx and tagged , , , . Bookmark the permalink.

Leave a comment