Skip to content

Instantly share code, notes, and snippets.

@mikebroberts
Created January 2, 2019 19:08
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mikebroberts/37bb031eae1d8b8c26fe87eac6aae59d to your computer and use it in GitHub Desktop.
Save mikebroberts/37bb031eae1d8b8c26fe87eac6aae59d to your computer and use it in GitHub Desktop.
Selectively triggering a GitHub sourced CodePipeline (only for one file)
# CodePipeline by default runs an execution whenever any change is detected in the configured source repository
# We can use a CodePipeline Webhook resource to filter such executions.
#
# This is a snippet that would be part of a CloudFormation template containing
# a CodePipeline resource (AWS::CodePipeline::Pipeline), named CodePipeline in this case, and
# assumes the GutHub OAuth token is available in the parameter GitHubOAuthToken.
# Typically a CodePipeline Webhook only contains the $.ref filter to check for
# the desired branch.
# However we can add up to 4 more filters, each of which can query the incoming webhook payload from Github.
# Such payloads are of the form:
# {
# ...
# "commits": [
# {
# ...
# "added": [
# "addedfile1.txt",
# ...
# ],
# "removed": [
#
# ],
# "modified": [
# "README.md",
# "modifiedfile2.txt"
# ]
# }
# ],
# ...
# }
# ... and so we can query the commits object in a JSON Path expression.
# The example here will therefore only trigger a pipeline if the README.md file is updated
# References:
# https://docs.aws.amazon.com/codepipeline/latest/APIReference/API_WebhookDefinition.html
# https://docs.aws.amazon.com/codepipeline/latest/APIReference/API_WebhookFilterRule.html
GithubWebhook:
Type: 'AWS::CodePipeline::Webhook'
Properties:
RegisterWithThirdParty: 'true'
Authentication: GITHUB_HMAC
AuthenticationConfiguration:
SecretToken: !Ref GitHubOAuthToken
Filters:
- JsonPath: "$.ref"
MatchEquals: refs/heads/{Branch}
# Only trigger pipeline if README is updated
Filters:
- JsonPath: "$.commits[*].*[?(@ == 'README.md')]"
MatchEquals: README.md
TargetPipeline: !Ref CodePipeline
TargetAction: Source
TargetPipelineVersion: !GetAtt CodePipeline.Version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment