Set environment variables then run script in Jenkins Scripted Pipeline

10,916

I figured it out. I was cloning directly into the workspace and then setting my environment variables to point to the workspace as well. I modified both those things. I now create a dir in my workspace and clone into it and I also set my environment variables to directories inside my workspace. Like so:

node('build-01') {
    withEnv(["CMAKE_INSTALL_DIR=${WORKSPACE}/cmake_install", "SDK_INSTALL_DIR=${WORKSPACE}/sdk"]){
        stage('Building') {
            echo "[*] Starting build (id: ${env.BUILD_ID}) on ${env.JENKINS_URL}"
            try {
                sh 'ls -l'
                //ls shows the damn file
                dir('path/to/checkout') {
                    sh '. ./setup-target'
                }
            } catch(all) { 
                sh "echo 'Failed to run setup-target script with error: ' ${all}"
            }
        }    
    }
}

This works.

Note: Since this site is in beta phase and I barely got any views I posted the same question to stackoverflow, here it is for refernce: https://stackoverflow.com/questions/56015463/set-environment-variables-then-run-script-in-jenkins-scripted-pipeline

Share:
10,916

Related videos on Youtube

Perplexabot
Author by

Perplexabot

Updated on September 18, 2022

Comments

  • Perplexabot
    Perplexabot over 1 year

    I am new to Jenkins, Groovy and pipelines. I have created a simple pipeline stage like so:

    //working build but not setting env variables
    node('build-01') {
        stage('Building') {
            echo "[*] Starting build (id: ${env.BUILD_ID}) on ${env.JENKINS_URL}"
            try {
                sh 'ls -l'
                //ls shows the damn file
                sh '. setup-target'
            } catch(all) { 
                sh "echo 'Failed to run setup-target script with error: ' ${all}"
            }
        }    
    }
    

    This works. But I want to modify/add environment variables to the session running this script (this script is a bash file with the correct shebang line on top). So I did:

    node('build-01') {
        withEnv(["CMAKE_INSTALL_DIR=${WORKSPACE}", "SDK_INSTALL_DIR=${WORKSPACE}"]){
            stage('Building') {
                echo "[*] Starting build (id: ${env.BUILD_ID}) on ${env.JENKINS_URL}"
                try {
                    sh 'ls -l'
                    //ls shows the damn file
                    sh '. setup-target'
                } catch(all) { 
                    sh "echo 'Failed to run setup-target script with error: ' ${all}"
                }
            }    
        }
    }
    

    This errors out with:

    /home/jenkins-sw/ci/workspace/myWorkSpace@tmp/durable-6d30b48d/script.sh: line 1: .: setup-target: file not found

    and

    Failed to run setup-target script with error: hudson.AbortException: script returned exit code 1

    But the environment variables are set, I check this by doing a sh 'printenv' right below the ls -l line. Interestingly ls -l does show the script.

    What am I missing?