Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
1 view
in Salesforce by (11.9k points)

I have a custom tool defined within Jenkins via the Custom Tools plugin. If I create a freestyle project the Install custom tools option correctly finds and uses the tool (Salesforce DX) during execution.

However, I cannot find a way to do the same via a pipeline file. I have used the pipeline syntax snippet generator to get:

tool name: 'sfdx', type: 'com.cloudbees.jenkins.plugins.customtools.CustomTool'

I have put that into my stage definition:

stage('FetchMetadata') {
            print 'Collect Prod metadata via SFDX'
            tool name: 'sfdx', type: 'com.cloudbees.jenkins.plugins.customtools.CustomTool'
            sh('sfdx force:mdapi:retrieve -r metadata/ -u DevHub -k ./metadata/package.xml')
        }

but I get an error message stating line 2: sfdx: command not found

Is there some other way I should be using this snippet?

Full Jenkinsfile for info:

node {

    currentBuild.result = 'SUCCESS'

    try {

        stage('CheckoutRepo') {

            print 'Get the latest code from the MASTER branch'

            checkout scm

        }

        stage('FetchMetadata') {

            print 'Collect Prod metadata via SFDX'

            tool name: 'sfdx', type: 'com.cloudbees.jenkins.plugins.customtools.CustomTool'

            sh('sfdx force:mdapi:retrieve -r metadata/ -u DevHub -k ./metadata/package.xml')

        }

        stage('ConvertMetadata') {

            print 'Unzip retrieved metadata file'

            sh('unzip unpackaged.zip .')

            print 'Convert metadata to SFDX format'

            sh('/usr/local/bin/sfdx force:mdapi:convert -r metadata/unpackaged/ -d force-app/')

        }

        stage('CommitChanges') {

            sh('git add --all')

            print 'Check if any changes need committing'

            sh('if ! git diff-index --quiet HEAD --; then echo "changes found - pushing to repo"; git commit -m "Autocommit from Prod @ $(date +%H:%M:%S\' \'%d/%m/%Y)"; else echo "no changes found"; fi')

            sshagent(['xxx-xxx-xxx-xxx']) {

                sh('git push -u origin master')

            }

        }

    }

    catch (err) {

        currentBuild.result = 'FAILURE'

        print 'Build failed'

        error(err)

    }

1 Answer

+1 vote
by (32.1k points)

I got to this workaround:

environment {

    GROOVY_HOME = tool name: 'Groovy-2.4.9', type: 'hudson.plugins.groovy.GroovyInstallation'

}

stages {

    stage('Run Groovy') {

        steps {

            bat "${groovy_home}/bin/groovy <script.name>"

        }

    }

}

Somehow the tool path is not added to PATH by default (as was customary on my 1.6 Jenkins server install). Adding the ${groovy_home} when executing the bat command fixes that for me. This way of calling a tool is basically lent from the scripted pipeline syntax. I am using this for all my custom tools (not only groovy).

The tool part:

tool name: 'Groovy-2.4.9', type: 'hudson.plugins.groovy.GroovyInstallation'

was generated by the snippet generator AS you did.

According to the Jenkins users mailing list, work is still ongoing for a definitive solution, so my solution really is a workaround.

If you want to learn more about Jenkins, you should join Jenkins Certification Course.

Browse Categories

...