我正在尝试将 AWS CodeBuild 用于我的项目。我的构建正在aws/codebuild/docker:1.12.1
图像上运行。这是我的buildspec.yml
,取自AWS Docker 示例
version: 0.1
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- $(aws ecr get-login --region $AWS_DEFAULT_REGION)
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t $IMAGE_REPO_NAME .
post-build:
commands:
- echo Build completed on `date`
- docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG
- docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG
所有环境变量都在构建配置中定义。但这是我的 CodeBuild 日志输出:
[Container] 2017/02/16 22:04:24 Waiting for agent
[Container] 2017/02/16 22:04:33 Phase is DOWNLOAD_SOURCE
[Container] 2017/02/16 22:04:34 Source is located at /tmp/src733484785/src
[Container] 2017/02/16 22:04:34 YAML location is /tmp/src733484785/src/buildspec.yml
[Container] 2017/02/16 22:04:34 Registering with agent
[Container] 2017/02/16 22:04:34 Phases found in YAML: 3
[Container] 2017/02/16 22:04:34 PRE_BUILD: 2 commands
[Container] 2017/02/16 22:04:34 BUILD: 3 commands
[Container] 2017/02/16 22:04:34 POST-BUILD: 3 commands
[Container] 2017/02/16 22:04:34 Phase complete: DOWNLOAD_SOURCE Success: true
[Container] 2017/02/16 22:04:34 Phase context status code: Message:
[Container] 2017/02/16 22:04:34 Processing plaintext environment variables
[Container] 2017/02/16 22:04:34 Processing build-level environment variables
[Container] 2017/02/16 22:57:53 {"AWS_DEFAULT_REGION":"<censored>","AWS_ACCOUNT_ID":"<censored>","IMAGE_TAG":"<censored>","IMAGE_REPO_NAME":"<censored>"}
[Container] 2017/02/16 22:57:53 AWS_DEFAULT_REGION = <censored>
[Container] 2017/02/16 22:57:53 AWS_ACCOUNT_ID = <censored>
[Container] 2017/02/16 22:57:53 IMAGE_TAG = <censored>
[Container] 2017/02/16 22:57:53 IMAGE_REPO_NAME = <censored>
[Container] 2017/02/16 22:04:34 Processing builtin environment variables
[Container] 2017/02/16 22:04:34 Moving to directory /tmp/src733484785/src
[Container] 2017/02/16 22:04:34 Preparing to copy artifacts
[Container] 2017/02/16 22:04:34 No artifact files specified
CodeBuild 会看到我的命令,但它不会运行它们!构建被标记为成功。有谁知道我在这里做错了什么?
更新:我需要更改post-build
为post_build
. build 现在拉取 maven 镜像来构建 jar,然后 docker 构建镜像。这样,我的jar不包含源代码,镜像也不包含maven和jdk。这是我的工作buildspec.yml
:
version: 0.1
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- $(aws ecr get-login --region $AWS_DEFAULT_REGION)
- echo Pulling maven image...
- docker pull maven:3.3-jdk-8
build:
commands:
- echo Build started on `date`
- echo Building jar...
- docker run -i --rm -w /opt/maven -v $PWD:/opt/maven -v $HOME/.m2:/root/.m2 maven:3.3-jdk-8 mvn clean install
- echo Building Docker Image...
- docker build -t $IMAGE_REPO_NAME .
- 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 Docker image...
- docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG