且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Shell脚本的Groovy参数

更新时间:2023-12-05 14:38:40

更新

您可以使用环境变量而无需environment {}

You can use environment variable without having environment {}

使用类似我在这里使用的环境变量(我对您的代码进行了一些重构).对外壳程序脚本使用三重单引号进行循环并向其添加grrovy变量:

Use environment variables like the ones i have used here (i refactored your code a little bit). Using triple single quotes for shell script for loop and adding grrovy variable to it:

def callfunc() {
  sh '''
  export s="key"
  echo $s
  for i in $VARENV1 
    do
      echo "Looping ... i is set to $i"
    done
    '''
}

pipeline {
    agent { label 'agent_1' }
    stages {
      stage ('Run script') {
        steps {
            script {
                env.VARENV1 = "Peace"
            }
            callfunc()
        }
      }
    }
}

输出:

参考: Jenkins-将参数传递给groovy函数