[英]How to set environment variables in AWS-codebuild for Windows 2019 at build time?
It seems that in AWS-codebuild variables are not propagated between commands for the windows 2019 environment.在 AWS-codebuild 中,变量似乎不会在 windows 2019 环境的命令之间传播。
With this buildspec.yml
有了这个
buildspec.yml
version: 0.2
env:
variables:
MY_VAR_0: $(git log -n 1 --date=short --pretty=format:%cd_%h)
phases:
build:
commands:
- $Env:MY_VAR_1 = & git log -n 1 --date=short --pretty=format:%cd_%h
- Get-ChildItem Env:MY_VAR_*
# build commands here
artifacts:
name: $MY_VAR_0
I get in the logs:我进入日志:
[Container] 2020/12/14 11:41:27 Entering phase BUILD
[Container] 2020/12/14 11:41:27 Running command $Env:MY_VAR_1 = & git log -n 1 --date=short --pretty=format:%cd_%h
[Container] 2020/12/14 11:41:27 Running command Get-ChildItem Env:MY_VAR_*
Name Value
---- -----
MY_VAR_0 $(git log -n 1 --date=short --pretty=format:%...
[Container] 2020/12/14 11:41:28 Phase complete: BUILD State: SUCCEEDED
The problem here are这里的问题是
MY_VAR_0
is set to the string $(git log...
and not the output of the command. MY_VAR_0
设置为字符串$(git log...
而不是命令的 output 。MY_VAR_1
is not propagated to following commands in phases.build.commands
MY_VAR_1
不会传播到phases.build.commands
中的以下命令 Up to now the only way I found to solve this problem is到目前为止,我发现解决此问题的唯一方法是
version: 0.2
phases:
build:
commands:
- |
$Env:MY_VAR_0 = & git log -n 1 --date=short --pretty=format:%cd_%h
$Env:MY_VAR_1 = & git log -n 1 --date=short --pretty=format:%cd_%h
Get-ChildItem Env:MY_VAR_*
# first build command here
- |
$Env:MY_VAR_0 = & git log -n 1 --date=short --pretty=format:%cd_%h
$Env:MY_VAR_1 = & git log -n 1 --date=short --pretty=format:%cd_%h
Get-ChildItem Env:MY_VAR_*
# second build command here
artifacts:
name: $(git log -n 1 --date=short --pretty=format:%cd_%h)
with the following log:使用以下日志:
[Container] 2020/12/14 12:25:18 Entering phase BUILD
[Container] 2020/12/14 12:25:18 Running command $Env:MY_VAR_0 = & git log -n 1 --date=short --pretty=format:%cd_%h
$Env:MY_VAR_1 = & git log -n 1 --date=short --pretty=format:%cd_%h
Get-ChildItem Env:MY_VAR_*
# first build command here
Name Value
---- -----
MY_VAR_1 2020-12-14_eccfb77
MY_VAR_0 2020-12-14_eccfb77
[Container] 2020/12/14 12:25:19 Running command $Env:MY_VAR_0 = & git log -n 1 --date=short --pretty=format:%cd_%h
$Env:MY_VAR_1 = & git log -n 1 --date=short --pretty=format:%cd_%h
Get-ChildItem Env:MY_VAR_*
# second build command here
Name Value
---- -----
MY_VAR_1 2020-12-14_eccfb77
MY_VAR_0 2020-12-14_eccfb77
[Container] 2020/12/14 12:25:20 Phase complete: BUILD State: SUCCEEDED
What I do not like in this approach is that I have to repeat the code for computing the MY_VAR_* values at the begin of each build command.我不喜欢这种方法的是,我必须在每个构建命令开始时重复计算 MY_VAR_* 值的代码。 (And no, I do not consider feasible to have a single, multiline huge build command.) Moreover the same code has to be repeated in
artifacts.name
(不,我不认为有一个单一的、多行的巨大构建命令是可行的。)此外,必须在
artifacts.name
中重复相同的代码
phases.*.commands
?phases.*.commands
?$(...)
expanded in artifacts.name
but not in env.variables.MY_VAR_0
?$(...)
在artifacts.name
中展开,但在env.variables.MY_VAR_0
中没有展开? Answer to #1:回答#1:
it looks like setting Environment variables in Env:
drive silently fails.看起来像在
Env:
驱动器静默失败。 However, using local variable syntax $Foo = "bar"
seems to work between commands as well as phases.但是,使用局部变量语法
$Foo = "bar"
似乎在命令和阶段之间起作用。
For example, in the following:例如,在下面:
version: 0.2
env:
shell: powershell.exe
phases:
pre_build:
commands:
- $foo = "bar"
- echo $foo
build:
commands:
- echo $foo
both echo
commands print bar
.两个
echo
命令都打印bar
。
Answer to #2:回答#2:
artifacts.name
always uses the shell
language (unix), where when setting a variable you are in either cmd.exe
or powershell.exe
depending on what you have env.shell
set to in the buildspec. artifacts.name
always uses the shell
language (unix), where when setting a variable you are in either cmd.exe
or powershell.exe
depending on what you have env.shell
set to in the buildspec. Reference 参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.