如何使用 buildspec.yml 从 AWS CodePipeline 中的 Dockerfile 构建镜像,然后推送到 ECR?
How to build Image from Dockerfile in AWS CodePipeline using buildspec.yml, and then push to ECR?
我在 CodeCommit 存储库中有一个 Dockerfile。我正在构建一个包含两个阶段的管道,它应该:
- 连接到 CodeCommit 源(成功)
- 从存储库中的 Dockerfile 构建映像并将其推送到 ECR(未成功)
我尝试了以下操作,但由于命令 IMAGE_URI= $REPO_URI:$IMAGE_TAG.
,它以退出状态 127 退出
CodeBuildProject:
Type: AWS::CodeBuild::Project
DependsOn: CodeBuildServiceRole
Properties:
Name: pipeline
Artifacts:
Type: CODEPIPELINE
Environment:
Type: LINUX_CONTAINER
ComputeType: !FindInMap [BuildPowerMap, !Ref BuildPower, ComputeType]
Image: aws/codebuild/standard:2.0
EnvironmentVariables:
- Name: AWS_DEFAULT_REGION
Value: !Ref AWS::Region
- Name: REPO_URI
Value: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${ContainerRepo}
- Name: AWS_ACCOUNT_ID
Value: !Ref AWS::Region
PrivilegedMode: true
ServiceRole: !Ref CodeBuildServiceRole
Source:
Type: CODEPIPELINE
BuildSpec: |
version: 0.2
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION)
- CODEBUILD_RESOLVED_SOURCE_VERSION="${CODEBUILD_RESOLVED_SOURCE_VERSION:-$IMAGE_TAG}"
- IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- IMAGE_URI= $REPO_URI:$IMAGE_TAG
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build --tag "$IMAGE_URI" .
post_build:
commands:
- bash -c "if [ /"$CODEBUILD_BUILD_SUCCEEDING/" == /"0/" ]; then exit 1; fi"
- echo Build completed on `date`
- echo Pushing the Docker image...
- docker push "$IMAGE_URI"
- printf '[{"name":"nginx","imageUri":"%s"}]' "$IMAGE_URI" > images.json
artifacts:
files: images.json
来自 AWS CodeBuild 的错误消息,在我 运行 管道之后:
根据评论。
问题是由 space 引起的:
IMAGE_URI= $REPO_URI:$IMAGE_TAG
应该是:
IMAGE_URI=$REPO_URI:$IMAGE_TAG
我在 CodeCommit 存储库中有一个 Dockerfile。我正在构建一个包含两个阶段的管道,它应该:
- 连接到 CodeCommit 源(成功)
- 从存储库中的 Dockerfile 构建映像并将其推送到 ECR(未成功)
我尝试了以下操作,但由于命令 IMAGE_URI= $REPO_URI:$IMAGE_TAG.
,它以退出状态 127 退出CodeBuildProject:
Type: AWS::CodeBuild::Project
DependsOn: CodeBuildServiceRole
Properties:
Name: pipeline
Artifacts:
Type: CODEPIPELINE
Environment:
Type: LINUX_CONTAINER
ComputeType: !FindInMap [BuildPowerMap, !Ref BuildPower, ComputeType]
Image: aws/codebuild/standard:2.0
EnvironmentVariables:
- Name: AWS_DEFAULT_REGION
Value: !Ref AWS::Region
- Name: REPO_URI
Value: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${ContainerRepo}
- Name: AWS_ACCOUNT_ID
Value: !Ref AWS::Region
PrivilegedMode: true
ServiceRole: !Ref CodeBuildServiceRole
Source:
Type: CODEPIPELINE
BuildSpec: |
version: 0.2
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION)
- CODEBUILD_RESOLVED_SOURCE_VERSION="${CODEBUILD_RESOLVED_SOURCE_VERSION:-$IMAGE_TAG}"
- IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- IMAGE_URI= $REPO_URI:$IMAGE_TAG
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build --tag "$IMAGE_URI" .
post_build:
commands:
- bash -c "if [ /"$CODEBUILD_BUILD_SUCCEEDING/" == /"0/" ]; then exit 1; fi"
- echo Build completed on `date`
- echo Pushing the Docker image...
- docker push "$IMAGE_URI"
- printf '[{"name":"nginx","imageUri":"%s"}]' "$IMAGE_URI" > images.json
artifacts:
files: images.json
来自 AWS CodeBuild 的错误消息,在我 运行 管道之后:
根据评论。
问题是由 space 引起的:
IMAGE_URI= $REPO_URI:$IMAGE_TAG
应该是:
IMAGE_URI=$REPO_URI:$IMAGE_TAG