Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger an AWS CodePipeline on every new pull request in GitHub repo

Source code in my organization is managed in a GitHub repository. For now, our CI process uses AWS CodePipeline as follows:

  • Webhooks detect code changes in a specific git branch
  • The updated branch is then used the input for AWS CodeBuild
  • The finished build is deployed onto one of our staging environments using Elastic Beanstalk
  • Tests are run on the Elastic Beanstalk environment.

We want to add detection of new pull requests in our git repository. Whenever a new PR is created in our repo, we'd like to automatically trigger a build to an EB environment, through CodePipeline as above.

Our roadblocks:

  • Looking at the available settings for GitHub Webhooks in CodePipeline, we cannot find a way to specify that the pipeline's trigger should be a new PR.
  • In any case, the GitHub source for a CodePipeline must be a specific branch. We'd like PRs to be detected in any branch.

What would be the best approach here? I've seen some methods being discussed, but most of them appear to be on the cumbersome/high-maintenance side. If there's anything new in the AWS toolchain that makes this easy, it'd be cool to know.

Thanks!

like image 348
a-k-c-l Avatar asked Nov 06 '22 12:11

a-k-c-l


1 Answers

The best approach to solving this problem seems to be creating a CodePipeline for each PR using a parameterized CloudFormation stack.

Essentially the steps are:

  1. Define your CodePipeline using CloudFormation and have a parameter that identifies the environment - Prod, QA, PR_xyz etc.
  2. Set up CodeBuild to trigger on any changes to your GitHub repository. When a new PR is created, have CodeBuild construct a new CodePipeline based on your CloudFormation template. Supply the name of the PR as the environment name when creating the CloudFormation stack.

Detailed steps are described here: https://moduscreate.com/blog/track-git-branches-aws-codepipeline/

like image 150
bikeman868 Avatar answered Nov 15 '22 07:11

bikeman868