简体   繁体   English

如何在此 buildspec.yml 文件中使用 env 变量?

[英]How can I use env variables in this buildspec.yml file?

I've been building out this buildspec.yml file to configure my CodeBuild integration and while I can inject variables everywhere else in the script, it fails when I try to repeat the pattern in the printf section shown below.我一直在构建这个buildspec.yml文件来配置我的 CodeBuild 集成,虽然我可以在脚本的其他地方注入变量,但当我尝试在下面显示的printf部分重复模式时它失败了。 I'm out of ideas in regards to setting it up and I'm curious if anyone can point me in the right direction.我对设置它没有想法,我很好奇是否有人能指出我正确的方向。

version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .
      - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...
      - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG
      - echo Writing image definitions file...
      - printf '[{"name":"<HOW_TO_PLACE_VAR_HERE>","imageUri":"<HOW_TO_PLACE_VAR_HERE>"}]' $IMAGE_REPO_NAME:$IMAGE_TAG > imagedefinitions.json

       ^ how would I inject variables here? the $PATTERN doesn't work.
artifacts:
  files:
    - '**/*'
    - 'imagedefinitions.json'
  name: s3-to-s3-latest-build.zip
  discard-paths: no

printf '[{"name":"<HOW_TO_PLACE_VAR_HERE>","imageUri":"<HOW_TO_PLACE_VAR_HERE>"}]' $IMAGE_REPO_NAME:$IMAGE_TAG > imagedefinitions.json printf '[{"name":"<HOW_TO_PLACE_VAR_HERE>","imageUri":"<HOW_TO_PLACE_VAR_HERE>"}]' $IMAGE_REPO_NAME:$IMAGE_TAG > imagedefinitions.json

Firstly, you are sending the output to a file, so probably you have not checked the actual contents.首先,您将输出发送到文件,因此您可能没有检查实际内容。

Secondly, you can go with echo statement as you have used elsewhere其次,您可以使用您在其他地方使用过的echo语句

echo '[{"name":"'$IMAGE_REPO_NAME'"}]' echo '[{"name":"'$IMAGE_REPO_NAME'"}]'

you can still send the contents to a file by redirecting the stdout to required file您仍然可以通过将标准输出重定向到所需文件来将内容发送到文件

echo '[{"name":"'$IMAGE_REPO_NAME'"}]' > imagedefinitions.json echo '[{"name":"'$IMAGE_REPO_NAME'"}]' > imagedefinitions.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用在 Codebuild 控制台中定义的环境变量,在 buildspec.yml 中 - How to use environment variables defined in the Codebuild console, within the buildspec.yml 如何将环境变量添加到CodeBuild buildspec.yml? - How Do You Add Environment Variables to CodeBuild buildspec.yml? 如何在 buildspec.yml 文件工件中添加 .platform - How to add .platform in buildspec.yml file artifacts 如何使用 AWS CodePipeline 为 Laravel 应用程序正确创建 buildspec.yml 文件 - How do I properly create buildspec.yml file for Laravel Application using AWS CodePipeline Spring 引导项目的 buildspec.yml 文件 - buildspec.yml file for Spring Boot project AWS CodeBuild - BuildSpec.yml - 导出变量未解析值 - AWS CodeBuild - BuildSpec.yml - exported-variables are not resolving values 如何在buildspec.yml中正确定义阶段和命令? - How to correctly define stages & commands in buildspec.yml? 如何将环境变量传递给 AWS 代码构建的 buildspec.yml - How to pass environment variable to the buildspec.yml for AWS codebuild AWS CodeBuild:如何强制 buildspec.yml 在出错时退出 - AWS CodeBuild: How to force buildspec.yml quit on error 我们如何在buildspec.yml中获取最新的Packer版本? - How do we fetch latest packer release in buildspec.yml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM