Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit 024cced

Browse files
authored
Merge pull request #135 from iansu/codebuild
Add support for AWS CodeBuild
2 parents f628c33 + 24a0a76 commit 024cced

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

lib/detect.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var services = {
1515
gitlab: require('./services/gitlab'),
1616
heroku: require('./services/heroku'),
1717
teamcity: require('./services/teamcity'),
18+
codebuild: require('./services/codebuild'),
1819
}
1920

2021
var detectProvider = function() {

lib/services/codebuild.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module.exports = {
2+
detect: function() {
3+
return !!process.env.CODEBUILD_CI
4+
},
5+
6+
configuration: function() {
7+
console.log(' AWS CodeBuild Detected')
8+
return {
9+
service: 'codebuild',
10+
build: process.env.CODEBUILD_BUILD_ID,
11+
job: process.env.CODEBUILD_BUILD_ID,
12+
commit: process.env.CODEBUILD_RESOLVED_SOURCE_VERSION,
13+
branch: detectBranchName(),
14+
pr: detectPRNumber(),
15+
slug: detectRepoSlug(),
16+
}
17+
function detectBranchName() {
18+
if (process.env.CODEBUILD_WEBHOOK_HEAD_REF) {
19+
return process.env.CODEBUILD_WEBHOOK_HEAD_REF.replace(
20+
/^refs\/heads\//,
21+
''
22+
)
23+
}
24+
throw new Error('Cannot detect branch name.')
25+
}
26+
function detectPRNumber() {
27+
if (process.env.CODEBUILD_SOURCE_VERSION) {
28+
return process.env.CODEBUILD_SOURCE_VERSION.replace(/^pr\//, '')
29+
}
30+
throw new Error('Cannot detect PR number.')
31+
}
32+
function detectRepoSlug() {
33+
if (process.env.CODEBUILD_SOURCE_REPO_URL) {
34+
return process.env.CODEBUILD_SOURCE_REPO_URL.replace(
35+
/^.*github.com\//,
36+
''
37+
).replace(/\.git$/, '')
38+
}
39+
throw new Error('Cannot detect repository slug.')
40+
}
41+
},
42+
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/services/codebuild.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var codebuild = require('../../lib/services/codebuild')
2+
3+
describe('AWS CodeBuild Provider', function() {
4+
it('can detect codebuild', function() {
5+
process.env.CODEBUILD_CI = 'true'
6+
expect(codebuild.detect()).toBe(true)
7+
})
8+
9+
it('can get codebuild env info', function() {
10+
process.env.CODEBUILD_CI = 'true'
11+
process.env.CODEBUILD_BUILD_ID =
12+
'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969'
13+
process.env.CODEBUILD_RESOLVED_SOURCE_VERSION =
14+
'39ec2418eca4c539d765574a1c68f3bd77e8c549'
15+
process.env.CODEBUILD_WEBHOOK_HEAD_REF = 'refs/heads/master'
16+
process.env.CODEBUILD_SOURCE_VERSION = 'pr/1'
17+
process.env.CODEBUILD_SOURCE_REPO_URL =
18+
'https://github.com/my-org/my-project.git'
19+
expect(codebuild.configuration()).toEqual({
20+
service: 'codebuild',
21+
build: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
22+
job: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
23+
commit: '39ec2418eca4c539d765574a1c68f3bd77e8c549',
24+
branch: 'master',
25+
pr: '1',
26+
slug: 'my-org/my-project',
27+
})
28+
})
29+
30+
it('throws if branch name cannot be detected', function() {
31+
delete process.env.CODEBUILD_WEBHOOK_HEAD_REF
32+
expect(function() {
33+
codebuild.configuration()
34+
}).toThrow()
35+
})
36+
37+
it('throws if pr number cannot be detected', function() {
38+
process.env.CODEBUILD_WEBHOOK_HEAD_REF = 'refs/heads/master'
39+
delete process.env.CODEBUILD_SOURCE_VERSION
40+
expect(function() {
41+
codebuild.configuration()
42+
}).toThrow()
43+
})
44+
45+
it('throws if slug cannot be detected', function() {
46+
process.env.CODEBUILD_RESOLVED_SOURCE_VERSION =
47+
'39ec2418eca4c539d765574a1c68f3bd77e8c549'
48+
delete process.env.CODEBUILD_SOURCE_REPO_URL
49+
expect(function() {
50+
codebuild.configuration()
51+
}).toThrow()
52+
})
53+
})

0 commit comments

Comments
 (0)