将变量从Bash脚本传递到Jenkins管道,无需插件。

huangapple go评论59阅读模式
英文:

Transferring variables from bash script to Jenkins pipeline without plugins

问题

我正在处理一个需要执行bash脚本以从API获取信息的流水线。然后,我需要将这些值返回到Jenkinsfile中以供以后使用。这里的主要问题是有多个变量,如果只有一个变量,我可以通过STDOUT捕获它。

关于此问题的解决方案已经被评论在此处:链接,但不幸的是,答案已被删除。

这里的解决方案使用额外的文件,我想尽量避免这种情况。那个答案也有4年的历史了,我不确定Jenkins是否添加了任何允许这种变量传递的新功能。

英文:

I am working on a pipeline which requires a bash script to be executed in order to get some information from an API. I then need to get these values back into the Jenkinsfile to use them later. The big problem here is the multiple variables, as if it was one, I could just capture it through STDOUT.
The link to a solution commented here: https://stackoverflow.com/questions/10625259/how-to-set-environment-variables-in-jenkins/53430757#53430757, fails, as the answer has been deleted.
The solutions here: https://stackoverflow.com/questions/56846461/is-there-any-way-to-pass-variables-from-bash-script-to-jenkinsfile-without-using, use an extra file, and I would like to avoid that as much as possible. That answer is also 4 years old and I am unsure if there have been any features added to Jenkins that would allow for such transfer of variables.

答案1

得分: 1

你正在尝试以一种巧妙的方式来实现这个,我认为来自Jenkins的任何人都不会实现这个(在脚本中将变量从脚本传输到Jenkins而不创建临时文件)。

另外,你可以使用stdout的解决方法,如下:

pipeline {
  agent any
  stages {
    stage('mainstage') {
      steps {
        script {
          // ---- test.sh内容 ----
          // #!/bin/bash
          // my_first_variable="contentoffirstvar"
          // my_second_variable="contentofsecondvar"
          // echo "my_first_variable=$my_first_variable;my_second_variable=$my_second_variable"
          // -------

          def VARIABLES = sh (
              script: '/var/jenkins_home/test.sh',
              returnStdout: true
          ).trim().split(';')
          VARIABLES.each { var ->
              def (var_name, var_value) = var.split('=')
              env."${var_name}" = var_value
          }
        }
      }
    }
  }
}

作为改进的一点,你可以添加类似以下的模式:

===
my-var1=value1
my-var2=value2
===

然后根据模式拆分和查找,根据需要进行处理。

英文:

you are trying to to that in a tricky way, i dont think anyone from jenkins will implement this (transfer var from script to jenkins without tmp file creation)

ps you can use workarounds with stdout like:

pipeline {
  agent any
  stages {
    stage('mainstage') {
      steps {
        script {
          // ---- test.sh content ----
          // #!/bin/bash
          // my_first_variable="contentoffirstvar"
          // my_second_variable="contentofsecondvar"
          // echo "my_first_variable=$my_first_variable;my_second_variable=$my_second_variable"
          // -------

          def VARIABLES = sh (
              script: '/var/jenkins_home/test.sh',
              returnStdout: true
          ).trim().split(";")
          VARIABLES.each { var ->
              def (var_name, var_value) = var.split('=')
              env."${var_name}" = var_value
          }
        }
      }
    }
  }
}

as point for improvement you can add pattern like

===
my-var1=value1
my-var2=value2
===

and split + find by pattern as you want

huangapple
  • 本文由 发表于 2023年7月13日 00:10:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定