Jenkinsfile のサンプル
Jenkinsfile は、Jenkins パイプラインの定義が含まれるテキストファイルです。Jenkinsfile には、Salesforce CLI を組み込み、スクラッチ組織を使用して Salesforce アプリケーションのテストを自動化する方法が示されています。
「Jenkinsfile のウォークスルー」トピックでは、この sfdx-jenkins-package Jenkinsfile が例として使用されています。
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}/sfdx auth:jwt:grant --instanceurl ${SF_INSTANCE_URL} --clientid ${SF_CONSUMER_KEY} --username ${SF_USERNAME} --jwtkeyfile ${server_key_file} --setdefaultdevhubusername --setalias 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}/sfdx force:org:create --targetdevhubusername HubOrg --setdefaultusername --definitionfile config/project-scratch-def.json --setalias ciorg --wait 10 --durationdays 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}/sfdx force:org:display --targetusername 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}/sfdx force:source:push --targetusername 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}/sfdx force:apex:test:run --targetusername ciorg --wait 10 --resultformat tap --codecoverage --testlevel ${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}/sfdx force:org:delete --targetusername ciorg --noprompt"
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}/sfdx force:package:version:create --package ${PACKAGE_NAME} --installationkeybypass --wait 10 --json --targetdevhubusername HubOrg"
115 } else {
116 output = bat(returnStdout: true, script: "${toolbelt}/sfdx force:package:version:create --package ${PACKAGE_NAME} --installationkeybypass --wait 10 --json --targetdevhubusername 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}/sfdx force:org:create --targetdevhubusername HubOrg --setdefaultusername --definitionfile config/project-scratch-def.json --setalias installorg --wait 10 --durationdays 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}/sfdx force:org:display --targetusername 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}/sfdx force:package:install --package ${PACKAGE_VERSION} --targetusername 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}/sfdx force:apex:test:run --targetusername installorg --resultformat tap --codecoverage --testlevel ${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}/sfdx force:org:delete --targetusername installorg --noprompt"
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}