簡體   English   中英

設置環境變量,然后在Jenkins Scripted Pipeline中運行腳本

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

我是Jenkins,Groovy和管道的新手。 我創建了一個簡單的管道階段,如下所示:

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

這有效。 但我想修改/添加環境變量到運行此腳本的會話(此腳本是一個bash文件,頂部有正確的shebang行)。 所以我做了:

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

這出錯了:

/home/jenkins-sw/ci/workspace/myWorkSpace@tmp/durable-6d30b48d/script.sh:line 1:。:setup-target:找不到文件

無法運行setup-target腳本,錯誤:hudson.AbortException:腳本返回退出代碼1

但是設置了環境變量,我通過在ls -l行下方sh 'printenv'檢查這一點。 有趣的是ls -l確實顯示了腳本。

我錯過了什么?

UPDATE

下列:

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

結果是:

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

有趣。 withEnv如何影響權限? 什么?! 如果我chmod該文件有權限,我得到一個新的錯誤,與“缺少工作區”相關的東西。

我的猜測是CMAKE_INSTALL_DIRSDK_INSTALL_DIR都在路徑上。

而不是sh '. setup-target' sh '. setup-target'你應該sh './setup-target' sh '. setup-target' sh './setup-target'

我想到了。 我直接克隆到工作區,然后將環境變量設置為指向工作區。 我修改了這些東西。 我現在在我的工作區中創建一個目錄並克隆到它中,我還將環境變量設置為工作區內的目錄。 像這樣:

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

這有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM