Conditionals In AWS Buildspec Files

If you’re using AWS’s Codebuild to compile your source code, run tests or product software packages then you’re familiar with a buildspec.yml file. A buildspec is “collection of build commands and related settings, in YAML format, that CodeBuild uses to run a build”. You might want to add some conditionals to your buildspec file as this Github Issue asks about Let’s say for example you want to perform some tasks based on a specific environment:

build:
  commands:
  - |
  if [ "$ENVIRONMENT" = "dev" ] || [ "$ENVIRONMENT" = "staging" ]; then
    docker build -f ./docker/Dockerfile .
  fi

Want another simpler conditional in there as well?!:

build:
  commands:
  - |
  if [ "$ENVIRONMENT" = "dev" ] || [ "$ENVIRONMENT" = "staging" ]; then
    if expr "${do_build}" : 'true' >/dev/null; then
      docker build -f ./docker/Dockerfile .
    fi
  fi

Read more about it here