Skip to content

Commit 456e0ee

Browse files
committed
Invoke CodeBuild from webhook handler Lambda function
1 parent 5227e97 commit 456e0ee

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

sam.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@ Resources:
156156
- "logs:PutLogEvents"
157157
Resource: "arn:aws:logs:*:*:*"
158158
- Effect: Allow
159-
Action: "states:StartExecution"
160-
Resource: !Ref BuildStateMachine
159+
Action:
160+
- "codebuild:StartBuild"
161+
Resource: !Sub "arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:project/${CodeBuildProjectName}"
161162
GitHubWebhookHandler:
162163
Type: "AWS::Serverless::Function"
163164
Properties:
@@ -176,6 +177,7 @@ Resources:
176177
Variables:
177178
DO_NOT_RUN: false
178179
CODEBUILD_PROJECT_REGION: !Ref CodeBuildRegion
180+
CODEBUILD_PROJECT_NAME: !Ref CodeBuildProjectName
179181
GITHUB_TOKEN: !Ref GitHubPersonalAccessToken
180182
GITHUB_REPOSITORY_URL: !Ref GitHubRepositoryUrl
181183
GITHUB_TARGET_RESOURCE: !Ref GitHubTargetResource
Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const AWS = require('aws-sdk'),
4-
stepfunctions = new AWS.StepFunctions()
4+
codebuild = new AWS.CodeBuild()
55
const GitHubApi = require('github'),
66
github = new GitHubApi({version: '3.0.0'})
77
const ghUrl = require('parse-github-url'),
@@ -26,39 +26,45 @@ exports.handler = (event, context, callback) => {
2626
callback()
2727
}
2828

29-
// Create GitHub pending status
29+
// Execute CodeBuild Project
3030
const sha = ghEvent.pull_request ? ghEvent.pull_request.head.sha : ghEvent.head_commit.id
31-
const p = new Promise((resolve) => {
32-
github.repos.createStatus({
33-
owner: repo.owner,
34-
repo: repo.name,
35-
sha: sha,
36-
state: 'pending',
37-
target_url: `https://${region}.console.aws.amazon.com/codebuild/home?region=${region}#/builds`,
38-
context: 'codebuild',
39-
description: 'AWS CodeBuild is preparing your tests...'
40-
}).then((data) => {
41-
console.log(data)
42-
resolve('status created')
43-
}).catch((err) => {
44-
console.log(err)
45-
resolve('status creation failed')
46-
})
47-
})
48-
49-
// Execute Step Functions
50-
p.then(() => {
51-
const params = {
52-
stateMachineArn: process.env.STEP_FUNCTIONS_ARN,
53-
input: JSON.stringify(ghEvent)
54-
}
55-
stepfunctions.startExecution(params, function(err, data) {
56-
if (err) {
57-
console.log(err, err.stack)
31+
const params = {
32+
projectName: process.env.CODEBUILD_PROJECT_NAME,
33+
sourceVersion: sha
34+
}
35+
codebuild.startBuild(params, (err, data) => {
36+
// Create GitHub status
37+
if (err) {
38+
// If CodeBuild couldn't start a build, save GitHub status as 'error'
39+
console.log(err, err.stack)
40+
github.repos.createStatus({
41+
owner: repo.owner,
42+
repo: repo.name,
43+
sha: sha,
44+
state: 'error',
45+
target_url: `https://${region}.console.aws.amazon.com/codebuild/home?region=${region}#/builds`,
46+
context: 'codebuild',
47+
description: 'AWS CodeBuild has failed to start your tests'
48+
}).then(() => {
5849
callback(err)
59-
} else {
50+
}).catch((err) => {
51+
callback(err)
52+
})
53+
} else {
54+
github.repos.createStatus({
55+
owner: repo.owner,
56+
repo: repo.name,
57+
sha: sha,
58+
state: 'pending',
59+
target_url: `https://${region}.console.aws.amazon.com/codebuild/home?region=${region}#/builds/${data.build.id}/view/new`,
60+
context: 'codebuild',
61+
description: 'AWS CodeBuild is running your tests'
62+
}).then((data) => {
6063
callback(null, data)
61-
}
62-
})
64+
}).catch((err) => {
65+
console.log(err, err.stack)
66+
callback(err)
67+
})
68+
}
6369
})
6470
}

0 commit comments

Comments
 (0)