Skip to content

Commit 1ee7ea1

Browse files
authored
Merge pull request #7 from toricls/skip-keywords
Implement a config to skip build by commit message
2 parents 1068b3d + 7368697 commit 1ee7ea1

File tree

7 files changed

+25
-9
lines changed

7 files changed

+25
-9
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,11 @@ yes | S3_SAM_ARTIFACTS_BUCKET_NAME | An S3 bucket to store AWS SAM's artifacts.
135135
yes | GITHUB_REPOSITORY_URL | A repository URL you wanted build. Use https style path and make sure trailing '.git' is removed. | https://github.com/your-org/your-repo
136136
yes | GITHUB_PERSONAL_ACCESS_TOKEN | Used for updating GitHub PR's status and Webhook configuration. Minimum scope are `admin:repo_hook` and `repo:status`. You can create and obtain a new token via [settings page](https://github.com/settings/tokens/new). | your-github-personal-access-token
137137
yes | GITHUB_TARGET_RESOURCE | Choose one event to decide when your CodeBuild project runs. Available value is `pr` or `push`. | push
138-
optional | GITHUB_IGNORE_BRANCH_REGEX | Regex string to specify branch name to ignore commit events. This parameter will be enabled only the `GITHUB_TARGET_RESOURCE` value is set to `push`. | wip.*
138+
optional | GITHUB_IGNORE_BRANCH_REGEX | Regex string to specify branch name to ignore commit events. This parameter will be enabled only the `GITHUB_TARGET_RESOURCE` value is `push`. | wip.*
139139
yes | AWS_DEFAULT_REGION | The region where you want to provision this tool via CloudFormation. | us-east-1
140140
yes | CODEBUILD_PROJECT_NAME | The AWS CodeBuild project name you've already configured for your GitHub repository. | your-codebuild-project-name
141141
yes | CODEBUILD_PROJECT_REGION | The region where you've created a CodeBuild project. You can specify a different region from the region of CloudFormation. | us-east-1
142+
optional | BUILD_SKIPPED_BY | Build invocation will be skipped if the head commit message includes the value of this parameter. This parameter will be used only the GITHUB_TARGET_RESOURCE value is `push`. | "skip ci"
142143

143144
#### Deploy
144145

env/example.env

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ GITHUB_IGNORE_BRANCH_REGEX=wip.*
88
AWS_DEFAULT_REGION=us-east-1
99
CODEBUILD_PROJECT_NAME=your-codebuild-project-name
1010
CODEBUILD_PROJECT_REGION=us-east-1
11+
BUILD_SKIPPED_BY="skip ci"

sam.yml

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Parameters:
1515
Type: String
1616
CodeBuildRegion:
1717
Type: String
18+
BuildSkippedBy:
19+
Type: String
1820

1921
Resources:
2022
# AWS SAM doesn't support `Transform` in nested templates, we includes all children into main template
@@ -208,6 +210,7 @@ Resources:
208210
GITHUB_REPOSITORY_URL: !Ref GitHubRepositoryUrl
209211
GITHUB_TARGET_RESOURCE: !Ref GitHubTargetResource
210212
GITHUB_IGNORE_BRANCH_REGEX: !Ref GitHubIgnoreBranchRegex
213+
BUILD_SKIPPED_BY: !Ref BuildSkippedBy
211214
StatesExecutionRole:
212215
Type: "AWS::IAM::Role"
213216
Properties:

scripts/deploy

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ aws cloudformation deploy \
2121
--parameter-overrides GitHubRepositoryUrl=$GITHUB_REPOSITORY_URL \
2222
GitHubPersonalAccessToken=$GITHUB_PERSONAL_ACCESS_TOKEN \
2323
GitHubTargetResource=$GITHUB_TARGET_RESOURCE \
24-
GitHubIgnoreBranchRegex=$GITHUB_IGNORE_BRANCH_REGEX \
24+
GitHubIgnoreBranchRegex="$GITHUB_IGNORE_BRANCH_REGEX" \
2525
CodeBuildProjectName=$CODEBUILD_PROJECT_NAME \
2626
CodeBuildRegion=$CODEBUILD_PROJECT_REGION \
27-
--region "$AWS_DEFAULT_REGION"
27+
BuildSkippedBy="$BUILD_SKIPPED_BY" \
28+
--region "$AWS_DEFAULT_REGION"

src/functions/github-webhook-handler/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ exports.handler = (event, context, callback) => {
2020

2121
const eventType = ghEventType(ghEvent),
2222
eventAction = ghEvent.action ? ghEvent.action : '',
23-
branchName = ghEvent.ref ? ghEvent.ref : ghEvent.ref.replace('refs/heads','')
24-
if (shouldIgnore(eventType, eventAction, branchName)) {
23+
branchName = ghEvent.ref ? ghEvent.ref : ghEvent.ref.replace('refs/heads',''),
24+
commitMessage = ghEvent.head_commit ? ghEvent.head_commit.message : ''
25+
if (shouldIgnore(eventType, eventAction, branchName, commitMessage)) {
2526
callback()
2627
}
2728

src/functions/github-webhook-handler/lib/should-ignore.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict'
22

33
// eventType is a string from lib/event-types.
4-
exports.shouldIgnore = (eventType, eventAction, branchName) => {
4+
exports.shouldIgnore = (eventType, eventAction = '', branchName = '', commitMessage = '') => {
55
// Temporarily disabled
66
if (process.env.DO_NOT_RUN+'' === 'true') {
77
console.log('`DO_NOT_RUN` option is enabled. We ignore the webhook event this time.')
88
return true
99
}
1010

11-
let target = process.env.GITHUB_TARGET_RESOURCE
11+
const target = process.env.GITHUB_TARGET_RESOURCE
1212
// Ignore if the type of this event is not target
1313
if (target !== eventType) {
1414
console.log(`${eventType} is not configured as a target. configured target is: ${target}`)
@@ -19,14 +19,18 @@ exports.shouldIgnore = (eventType, eventAction, branchName) => {
1919
console.log('Closed PR.')
2020
return true
2121
}
22+
// Ignore if the commit message includes specific text
23+
const ignoreKeyword = process.env.BUILD_SKIPPED_BY
24+
if (eventType === 'push' && commitMessage.indexOf(ignoreKeyword) !== -1) {
25+
console.log(`The push is ignored because the message of the head commit includes the keyword "${ignoreKeyword}".`)
26+
return true
27+
}
2228
// Ignore specific branches
2329
if (eventType === 'push' && process.env.GITHUB_IGNORE_BRANCH_REGEX.trim() !== '') {
2430
let re = new RegExp('^' + process.env.GITHUB_IGNORE_BRANCH_REGEX.trim() + '$', 'g')
2531
if (re.test(branchName)) {
2632
console.log(`Branch "${branchName}" is ignored by configuration.`)
2733
return true
28-
} else {
29-
console.log('NOT MATCHED')
3034
}
3135
}
3236
return false

src/functions/github-webhook-handler/lib/should-ignore.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ describe('should-ignore', () => {
2727
process.env.GITHUB_IGNORE_BRANCH_REGEX = 'wip.*'
2828
assert.equal(true, shouldIgnore('push', '', 'wip-branch'))
2929
})
30+
it('should ignore the push event if the message of the head commit has the keyword to be ignored', () => {
31+
process.env.GITHUB_TARGET_RESOURCE = 'push'
32+
process.env.BUILD_SKIPPED_BY = 'skip ci'
33+
assert.equal(true, shouldIgnore('push', '', 'master', '[skip ci] This commit should be ignored'))
34+
})
3035
it('should NOT ignore the push event if the branch is NOT ignored by GITHUB_IGNORE_BRANCH_REGEX', () => {
3136
process.env.GITHUB_TARGET_RESOURCE = 'push'
3237
process.env.GITHUB_IGNORE_BRANCH_REGEX = 'wip.*'

0 commit comments

Comments
 (0)