Skip to content

Commit 5a73471

Browse files
bcoeAce Nassri
authored and
Ace Nassri
committed
feat: initial release of cloud build library (#2)
1 parent 89e21b6 commit 5a73471

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

cloudbuild/list-build-triggers.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright 2019, Google, LLC.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
// [START cloudbuild_list_build_triggers]
19+
async function listBuildTriggers(
20+
projectId = 'YOUR_PROJECT_ID' // Your Google Cloud Platform project ID
21+
) {
22+
// Imports the Google Cloud client library
23+
const {CloudBuildClient} = require('@google-cloud/cloudbuild');
24+
25+
// Creates a client
26+
const cb = new CloudBuildClient();
27+
28+
// What project should we list triggers for?
29+
const request = {
30+
projectId,
31+
};
32+
33+
const [result] = await cb.listBuildTriggers(request);
34+
console.info(JSON.stringify(result.triggers, null, 2));
35+
}
36+
// [END cloudbuild_list_build_triggers]
37+
38+
const args = process.argv.slice(2);
39+
listBuildTriggers(...args).catch(console.error);

cloudbuild/package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "nodejs-cloudbuild-samples",
3+
"private": true,
4+
"license": "Apache-2.0",
5+
"files": [
6+
"*.js",
7+
"resources"
8+
],
9+
"author": "Google Inc.",
10+
"repository": "googleapis/nodejs-cloudbuild",
11+
"engines": {
12+
"node": ">=8"
13+
},
14+
"scripts": {
15+
"test": "mocha system-test --timeout=800000"
16+
},
17+
"dependencies": {
18+
"@google-cloud/cloudbuild": "^0.1.0"
19+
},
20+
"devDependencies": {
21+
"chai": "^4.2.0",
22+
"mocha": "^6.0.0"
23+
}
24+
}

cloudbuild/quickstart.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2019, Google, LLC.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
// [START cloudbuild_quickstart]
19+
async function quickstart(
20+
projectId = 'YOUR_PROJECT_ID', // Your Google Cloud Platform project ID
21+
triggerId = 'YOUR_TRIGGER_ID', // UUID for build trigger.
22+
branchName = 'BRANCH_TO_BUILD' // Branch to run build against.
23+
) {
24+
// Imports the Google Cloud client library
25+
const {CloudBuildClient} = require('@google-cloud/cloudbuild');
26+
27+
// Creates a client
28+
const cb = new CloudBuildClient();
29+
30+
// Starts a build against the branch provided.
31+
const request = {
32+
projectId,
33+
triggerId,
34+
source: {
35+
projectId: projectId,
36+
dir: './',
37+
branchName,
38+
},
39+
};
40+
await cb.runBuildTrigger(request);
41+
console.info(`triggered build for ${triggerId}`);
42+
}
43+
// [END cloudbuild_quickstart]
44+
45+
const args = process.argv.slice(2);
46+
quickstart(...args).catch(console.error);

cloudbuild/system-test/.eslintrc.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
env:
3+
mocha: true
4+
rules:
5+
node/no-extraneous-require: off

cloudbuild/system-test/samples.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright 2019, Google, LLC.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const path = require('path');
19+
const {assert} = require('chai');
20+
const cp = require('child_process');
21+
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
23+
24+
const cwd = path.join(__dirname, '..');
25+
26+
const PROJECT_ID = process.env.GCLOUD_PROJECT;
27+
// Use list-build-triggers.js to figure out the ID of the trigger
28+
// you would like to execute.
29+
const TRIGGER_ID =
30+
process.env.TRIGGER || 'c9033094-51a9-44c5-b3a0-1d882deb4464';
31+
32+
const {CloudBuildClient} = require('@google-cloud/cloudbuild');
33+
const cb = new CloudBuildClient();
34+
35+
describe('Sample Integration Tests', () => {
36+
it('should run quickstart.js', async () => {
37+
execSync(
38+
`node ./samples/quickstart.js ${PROJECT_ID} ${TRIGGER_ID} cloud-build-mvp`,
39+
{cwd}
40+
);
41+
// confirm that a build has just been kicked off.
42+
const [builds] = await cb.listBuilds({
43+
projectId: PROJECT_ID,
44+
});
45+
const createTime = builds[0].createTime.seconds * 1000;
46+
const delta = Date.now() - createTime;
47+
const maxDelta = 20000; // last build was within 20s.
48+
assert.ok(delta < maxDelta, `delta ${delta} was > ${maxDelta}`);
49+
});
50+
51+
it('should run list-build-triggers.js', async () => {
52+
const stdout = execSync(
53+
`node ./samples/list-build-triggers.js ${PROJECT_ID} ${TRIGGER_ID} cloud-build-mvp`,
54+
{cwd}
55+
);
56+
assert.include(stdout, 'Push-to-any-branch');
57+
});
58+
});

0 commit comments

Comments
 (0)