如何在构建时为 Windows 2019 在 AWS-codebuild 中设置环境变量?

How to set environment variables in AWS-codebuild for Windows 2019 at build time?

似乎在 AWS-codebuild 中变量不会在 windows 2019 环境的命令之间传播。

有了这个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

我进入日志:

[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

这里的问题是


到目前为止我发现解决这个问题的唯一方法是

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)

具有以下日志:

[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

我不喜欢这种方法的是,我必须在每个构建命令的开头重复计算 MY_VAR_* 值的代码。 (不,我认为拥有一个单一的、多行的巨大构建命令是不可行的。)此外,必须在 artifacts.name

中重复相同的代码

问题

  1. 如何在不同 phases.*.commands 之间传播构建时计算的环境变量?
  2. 为什么 $(...)artifacts.name 中展开但在 env.variables.MY_VAR_0 中没有展开?

对 #1 的回答:

似乎在 Env: 驱动器中设置环境变量静默失败。但是,使用局部变量语法 $Foo = "bar" 似乎可以在命令和阶段之间工作。

例如,在下面:

version: 0.2
env:
  shell: powershell.exe
phases:
  pre_build: 
    commands:
      - $foo = "bar"
      - echo $foo
  build:
    commands:
      - echo $foo

两个 echo 命令打印 bar.

#2 的答案:

artifacts.name 始终使用 shell 语言 (unix),在设置变量时,您处于 cmd.exepowershell.exe 中,具体取决于您拥有 env.shell 在构建规范中设置为。 Reference