我遇到了一個問題,我似乎無法在 Jenkins 管道腳本中運行 python 腳本。
如果我創建一個 Jenkins Freestyle 作業并使用以下命令執行 shell:
#!/usr/bin/env python
import boto3
匯入有效,我可以運行其余的 python 代碼。
但是,如果我創建一個 Jenkins 管道并嘗試運行以下命令:
pipeline {
agent { label 'master' }
stages {
stage('job') {
steps {
sh '''
#!/usr/bin/env python
import boto3
'''
}
}
}
}
我得到 import: command not found
我需要以其他方式設定 env 嗎?
更新:
所以我試過這個:
pipeline {
agent { label 'master' }
stages {
stage('job') {
steps {
sh '''#!/usr/bin/env python
import boto3
'''
}
}
}
}
現在我收到了縮進錯誤。
我試過以百萬種方式將它縮進,但沒有運氣。
當我在沒有匯入 boto3 的情況下運行上述內容時,作業運行良好。
我還檢查了 Jenkins 服務器上的代碼段生成器并閱讀了以下內容:
“可以使用解釋器選擇器,例如:#!/usr/bin/perl”
更新
以下作業:
pipeline {
agent { label 'master' }
stages {
stage('job') {
steps {
sh '''#!/usr/bin/env python \n'''
'''import boto3'''
}
}
}
}
唯一的問題是必須在每一行之后使用 \n 和 似乎有點太多......必須有另一種方式
uj5u.com熱心網友回復:
這里的問題是您呼叫 shell step 方法會在 shell 解釋器中執行命令,而您想要執行 Python 代碼。要在 shell 解釋器中啟用此功能,您需要指示 Python 解釋器在 shell 解釋器中執行 Python 代碼:
steps {
sh(label: 'Execute Python Code', script: 'python -c "import boto3"')
}
這將實作所需的行為。
uj5u.com熱心網友回復:
如果您已經將 Python 代碼保存在檔案中,那就更好了
stage('build') {
steps {
sh 'python abc.py'
}
只需確保在該 jenkins 實體上安裝了 python。使用which python來檢查你可能需要的相對路徑
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/388720.html
