Featured image of post Pull Request Merge Workflow Event

Pull Request Merge Workflow Event

Setting up infrastructure to trigger a lambda function when a pull request is merged.

Hugo is supported for deploy on many platforms where this setup is not necessary.

But this is an application I can learn, understand how things work, and customize my own; and it’s fun!

Planning

In this phase, I’m going to plan and build out the CI/CD pipeline for when a pull request is merged to main it triggers a CodePipeline from a lambda function.

First, we’ll add the pull reqeust merged event API to the Dev Workflow pipeline. Eventually, we’ll update this to trigger CodePipeline.

  1. GitHub event triggered (PR-merged)
  2. Lambda Dispatcher
  3. SNS Pull Request – merged
  4. Lambda Pull Request – merged (Hello world logs)

Once that is complete, we’ll break down these steps and get those working.

  • 🔲 Setup CodeBuild
  • 🔲 Setup CodePipeline
  • 🔲 Setup Lambda Pull Request – merged to trigger CodePipeline

Working backwards

Since the foundation infrastructure is already in place, let’s work backwards to implement the pull request merged function.

I’ll be working from the smylee-dev-workflows repo at this point in time.

Lambda Pull Request – merged

Fist I’ll create the files that will be needed for the lambda function. It will simply log the event that triggers the lambda function.

File structure of Pull Request Merged

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import logging
import os

logger = logging.getLogger()
logger.setLevel(os.environ.get("LOGGING_LEVEL", logging.DEBUG))


def lambda_handler(event, context):
    logger.debug(event)

    return "done"

Infrastructure to run Lambda Pull Request – merged

Then I update the infrastructure code to include the new lambda function and SNS topic.

Then I updated the dispatcher to look for this new event and sent a message to the Pull Request Merged SNS topic.

flowchart TD A[GitHub]-->|/github_dispatcher endpoint|B[API Gateway] B-->|triggers|C[Lambda Dispatcher] C-->|sends message to|D[SNS PR Merged] D-->|triggers|E[Lambda PR Merged]

☝️ Checkout this post on how to set up mermaid diagrams on hugo!

So now the dispatcher knows how to send a message if it’s pull request merge.

Next we’ll set up Code Build to trigger and then do the hugo site updates when the PR is merged to main.

Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy