Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger CodePipeline for GitHub pull requests being merged?

How can I configure CodePipeline to be triggered for Pull Requests being opened, edited or merged?

Here is a Terraform configuration:

resource "aws_codepipeline_webhook" "gh_to_codepipeline_integration" {
  name            = "gh_to_codepipeline_integration"
  authentication  = "GITHUB_HMAC"
  target_action   = "Source"
  target_pipeline = aws_codepipeline.mycodepipeline.name

  authentication_configuration {
    secret_token = var.github_webhook_secret
  }

  // accept pull requests
  // Is there a way to filter on the PR being closed and merged?  This isn't it...
  filter {
    json_path    = "$.action"
    match_equals = "closed"
  }

}

CodePipeline is set to accept webhook events that have all of the conditions specified in the filters, which corresponds to Pull Request Events.

Note that the GitHub documentation states for the action field of a PullRequestEvent (my emphasis in bold):

The action that was performed. Can be one of assigned, unassigned, review_requested, review_request_removed, labeled, unlabeled, opened, edited, closed, ready_for_review, locked, unlocked, or reopened. If the action is closed and the merged key is false, the pull request was closed with unmerged commits. If the action is closed and the merged key is true, the pull request was merged. While webhooks are also triggered when a pull request is synchronized, Events API timelines don't include pull request events with the synchronize action.

It seems like I need to filter for both $.action==closed && $.pull_request_merged=true, but it doesn't look like I can do both. If I just filter on $.action==closed then my pipeline will rebuild if PRs are closed without merging. Is this an oversight on my part, or are CodePipelines not as flexible in their triggers as CodeBuild projects?

like image 659
John Avatar asked Nov 13 '19 14:11

John


People also ask

How do I accept a merge pull request on GitHub?

To accept the pull request, click the Pull Requests tab to see a summary of pending pull requests. If you are happy with the changes, click Merge Pull request to accept the pull request and perform the merge. You can add in a comment if you want. Once you click Merge Pull request, you will see a button Confirm merge.

How do I know if a pull request is merged?

Pull requests are closed automatically whenever the maintainer merge the changes through the web interface. If he merged using the command line, it will be closed as soon as he pushes the code back to Github. So if a PR is still open, it means it is not merged.

How can you automate AWS CodePipeline execution?

Your pipeline runs automatically only when something changes in the source repository and branch that you have defined. Manually: You can use the console or the AWS CLI to start a pipeline manually. For information, see Start a pipeline manually. On a schedule: You can set up a schedule that starts your pipeline.


1 Answers

For pull requests being opened/updated, because CodePipeline's Git integrations require a branch name, this is not natively supported as the branch name is variable, unless you open PRs on long running branches like dev, qa etc (e.g. if you are using a Gitflow-based workflow).

The way that we support PRs based from dynamic branches is use CodeBuild for the build/unit test stage of our workflow, and then package up the repository and build artefacts to S3. From there we trigger Deployment pipelines for integration and acceptance environments using S3 artefact as the source. Using CodePipeline for deployments is powerful as it automatically ensures only one stage can execute at a time, meaning only one change for a given application is going through a given environment at any one time.

This approach is however quite complex and requires quite a bit of Lambda magic mixed with SQS FIFO queues to deal with concurrent PRs (this is to overcome the superseding behaviour of CodePipeline), but it's quite a powerful pattern. We also use GitHub reviews to do things like trigger acceptance stage, and auto-approve manual approval steps in CodePipeline.

Once you are ready to merge the PR, we just use normal CodePipeline triggered off master to deploy to production - one thing you also need to do is ensure you use the artefact that was built and tested on the PR.

like image 179
mixja Avatar answered Oct 16 '22 06:10

mixja