如何将阶段内的步骤移动到 Jenkins 管道中的函数
How to move steps inside stage to a function in Jenkins pipeline
我有一个这样的 Jenkinsfile
pipeline {
agent { label 'master' }
stages {
stage('1') {
steps {
script {
sh '''#!/bin/bash
source $EXPORT_PATH_SCRIPT
cd $SCRIPT_PATH
python -m scripts.test.test
'''
}
}
}
stage('2') {
steps {
script {
sh '''#!/bin/bash
source $EXPORT_PATH_SCRIPT
cd $SCRIPT_PATH
python -m scripts.test.test
'''
}
}
}
}
}
如您所见,在这两个阶段我都使用相同的脚本并调用相同的文件。
我可以将此步骤移动到 Jenkinsfile 中的函数并在脚本中调用该函数吗?像这样
def execute script() {
return {
sh '''#!/bin/bash
source $EXPORT_PATH_SCRIPT
cd $SCRIPT_PATH
python -m scripts.test.test
'''
}
}
是的,可以像下面的例子:
詹金斯文件
def doIt(name) {
return "The name is : ${name}"
}
def executeScript() {
sh "echo HelloWorld"
}
pipeline {
agent any;
stages {
stage('01') {
steps {
println doIt("stage 01")
executeScript()
}
}
stage('02') {
steps {
println doIt("stage 02")
executeScript()
}
}
}
}
我有一个这样的 Jenkinsfile
pipeline {
agent { label 'master' }
stages {
stage('1') {
steps {
script {
sh '''#!/bin/bash
source $EXPORT_PATH_SCRIPT
cd $SCRIPT_PATH
python -m scripts.test.test
'''
}
}
}
stage('2') {
steps {
script {
sh '''#!/bin/bash
source $EXPORT_PATH_SCRIPT
cd $SCRIPT_PATH
python -m scripts.test.test
'''
}
}
}
}
}
如您所见,在这两个阶段我都使用相同的脚本并调用相同的文件。 我可以将此步骤移动到 Jenkinsfile 中的函数并在脚本中调用该函数吗?像这样
def execute script() {
return {
sh '''#!/bin/bash
source $EXPORT_PATH_SCRIPT
cd $SCRIPT_PATH
python -m scripts.test.test
'''
}
}
是的,可以像下面的例子:
詹金斯文件
def doIt(name) {
return "The name is : ${name}"
}
def executeScript() {
sh "echo HelloWorld"
}
pipeline {
agent any;
stages {
stage('01') {
steps {
println doIt("stage 01")
executeScript()
}
}
stage('02') {
steps {
println doIt("stage 02")
executeScript()
}
}
}
}