Bill
Bill

Reputation: 19338

AWS codebuild not running terraform at post_build

I am trying to set up the build for my project on aws codebuild and i am using terraform to setup the all the instances, route53 and ect... but after docker push the terraform script never gets executed. i am wondering why. (I am new to this...)

version: 0.2

phases:
  install:
    commands: |
      echo Running docker daemon
      nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2375 --storage-driver=overlay&
  build:
    commands: |
      docker build -t mywebsite .
  post_build:
    commands: |

      IMAGE_TAG=`echo $CODEBUILD_BUILD_ID`
      IMAGE_PATH="$IMAGE_PATH_PREFIX:$IMAGE_TAG"
      docker tag mywebsite $IMAGE_PATH
      docker push $IMAGE_PATH
      - cd ./buildTools/terraform
      - terraform init
      - terraform apply

output at the end in the build history:

sha256:xxxxxx size: 3066

[Container] 2018/01/11 22:33:08 Phase complete: POST_BUILD Success: true
[Container] 2018/01/11 22:33:08 Phase context status code: Message: 

and my terraform script were never run at the end? please help.

thanks

Upvotes: 0

Views: 758

Answers (2)

Clare Liguori
Clare Liguori

Reputation: 1650

From your YAML, it looks like you're trying to mix a single multi-line value (IMAGE_TAG...) with another list of values (your terraform scripts) for the post_build commands. The effect is that "commands" will be set to only that first single multi-line value, and the list is disgarded.

The commands item is intended to be a YAML list: https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html

For example:

version: 0.2

phases:
  install:
    commands:
      - echo Running docker daemon
      - nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2375 --storage-driver=overlay&
  build:
    commands:
      - docker build -t mywebsite .
  post_build:
    commands:
      - IMAGE_TAG=`echo $CODEBUILD_BUILD_ID`
      - IMAGE_PATH="$IMAGE_PATH_PREFIX:$IMAGE_TAG"
      - docker tag mywebsite $IMAGE_PATH
      - docker push $IMAGE_PATH
      - cd ./buildTools/terraform
      - terraform init
      - terraform apply

Upvotes: 2

Yusinto Ngadiman
Yusinto Ngadiman

Reputation: 176

You need to get rid of the dashes "-" because you are already using the bulk shell script symbol "|". So your post_build step should look like this:

 post_build:
 commands: |
  IMAGE_TAG=`echo $CODEBUILD_BUILD_ID`
  IMAGE_PATH="$IMAGE_PATH_PREFIX:$IMAGE_TAG"
  docker tag mywebsite $IMAGE_PATH
  docker push $IMAGE_PATH
  cd ./buildTools/terraform
  terraform init
  tterraform apply

Upvotes: 1

Related Questions