且构网

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

无法更改Jenkins字符串参数变量

更新时间:2023-11-15 15:21:16

每个"执行Shell "或任何其他构建步骤都会启动一个新的独立环境.这个新环境继承了实际环境变量和为该作业定义的所有构建参数的副本,但意识到它们是副本/继承.

Each "Execute Shell" or any other build step initiates a new and separate environment. This new environment inherits a copy of actual environment variables and all build parameters that are defined for the job, but realize that they are copies/inherited.

您可以轻松更改环境变量的值:

You can change the value of an environment variable easily:

  • param = new_value(在Unix中)
  • set param = new_value(在Windows中)

但是,对于执行外壳"步骤的那个实例,该更改将本地.如果您在同一执行外壳"步骤中回显该变量,则可以看到更改,但是在下一个执行外壳"中,您将获得一个新副本(具有原始值).

However that change will be local to that instance of the "execute shell" step. You can see the change if you echo that variable within the same "execute shell" step, but in the next "execute shell", you get a new copy (with original value).

要在构建步骤之间(或在此作业之间)保留更改的变量,您需要在第一步中保存它,并在下一步中加载它.最简单的是将值输出到文件:
echo param=$param > temp.props
然后使用 EnvInject插件将此文件读入第二个执行外壳"步骤>(请注意,您需要在现有的两个执行外壳"步骤之间配置 EnvInject 构建步骤.

To preserve your changed variable between build steps (or between jobs for that matter), you need to save it during the first step, and load it during the next. Easiest is to output the value to a file:
echo param=$param > temp.props
And then read this file into the second "execute shell" step using EnvInject plugin (note, you will need to configure EnvInject build step in between your 2 existing "execute shell" steps.