bash 无法将变量从 Jenkins 中的执行 shell 导出到其他项目(使用属性文件)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30938936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:13:45  来源:igfitidea点击:

can't export a variable from execute shell in Jenkins to other project (with using properties file)

bashshelljenkins

提问by FotisK

I have a Jenkins project with one "Execute shell" that executes some bash commands and exports one variable to the env. variables:

我有一个带有一个“执行 shell”的 Jenkins 项目,它执行一些 bash 命令并将一个变量导出到 env。变量:

#!/bin/bash

...
TARGET_FULLPATH="blablabla"
export TARGET_FULLPATH

In addition, I have configured to "Post-build Actions" with:

此外,我已将“构建后操作”配置为:

  1. "Trigger parameterized build on other projects"
  2. "Current build parameters"
  3. "Predefined parameters" -> FULLPATH=$TARGET_FULLPATH
  1. “在其他项目上触发参数化构建”
  2. “当前构建参数”
  3. “预定义参数”-> FULLPATH=$TARGET_FULLPATH

But, when I run this project the other project that will start as soon the first one is finished seems that it doesn't get the parameter FULLPATH at all!

但是,当我运行这个项目时,另一个将在第一个完成后立即启动的项目似乎根本没有获得参数 FULLPATH!

At least in the "Execute shell" (of the second project) the bash script print nothing for echo $FULLPATH! Also at the "Environment Variables" of the second project the FULLPATH is not included!

至少在(第二个项目的)“执行 shell”中,bash 脚本不会为 echo $FULLPATH 打印任何内容!同样在第二个项目的“环境变量”中,FULLPATH 不包括在内!

Any clue what am I doing wrong?

任何线索我做错了什么?

One more thing, i can't use a properties file to store the parameters, since the two projects are running on different servers and there are restrictions on copying files between those servers!

还有一件事,我不能使用属性文件来存储参数,因为这两个项目在不同的服务器上运行,并且在这些服务器之间复制文件有限制!

回答by Bruno Lavit

Did you have a look to this solution? Jenkins: How to use a variable from a pre-build shell in the Maven "Goals and options"

你看过这个解决方案吗? Jenkins:如何在 Maven“目标和选项”中使用预构建 shell 中的变量

Using a shell pre-build step + the InjectEnv plugin, you should be able to solve your problem.

使用 shell 预构建步骤 + InjectEnv 插件,您应该能够解决您的问题。

Update from June 22nd, I add some screen copies.

从 6 月 22 日更新,我添加了一些屏幕副本。

1/ Add a first "Execute shell" script to create the properties file and an "Inject environment variables" step to load the properties file:

1/ 添加第一个“执行 shell”脚本以创建属性文件和“注入环境变量”步骤以加载属性文件:

enter image description here

enter image description here

2/ For the demo, I add a "post-build task" step to read the variable

2/ 对于演示,我添加了一个“构建后任务”步骤来读取变量

enter image description here

enter image description here

3/ Here is the console output, it works :)

3/ 这是控制台输出,它有效:)

enter image description here

enter image description here