Here is a short Bitbucket pipeline script that solves the purpose
image: salesforce/salesforcedx:latest-full
definitions:
steps:
- step: &verifyInScratchOrg
name: Create scratch org, deploy & test metadata
script:
- echo $DEV_HUB_URL > devHubURLFile
- sfdx force:auth:sfdxurl:store -d -f devHubURLFile
- rm devHubURLFile
- chmod +x ./scripts/org/verificationScript.sh
- ./scripts/org/verificationScript.sh
pipelines:
pull-requests:
feature/*:
- step: *verifyInScratchOrg
bugfix/*:
- step: *verifyInScratchOrg
To use this script you need to configure a Secured Repository variable, DEV_HUB_URL to connect to Dev Hub.
Steps to generate Auth URL:
- Authorize the org whose auth URL you need using
sfdx force:auth:web:login -a OrgAlias
- Log in with your credentials and authorize the Global Connected App.
- Once you've successfully authorized the org, close that browser tab and run
sfdx force:org:display -u <OrgAlias> --verbose
command (substituting the appropriate OrgAlias).
- This will display the Auth URL for the org. You can then add that as a secured repository variable in your Bitbucket pipeline to connect to that org.
The above script will run whenever a Pull request is created from any feature branch or bugfix branch. It merges the destination branch into your working branch before it runs, ensuring the target branch doesn't fail when code is merged giving reviewer more confidence
I have provided a relative path to Scratch ORG creation script which is part of the repository, a sample scratch org creation script can be
#!/bin/bash
source `dirname $0`/config.sh
execute() {
$@ || exit
}
echo "deleting old scratch org"
sfdx force:org:delete -p -u $SCRATCH_ORG_ALIAS
echo "Creating scratch ORG"
sfdx force:org:create -a $SCRATCH_ORG_ALIAS -s -f ./config/project-scratch-def.json -d 1
echo "Pushing changes to scratch org"
execute "sfdx force:source:push"
echo "Assigning permission"
execute "sfdx force:user:permset:assign -n Admin"
echo "Running apex tests"
execute "sfdx force:apex:test:run -l RunLocalTests -w 30"
echo "Running jest tests"
npm install
execute "npm run test:unit"
- I am creating a temporary dev hub URL file because force:auth:sfdxurl:store only accepts a file name as parameter and Auth URL should never be part of the repository and should be configurable by repository admins
- I am also setting the permission for executable script file to avoid permission exceptions