4

Issue

I am trying to setup jenkins to build using the Jenkinsfile and the sfdx cli. I have been following the steps and examples here. I have followed the steps and tried a few times from scratch but to no avail. I am using a local install of jenkins and so the server private key and the certificate linked to the dev hub connected app is the same for running the sfdx command in the terminal and via jenkins. Running it locally works and the dev hub is authorised but running the stage via the Jenkinsfile the command just hangs, I would have thought that if it was an authorisation issue it would fail or at least timeout. I have looked on the dev hub login history and it is saying a successful login request. Any idea as to what I may have missed and is anyone else having issues?

Jenkinsfile Example

#!groovy
import groovy.json.JsonSlurperClassic
node {
    def BUILD_NUMBER = env.BUILD_NUMBER
    def RUN_ARTIFACT_DIR = "tests/${BUILD_NUMBER}"
    def SFDC_USERNAME
    def HUB_ORG = env.HUB_ORG_DH
    def SFDC_HOST = env.SFDC_HOST_DH
    def JWT_KEY_CRED_ID = env.JWT_CRED_ID_DH
    def CONNECTED_APP_CONSUMER_KEY = env.CONNECTED_APP_CONSUMER_KEY_DH
    def toolbelt = tool 'toolbelt'

    stage('Retieve Source') {
        checkout scm
    }

    withCredentials([file(credentialsId: JWT_KEY_CRED_ID, variable: 'jwt_key_file')]) {
        stage('Authorise Dev Hub') {
            rc = sh returnStatus: true, script: "${toolbelt}/sfdx force:auth:jwt:grant -i ${CONNECTED_APP_CONSUMER_KEY} -u ${HUB_ORG} -f ${jwt_key_file} -d"
            if(rc != 0) {
                error 'Hub Org authorisation failed'
            }
        }
    }
}

I've stripped out all the stuff afterwards as it never gets passed the authorise dev hub stage.

3

Are you by any chance using MacOS? It could be due to an issue that the DX tooling cannot connect to the keychain as it's attempting to display a dialog window to indicate this and results in it appearing to hang.

Below is a simplified version of a declarative pipeline which shows how to use the generic keychain functionality and resolved the issue for me.

pipeline {
    agent any
    environment {
        // Removed other variables for clarity...
        SFDX_USE_GENERIC_UNIX_KEYCHAIN = true
        // ...
    }
    stages {    
        stage('TEST') {
            steps {
                withCredentials([file(credentialsId: 'jenkins-cert', variable: 'VAR_CERT_FILE')]) {
                    sh returnStdout: true, script: "${SFDX_HOME}/sfdx force:auth:jwt:grant --clientid ${HUB_CLIENT_ID} --username ${HUB_USERNAME} --jwtkeyfile ${VAR_CERT_FILE} --setdefaultdevhubusername --instanceurl ${HUB_HOST}"
                }
            }
        }
    }
}

You can find more environment variables at which can influence the DX tooling on the Salesforce help and support website:

https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_cli_env_variables.htm

Hope this helps!

Adam

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.