Prerequisite For CI/CD Implimentatation In Salesfroce:
- Salesforce production org
- Connected app/Consumer key
- Gitlab server
- Gitlab-runner
- Metadata package ID
- Self-signed SSL certificate
In this blog, we will learn how to implement CI/CD with SFDX using GitLab. To start CI/CD in salesforce DX, we required a connected app for JWT-based authentication to our Salesforce Org. We can create a connected app in our Salesforce Org.
To create a connected app in your org, follow the official Salesforce DX Developer Guide link :
After creating a connected app we can test our connection with our packaging org by running the command with the below-mentioned parameters.
Packaging_Org_CONSUMER_KEY (Client ID)
Packging_Org_USERNAME (User name of your org)
JWT_KEY_FILE (Self-signed Key).
“sfdx force:auth:jwt:grant –clientid ${Packaging_Org_CONSUMER_KEY} –username ${Packging_Org_USERNAME} \ –jwtkeyfile ${JWT_KEY_FILE} –setdefaultdevhubusername”
After creating a connected app we required a script with SFDX commands to start a pipeline written in YAML with the name .gitlab-ci.yml. Basicaly a .gitlab-ci.yml file contains three jobs like Build,Test and deploy.
In this script, we will write our sfdx commands in the script section as per our production requirement to start continuous integration on salesforce. As per our production requirement, I have created a script to test, deploy and release the salesforce app versions.
Content of .gitlab-ci.yml is here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
stages: - test - deploy test: stage: test script: - sfdx force:auth:jwt:grant --clientid ${Pack_Org_CONSUMER_KEY} --username ${Pack_Org_USERNAME} --jwtkeyfile ${Pack_Org_JWT_KEY_FILE} - sfdx force:source:convert --outputdir ../mdipkg/ --packagename "your package name" - sfdx force:mdapi:deploy --deploydir ../mdipkg/ --targetusername ${Pack_Org_USERNAME} -w -1 - sfdx force:mdapi:deploy:report - sfdx force:apex:test:run -l RunLocalTests -d /home/gitlab-runner/builds/cd83014f/0/salesforce/mdipkg/classes -c -r human -u ${Pack_Org_USERNAME} -w 5 only: - staging - master tags: - your pipeline tag - your pipeline tag beta-release: stage: deploy script: - a=$(git log -1 --pretty=%B) - b=$(echo $a | cut -d " " -f 3 |grep -oE "[0-9]{1,3}.?[0-9]{1,3}") - echo $b - sfdx force:auth:jwt:grant --clientid ${Pack_Org_CONSUMER_KEY} --username ${Pack_Org_USERNAME} --jwtkeyfile ${Pack_Org_JWT_KEY_FILE} - a=$(sfdx force:package1:version:create --packageid ${METADATAPACKAGEID} --name "your package name" -v $b -u ${Pack_Org_USERNAME} -w 5 --json | python -c "import sys, json; print json.load(sys.stdin)['result']['Id']") - sfdx force:package1:version:create:get -i $a -u ${Pack_Org_USERNAME} only: - staging tags: - your pipeline tags - your pipeline tags managed-release: stage: deploy script: - sfdx force:auth:jwt:grant --clientid ${Pack_Org_CONSUMER_KEY} --username ${Pack_Org_USERNAME} --jwtkeyfile ${Pack_Org_JWT_KEY_FILE} - a=$(sfdx force:package1:version:create --packageid ${METADATAPACKAGEID} --name "your package name" -u ${Pack_Org_USERNAME} --managedreleased -w 5 --json | python -c "import sys, json; print json.load(sys.stdin)['result']['Id']") - sfdx force:package1:version:create:get -i $a -u ${Pack_Org_USERNAME} only: - master tags: - your pipeline tags - your pipeline tags |
Let’s know how the Script is working :
In the above script, I have created three jobs, in the first job test, I am running the command “sfdx force:auth:jwt:grant” to connect the production org.
Then converted sfdx source code to mdapi format using the command “sfdx force:source:convert”
Then deployed the project to the production org by running the command “sfdx force:mdapi:deploy” after deployment, I am running apex test command “sfdx force:apex:test:run” to check the code coverage.
Create a beta package:
In the second job beta-release first connects to the salesforce Org and runs “sfdx force:package1:version:create” command to release the beta version of the salesforce package which I deployed in my first job.
Create a managed package:
In the third job, the last job named managed release again runs “sfdx force:package1:version:create” command with the parameter “--managedreleased” to create a managed package.
If you have any queries, you can ask in the comment box.