[英]How to load bash script in Jenkins pipeline?
我們有一些復雜的 bash 腳本,現在位於 Jenkins 的托管文件部分。 我們嘗試將作業遷移到管道,但我們不知道將 bash 腳本轉換為 groovy,因此我們希望將其保留在 bash 中。 我們在 Git 中有一個 jenkins-shared-library,我們在其中存儲我們的管道模板。 在作業中,我們添加了正確的環境變量。
我們希望將 bash 腳本保存在 git 中而不是托管文件中。 在管道中加載此腳本並執行它的正確方法是什么? 我們嘗試了libraryResource
一些東西,但我們沒有設法讓它工作。 我們必須將test.sh
腳本放在 git 中的什么位置,我們如何調用它? (或者在這里運行shell腳本是完全錯誤的)
def call(body) {
// evaluate the body block, and collect configuration into the object
def pipelineParams= [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = pipelineParams
body()
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr: '3'))
}
stages {
stage ('ExecuteTestScript') {
steps {
def script = libraryResource 'loadtestscript?'
script {
sh './test.sh'
}
}
}
}
post {
always {
cleanWs()
}
}
}
}
在我的公司,我們的 CI 中也有復雜的 bash 腳本,而libraryResource
是一個更好的解決方案。 按照您的腳本,您可以執行一些更改以使用存儲在libraryResource
的bash
腳本:
stages {
stage ('ExecuteTestScript') {
steps {
// Load script from library with package path
def script_bash = libraryResource 'com/example/loadtestscript'
// create a file with script_bash content
writeFile file: './test.sh', text: script_bash
// Run it!
sh 'bash ./test.sh'
}
}
}
我想詳細說明@Barizon 的答案,這為我指明了正確的方向。
我的需要是使用 ssh 在遠程服務上執行腳本。
我在共享庫項目的/var
文件夾中創建了一個 groovy 腳本,我們稱之為my_script.groovy
。
在腳本中我定義了函數:
def my_function(String serverIp, String scriptArgument) {
def script_content = libraryResource 'my_scripts/test.sh'
// create a file with script_bash content
writeFile file: './test.sh', text: script_content
echo "Execute remote script test.sh..."
def sshCommand = "ssh username@${serverIp} \'bash -xs\' < ./test.sh ${scriptArgument}"
echo "Ssh command is: ${sshCommand}"
sh(sshCommand)
}
從管道中,我可以像這樣調用它:
@Library('MySharedLibrary')_
pipeline {
agent any
stages {
stage('MyStage') {
steps {
script {
my_script.my_function("192.168.1.1", "scriptArgumentValue")
}
}
}
}
}
您可以直接從自定義步驟調用腳本,而不是將腳本復制到您的工作區。 例如,在庫的 vars 目錄中創建一個名為doSomething.groovy
:
#!/usr/bin/env groovy
def call(args) {
def scriptDir = WORKSPACE + '@libs/my-shared-library'
sh "$scriptDir/do-something.sh $args"
}
這是有效的,因為共享庫被檢出到以作業的工作空間命名的目錄中,后綴為@libs。 如果您願意,可以將do-something.sh
移動到庫的資源目錄或其他目錄。
我不知道這對於 OP 是否不可能,但我發現將我的實用程序腳本保存在與 Jenkinsfile 相同的 git 存儲庫中要簡單得多,我從來libraryResource
使用libraryResource
。 然后你可以直接用sh
指令調用它們,你甚至可以傳遞environment
塊中定義的變量。 例如,在script
或steps
塊內,在管道stage
:
sh "./build.sh $param1"
你也可以把一堆 bash 函數放在它自己的文件中,比如“ scripts/myfuncs.sh ”。 你甚至可以有一個調用這些腳本的 groovy 函數,本質上是一個包裝器,見下文:
def call_func() {
sh"""
#!/bin/bash
. ./scripts/myfuncs.sh
my_util_func "$param1"
"""
}
需要注意的幾點:
盡管在我的終端上我可以使用source scripts/myfuncs.sh
,但在 Jenkins 上我需要使用.
source
簡寫(如上所示)否則它會抱怨它找不到source
!
在將其推送到存儲庫之前,您必須使./scripts/myfuncs.sh
可執行,例如chmod 755 ./scripts/myfuncs.sh
。
完整管道示例:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh '''
./build.sh
'''
}
}
}
post {
always {
sh '''
./scripts/clean.sh
'''
}
success {
script {
if (env.BRANCH_NAME == 'master') {
sh "./scripts/master.sh"
} else if (env.BRANCH_NAME.startsWith('PR')) {
sh "./scripts/mypr.sh"
} else {
// this is some other branch
}
}
}
}
}
在上面的示例中,“Jenkinsfile”和“build.sh”都在存儲庫的根目錄中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.