Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Sample Jenkinsfile
A Jenkinsfile is a text file that contains the
definition of a Jenkins Pipeline. This Jenkinsfile
shows how to integrate Salesforce CLI commands to automate testing of your Salesforce
applications using scratch orgs.
The Jenkinsfile Walkthrough topic uses this sfdx-jenkins-package Jenkinsfile as an example.
1#!groovy
2
3import groovy.json.JsonSlurperClassic
4
5node {
6
7 def SF_CONSUMER_KEY=env.SF_CONSUMER_KEY
8 def SF_USERNAME=env.SF_USERNAME
9 def SERVER_KEY_CREDENTALS_ID=env.SERVER_KEY_CREDENTALS_ID
10 def TEST_LEVEL='RunLocalTests'
11 def PACKAGE_NAME='0Ho1U000000CaUzSAK'
12 def PACKAGE_VERSION
13 def SF_INSTANCE_URL = env.SF_INSTANCE_URL ?: "https://login.salesforce.com"
14
15 def toolbelt = tool 'toolbelt'
16
17
18 // -------------------------------------------------------------------------
19 // Check out code from source control.
20 // -------------------------------------------------------------------------
21
22 stage('checkout source') {
23 checkout scm
24 }
25
26
27 // -------------------------------------------------------------------------
28 // Run all the enclosed stages with access to the Salesforce
29 // JWT key credentials.
30 // -------------------------------------------------------------------------
31
32 withEnv(["HOME=${env.WORKSPACE}"]) {
33
34 withCredentials([file(credentialsId: SERVER_KEY_CREDENTALS_ID, variable: 'server_key_file')]) {
35
36 // -------------------------------------------------------------------------
37 // Authorize the Dev Hub org with JWT key and give it an alias.
38 // -------------------------------------------------------------------------
39
40 stage('Authorize DevHub') {
41 rc = command "${toolbelt}/sf org login jwt --instance-url ${SF_INSTANCE_URL} --client-id ${SF_CONSUMER_KEY} --username ${SF_USERNAME} --jwt-key-file ${server_key_file} --set-default-dev-hub --alias HubOrg"
42 if (rc != 0) {
43 error 'Salesforce dev hub org authorization failed.'
44 }
45 }
46
47
48 // -------------------------------------------------------------------------
49 // Create new scratch org to test your code.
50 // -------------------------------------------------------------------------
51
52 stage('Create Test Scratch Org') {
53 rc = command "${toolbelt}/sf org create scratch --target-dev-hub HubOrg --set-default --definition-file config/project-scratch-def.json --alias ciorg --wait 10 --duration-days 1"
54 if (rc != 0) {
55 error 'Salesforce test scratch org creation failed.'
56 }
57 }
58
59
60 // -------------------------------------------------------------------------
61 // Display test scratch org info.
62 // -------------------------------------------------------------------------
63
64 stage('Display Test Scratch Org') {
65 rc = command "${toolbelt}/sf org display --target-org ciorg"
66 if (rc != 0) {
67 error 'Salesforce test scratch org display failed.'
68 }
69 }
70
71
72 // -------------------------------------------------------------------------
73 // Push source to test scratch org.
74 // -------------------------------------------------------------------------
75
76 stage('Push To Test Scratch Org') {
77 rc = command "${toolbelt}/sf project deploy start --target-org ciorg"
78 if (rc != 0) {
79 error 'Salesforce push to test scratch org failed.'
80 }
81 }
82
83
84 // -------------------------------------------------------------------------
85 // Run unit tests in test scratch org.
86 // -------------------------------------------------------------------------
87
88 stage('Run Tests In Test Scratch Org') {
89 rc = command "${toolbelt}/sf apex run test --target-org ciorg --wait 10 --result-format tap --code-coverage --test-level ${TEST_LEVEL}"
90 if (rc != 0) {
91 error 'Salesforce unit test run in test scratch org failed.'
92 }
93 }
94
95
96 // -------------------------------------------------------------------------
97 // Delete test scratch org.
98 // -------------------------------------------------------------------------
99
100 stage('Delete Test Scratch Org') {
101 rc = command "${toolbelt}/sf org delete scratch --target-org installorg --no-prompt"
102 if (rc != 0) {
103 error 'Salesforce test scratch org deletion failed.'
104 }
105 }
106
107
108 // -------------------------------------------------------------------------
109 // Create package version.
110 // -------------------------------------------------------------------------
111
112 stage('Create Package Version') {
113 if (isUnix()) {
114 output = sh returnStdout: true, script: "${toolbelt}/sf package version create --package ${PACKAGE_NAME} --installation-key-bypass --wait 10 --json --target-dev-hub HubOrg"
115 } else {
116 output = bat(returnStdout: true, script: "${toolbelt}/sf package version create --package ${PACKAGE_NAME} --installation-key-bypass --wait 10 --json --target-dev-hub HubOrg").trim()
117 output = output.readLines().drop(1).join(" ")
118 }
119
120 // Wait 5 minutes for package replication.
121 sleep 300
122
123 def jsonSlurper = new JsonSlurperClassic()
124 def response = jsonSlurper.parseText(output)
125
126 PACKAGE_VERSION = response.result.SubscriberPackageVersionId
127
128 response = null
129
130 echo ${PACKAGE_VERSION}
131 }
132
133
134 // -------------------------------------------------------------------------
135 // Create new scratch org to install package to.
136 // -------------------------------------------------------------------------
137
138 stage('Create Package Install Scratch Org') {
139 rc = command "${toolbelt}/sf org create scratch --target-dev-hub HubOrg --set-default --definition-file config/project-scratch-def.json --alias installorg --wait 10 --duration-days 1"
140 if (rc != 0) {
141 error 'Salesforce package install scratch org creation failed.'
142 }
143 }
144
145
146 // -------------------------------------------------------------------------
147 // Display install scratch org info.
148 // -------------------------------------------------------------------------
149
150 stage('Display Install Scratch Org') {
151 rc = command "${toolbelt}/sf org display --target-org installorg"
152 if (rc != 0) {
153 error 'Salesforce install scratch org display failed.'
154 }
155 }
156
157
158 // -------------------------------------------------------------------------
159 // Install package in scratch org.
160 // -------------------------------------------------------------------------
161
162 stage('Install Package In Scratch Org') {
163 rc = command "${toolbelt}/sf package install --package ${PACKAGE_VERSION} --target-org installorg --wait 10"
164 if (rc != 0) {
165 error 'Salesforce package install failed.'
166 }
167 }
168
169
170 // -------------------------------------------------------------------------
171 // Run unit tests in package install scratch org.
172 // -------------------------------------------------------------------------
173
174 stage('Run Tests In Package Install Scratch Org') {
175 rc = command "${toolbelt}/sf apex run test --target-org installorg --result-format tap --code-coverage --test-level ${TEST_LEVEL} --wait 10"
176 if (rc != 0) {
177 error 'Salesforce unit test run in pacakge install scratch org failed.'
178 }
179 }
180
181
182 // -------------------------------------------------------------------------
183 // Delete package install scratch org.
184 // -------------------------------------------------------------------------
185
186 stage('Delete Package Install Scratch Org') {
187 rc = command "${toolbelt}/sf org delete scratch --target-org installorg --no-prompt"
188 if (rc != 0) {
189 error 'Salesforce package install scratch org deletion failed.'
190 }
191 }
192 }
193 }
194}
195
196def command(script) {
197 if (isUnix()) {
198 return sh(returnStatus: true, script: script);
199 } else {
200 return bat(returnStatus: true, script: script);
201 }
202}