简体   繁体   English

设置环境变量,然后在Jenkins Scripted Pipeline中运行脚本

[英]Set environment variables then run script in Jenkins Scripted Pipeline

I am new to Jenkins, Groovy and pipelines. 我是Jenkins,Groovy和管道的新手。 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). 但我想修改/添加环境变量到运行此脚本的会话(此脚本是一个bash文件,顶部有正确的shebang行)。 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 /home/jenkins-sw/ci/workspace/myWorkSpace@tmp/durable-6d30b48d/script.sh:line 1:。:setup-target:找不到文件

and

Failed to run setup-target script with error: hudson.AbortException: script returned exit code 1 无法运行setup-target脚本,错误:hudson.AbortException:脚本返回退出代码1

But the environment variables are set, I check this by doing a sh 'printenv' right below the ls -l line. 但是设置了环境变量,我通过在ls -l行下方sh 'printenv'检查这一点。 Interestingly ls -l does show the script. 有趣的是ls -l确实显示了脚本。

What am I missing? 我错过了什么?

UPDATE UPDATE

The following: 下列:

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}"
            }
        }    
    }
}

results in: 结果是:

/home/jenkins-sw/ci/workspace/myWorkSpace@tmp/durable-6d30b48d/script.sh: line 1: ./setup-target: Permission denied /home/jenkins-sw/ci/workspace/myWorkSpace@tmp/durable-6d30b48d/script.sh:line 1:./ setup-target:权限被拒绝

Interesting. 有趣。 How is withEnv effecting permissions? withEnv如何影响权限? What?! 什么?! And if I chmod that file to have permissions, i get a new error, something related to "missing workspace". 如果我chmod该文件有权限,我得到一个新的错误,与“缺少工作区”相关的东西。

My guess would be that either CMAKE_INSTALL_DIR or SDK_INSTALL_DIR are on the path. 我的猜测是CMAKE_INSTALL_DIRSDK_INSTALL_DIR都在路径上。

Instead of sh '. setup-target' 而不是sh '. setup-target' sh '. setup-target' you should sh './setup-target' . sh '. setup-target'你应该sh './setup-target' sh '. setup-target' sh './setup-target'

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. 这有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM