【问题标题】:use of bash env variable in post section of pipeline在管道的 post 部分使用 bash env 变量
【发布时间】:2021-07-19 15:33:25
【问题描述】:

我一直在阅读尽可能多的关于此主题的帖子,但没有一篇为我提供可行的解决方案,因此,再次将其扔给社区:

在 Jenkinsfile 管道中我有

steps {
  (...)
  sh script: '''
    $pkgname #existing var
    export report_filename=$pkgname'_report.txt'
    (stuff is being written to the $report_filename file...)
  '''
}
post {
 always {
  script {
    //want to read the file with name carried by $report_filename
    def report = readFile(file: env.report_filename, encoding: 'utf-8').trim()
    buildDescription(report)
  }
 }
}

我无法将 report_filename bash var 的值传递给 post > always > 脚本部分。尝试了 ${env.report_filename} (带/不带单引号/双引号),带/不带 env。以及其他一些疯狂的事情。

我在这里做错了什么?

谢谢。

【问题讨论】:

  • 你希望 shell 脚本第一行的$pkgname 做什么?变量包含什么?
  • export 使该变量对 shell 的 children 可用,而不是对父级可用。
  • 变量 $pkgname 捕获我刚刚构建的 conda 包的名称(在“步骤”部分中)。然后我构建字符串 {name_of_conda_package}_report.txt 并希望将其存储在 var 报告中以用于后置条件。
  • 所以如果变量包含condapackage.zip,那么只有$pkgname 会产生condapackage.zip: no such file or directory
  • $pkgname 包含一个字符串,例如'a-b-c',我通过report_filename 分配构建了一个报告名称'a-b-c_report.txt',我想在Groovy 中使用report_filename var。跨度>

标签: bash jenkins groovy jenkins-pipeline jenkins-groovy


【解决方案1】:

可能有点不对。

  1. 创建变量 def var
  2. 使用选项 returnStdout: true。并解析输出。 var = sh ( script " echo #existing var", returnStdout: true).split("\n")
  3. 在阶段 readFile(file: var[0]...) 中使用 var[0]

如果你可以使用 env,请添加:

environment {
    VAR = sh (script " echo  #existing var", returnStdout: true).split("\n") [0]
}
script {
    //want to read the file with name carried by $report_filename
    def report = readFile(file: env.VAR , encoding: 'utf-8').trim()
    buildDescription(report)
}

【讨论】:

  • 在 Jenkinsfile 中定义变量并且不将 shell echo 还给您会不会容易得多?
【解决方案2】:

我不明白你为什么不一开始就在 Groovy 中声明变量。

我对这门语言不太熟悉,目前也没有办法对此进行测试;但是是这样的:

def pkgname = "gunk"
def report_filename = "${pkgname}_report.txt"
steps {
  (...)
  sh script: """
    # use triple double quotes so that Groovy variables are interpolated
    # $pkgname #syntax error, take it out
    (stuff is being written to the $report_filename file...)
  """
}
post {
 always {
  script {
    //want to read the file with name carried by $report_filename
    def report = readFile(file: env.report_filename, encoding: 'utf-8').trim()
    buildDescription(report)
  }
 }
}

【讨论】:

    猜你喜欢
    • 2017-08-15
    • 2011-09-09
    • 2022-01-18
    • 2018-03-20
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    相关资源
    最近更新 更多