4
votes

I am trying to use AWS CodeBuild for my project. My build is running on the aws/codebuild/docker:1.12.1 image. This is my buildspec.yml, which was taken from the AWS Docker Sample

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

All of the environment variables are defined in the build configuration. But this is my CodeBuild log output:

[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 sees my commands, but it doesn't run them! The build is marked as successful. Does anyone know what I've done wrong here?

UPDATE: I needed to change post-build to post_build. The build now pulls the maven image to build the jar, and then docker builds the image. This way, my jar doesn't contain the source code, and the image doesn't contain maven and the jdk. This is my working 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
1
I don't know what I did, but it started working. Does anyone know what the source of flakiness might have been? - zalpha314
did you figure out why you got this to work? I think I'm having the same issue - cdimitroulas
@cdimitroulas, no, but I think it had to do with the indentation and spacing of the lists. YAML is very picky about all that. - zalpha314
do you have the latest version of your buildspec.yml? It would be extremely useful for me to see that and compare with what you had originally posted :) - cdimitroulas
@cdimitroulas, funny, double-spacing works for me. I've posted my working buildspec.yml for you. - zalpha314

1 Answers

9
votes

post-build should be post_build as per the Build Specification Reference.