简体   繁体   English

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

[英]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中的以下命令
  • and of course my artifacts end in the wrong place.当然,我的文物在错误的地方结束。

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中重复相同的代码

questions问题

  1. How do I propagate environment variables computed at build-time between different phases.*.commands ?如何在不同phases.*.commands
  2. Why is $(...) 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.

相关问题 在Jenkins中构建成功,但AWS-Codebuild正在给出插件错误 - Build is successful in Jenkins but AWS-Codebuild is giving plugin error 在适用于Android构建的AWS Codebuild中设置环境 - Set environment in AWS codebuild for Android build 如何使用 aws aws-codebuild、codedeploy、cloudformation 的服务为 aws lambda 与 java 配置持续集成 - How to configure continuous integration for aws lambda with java using the services of aws aws-codebuild, codedeploy, cloudformation 用于版本控制的 AWS CodeBuild 环境变量? - AWS CodeBuild environment variables for versioning? AWS CodeBuild 默认环境变量 - AWS CodeBuild Default Environment Variables AWS CodeBuild Visual Studio 2019 构建工具 - AWS CodeBuild Visual Studio 2019 build tools AWS CodePipline & Codebuild - 如何向 Docker-Image 添加环境变量? - AWS CodePipline & Codebuild - How to add environment variables to the Docker-Image? 如何在 aws codebuild 中基于 $CODEBUILD_BUILD_SUCCEEDING 环境变量使构建失败? - How to fail the build based on $CODEBUILD_BUILD_SUCCEEDING environment variable in aws codebuild? AWS CodeBuild 默认环境变量。 - AWS CodeBuild default environment variables. AWS CodeBuild Batch 配置未传递环境变量 - AWS CodeBuild Batch configuration not passing environment variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM