Newer Version Available

This content describes an older version of this product. View Latest

Sample Jenkinsfile

A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline. This Jenkinsfile shows how to integrate the Salesforce DX CLI commands to automate testing of your Salesforce applications using scratch orgs.

The Jenkinsfile Walkthrough topic uses this Jenkinsfile as an example.

1#!groovy
2import groovy.json.JsonSlurperClassic
3node {
4
5    def BUILD_NUMBER=env.BUILD_NUMBER
6    def RUN_ARTIFACT_DIR="tests/${BUILD_NUMBER}"
7    def SFDC_USERNAME
8
9    def HUB_ORG=env.HUB_ORG_DH
10    def SFDC_HOST = env.SFDC_HOST_DH
11    def JWT_KEY_CRED_ID = env.JWT_CRED_ID_DH
12    def CONNECTED_APP_CONSUMER_KEY=env.CONNECTED_APP_CONSUMER_KEY_DH
13
14    def toolbelt = tool 'toolbelt'
15
16    stage('checkout source') {
17        // when running in multi-branch job, one must issue this command
18        checkout scm
19    }
20
21    withCredentials([file(credentialsId: JWT_KEY_CRED_ID, variable: 'jwt_key_file')]) {
22        stage('Create Scratch Org') {
23
24            rc = sh returnStatus: true, script: "${toolbelt}/sfdx force:auth:jwt:grant --clientid ${CONNECTED_APP_CONSUMER_KEY} --username ${HUB_ORG} --jwtkeyfile ${jwt_key_file} --setdefaultdevhubusername --instanceurl ${SFDC_HOST}"
25            if (rc != 0) { error 'hub org authorization failed' }
26
27            // need to pull out assigned username
28            rmsg = sh returnStdout: true, script: "${toolbelt}/sfdx force:org:create --definitionfile config/project-scratch-def.json --json --setdefaultusername"
29            printf rmsg
30            def jsonSlurper = new JsonSlurperClassic()
31            def robj = jsonSlurper.parseText(rmsg)
32            if (robj.status != 0) { error 'org creation failed: ' + robj.message }
33            SFDC_USERNAME=robj.result.username
34            robj = null
35
36        }
37
38        stage('Push To Test Org') {
39            rc = sh returnStatus: true, script: "${toolbelt}/sfdx force:source:push --targetusername ${SFDC_USERNAME}"
40            if (rc != 0) {
41                error 'push failed'
42            }
43            // assign permset
44            rc = sh returnStatus: true, script: "${toolbelt}/sfdx force:user:permset:assign --targetusername ${SFDC_USERNAME} --permsetname DreamHouse"
45            if (rc != 0) {
46                error 'permset:assign failed'
47            }
48        }
49
50        stage('Run Apex Test') {
51            sh "mkdir -p ${RUN_ARTIFACT_DIR}"
52            timeout(time: 120, unit: 'SECONDS') {
53                rc = sh returnStatus: true, script: "${toolbelt}/sfdx force:apex:test:run --testlevel RunLocalTests --outputdir ${RUN_ARTIFACT_DIR} --resultformat tap --targetusername ${SFDC_USERNAME}"
54                if (rc != 0) {
55                    error 'apex test run failed'
56                }
57            }
58        }
59
60        stage('collect results') {
61            junit keepLongStdio: true, testResults: 'tests/**/*-junit.xml'
62        }
63    }
64}