每当推送到我的源代码并将docker镜像移动到ECR(EC2 Container Registry)时,我都会尝试构建一个docker镜像。
我尝试使用以下构建规范文件
version: 0.2
env:
variables:
IMG: "app"
REPO: "<<zzzzzzzz>>.dkr.ecr.us-east-1.amazonaws.com/app"
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws ecr get-login --region us-east-1
- TAG=echo $CODEBUILD_RESOLVED_SOURCE_VERSION | head -c 8
build:
commands:
- echo $TAG
- docker build -t $IMG:$TAG .
- docker tag $IMG:$TAG $REPO:$TAG
post_build:
commands:
- docker push $REPO:$TAG
- printf Image":"%s:%s" $REPO $TAG > build.json
artifacts:
files: build.json
discard-paths: yes
当我构建这个时,我在docker build -t
收到错误invalid reference format
我查看了文档但没有找到任何帮助。
答案 0 :(得分:1)
TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | head -c 8)
你可以使用$()
version: 0.2
phases:
install:
commands:
- echo Entered the install phase...
- TAG=$(echo "This is test")
pre_build:
commands:
- echo $TAG
build:
commands:
- echo Entered the build phase...
- echo Build started on $TAG
日志:
[Container] 2018/03/17 16:15:31 Running command TAG=$(echo "This is test")
[Container] 2018/03/17 16:15:31 Entering phase PRE_BUILD
[Container] 2018/03/17 16:15:31 Running command echo $TAG
This is test
答案 1 :(得分:1)
经过多次重试后,我终于找到了我的错误。
Env CODEBUILD_RESOLVED_SOURCE_VERSION 应替换为 CODEBUILD_SOURCE_VERSION 环境变量,因为我正在使用codebuild直接从GitHub中的源代码库构建。
要登录ecr,需要添加--no-include-email
选项并使用$()包装命令。这将允许您运行docker登录。我更新的buildspec文件类似于
version: 0.2
env:
variables:
REPO: "184665364105.dkr.ecr.us-east-1.amazonaws.com/app"
phases:
pre_build:
commands:
- echo $CODEBUILD_SOURCE_VERSION
- TAG=$(echo $CODEBUILD_SOURCE_VERSION | head -c 8)
- echo Logging in to Amazon ECR...
- $(aws ecr get-login --no-include-email --region us-east-1)
build:
commands:
- echo $TAG
- echo $REPO
- docker build --tag $REPO:$TAG .
post_build:
commands:
- docker push $REPO:$TAG