6

My build is triggered on every push to the repo and on every pull request.

So CODEBUILD_SOURCE_VERSION looks like "pr/8" or "4570d2e7158cfef687af8da31d1ffec7b02e5ca3".

I only want the build to execute for pr branches and pushes to master. What is the best way to achieve this? I don't want to use CodeDeploy as I am just deploying lambdas.

I could write a bash function that checks CODEBUILD_SOURCE_VERSION on the install phase and does an exit 1. But this will create a lot of false positives in our slack channel as these are not really "failed builds".

mikestaub
  • 2,134
  • 4
  • 24
  • 34
  • You could exit with 0 if Slack notifications is your only concern. I agree though it would be better to skip the build altogether. – mahemoff Feb 23 '20 at 02:06

2 Answers2

5

You can configure event filters and do exactly what you want, here is an example of configuration building PRs or pushes on a master branch

enter image description here

It's all here https://docs.aws.amazon.com/codebuild/latest/userguide/sample-github-pull-request.html

2

Your approach is going to depend on what is triggering your build events and then you'll have to route those events to codebuild. If you're using Github, you could explicitly mark which events you want to trigger your system by creating a webhook that fires on push and pull_request events. That webhook sends a post request into your system, which is going to need some code to convert that request into starting a codebuild build. I don't think you want to parse things after the build has started to see if the build should continue. It is better to not start the build at all if it shouldn't run.

jstewart379
  • 436
  • 4
  • 9
  • I understand I can just move my logic out of the buildspec.yml and into some service, but I was hoping I could remove that logic completely. Too bad I can't create a webhook for a specific branch on github. Your suggestion seems like the best approach, thanks. – mikestaub Feb 27 '18 at 14:12