Skip to content

Commit

Permalink
feat: initial release of cloud build library (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe authored Oct 17, 2019
0 parents commit 2c0c0a4
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 0 deletions.
5 changes: 5 additions & 0 deletions generated,README.md,.eslintrc.yml/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
rules:
no-console: off
node/no-missing-require: off
node/no-extraneous-require: off
44 changes: 44 additions & 0 deletions generated,README.md,.eslintrc.yml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[//]: # "This README.md file is auto-generated, all changes to this file will be lost."
[//]: # "To regenerate it, use `python -m synthtool`."
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>

# [Cloud Build: Node.js Samples](https://github.com/googleapis/nodejs-cloudbuild)

[![Open in Cloud Shell][shell_img]][shell_link]



## Table of Contents

* [Before you begin](#before-you-begin)
* [Samples](#samples)
* [Quickstart](#quickstart)

## Before you begin

Before running the samples, make sure you've followed the steps outlined in
[Using the client library](https://github.com/googleapis/nodejs-cloudbuild#using-the-client-library).

## Samples



### Quickstart

View the [source code](https://github.com/googleapis/nodejs-cloudbuild/blob/master/samples/quickstart.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-cloudbuild&page=editor&open_in_editor=samples/quickstart.js,samples/README.md)

__Usage:__


`node quickstart.js`






[shell_img]: https://gstatic.com/cloudssh/images/open-btn.png
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-cloudbuild&page=editor&open_in_editor=samples/README.md
[product-docs]: https://cloud.google.com/cloud-build/docs/
39 changes: 39 additions & 0 deletions generated,README.md,.eslintrc.yml/list-build-triggers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2019, Google, LLC.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// [START cloudbuild_list_build_triggers]
async function listBuildTriggers(
projectId = 'YOUR_PROJECT_ID' // Your Google Cloud Platform project ID
) {
// Imports the Google Cloud client library
const {CloudBuildClient} = require('@google-cloud/cloudbuild');

// Creates a client
const cb = new CloudBuildClient();

// What project should we list triggers for?
const request = {
projectId,
};

const [result] = await cb.listBuildTriggers(request);
console.info(JSON.stringify(result.triggers, null, 2));
}
// [END cloudbuild_list_build_triggers]

const args = process.argv.slice(2);
listBuildTriggers(...args).catch(console.error);
24 changes: 24 additions & 0 deletions generated,README.md,.eslintrc.yml/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "nodejs-cloudbuild-samples",
"private": true,
"license": "Apache-2.0",
"files": [
"*.js",
"resources"
],
"author": "Google Inc.",
"repository": "googleapis/nodejs-cloudbuild",
"engines": {
"node": ">=8"
},
"scripts": {
"test": "mocha system-test --timeout=800000"
},
"dependencies": {
"@google-cloud/cloudbuild": "^0.1.0"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^6.0.0"
}
}
46 changes: 46 additions & 0 deletions generated,README.md,.eslintrc.yml/quickstart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright 2019, Google, LLC.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// [START cloudbuild_quickstart]
async function quickstart(
projectId = 'YOUR_PROJECT_ID', // Your Google Cloud Platform project ID
triggerId = 'YOUR_TRIGGER_ID', // UUID for build trigger.
branchName = 'BRANCH_TO_BUILD' // Branch to run build against.
) {
// Imports the Google Cloud client library
const {CloudBuildClient} = require('@google-cloud/cloudbuild');

// Creates a client
const cb = new CloudBuildClient();

// Starts a build against the branch provided.
const request = {
projectId,
triggerId,
source: {
projectId: projectId,
dir: './',
branchName,
},
};
await cb.runBuildTrigger(request);
console.info(`triggered build for ${triggerId}`);
}
// [END cloudbuild_quickstart]

const args = process.argv.slice(2);
quickstart(...args).catch(console.error);
5 changes: 5 additions & 0 deletions generated,README.md,.eslintrc.yml/system-test/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
env:
mocha: true
rules:
node/no-extraneous-require: off
58 changes: 58 additions & 0 deletions generated,README.md,.eslintrc.yml/system-test/samples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright 2019, Google, LLC.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const path = require('path');
const {assert} = require('chai');
const cp = require('child_process');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const cwd = path.join(__dirname, '..');

const PROJECT_ID = process.env.GCLOUD_PROJECT;
// Use list-build-triggers.js to figure out the ID of the trigger
// you would like to execute.
const TRIGGER_ID =
process.env.TRIGGER || 'c9033094-51a9-44c5-b3a0-1d882deb4464';

const {CloudBuildClient} = require('@google-cloud/cloudbuild');
const cb = new CloudBuildClient();

describe('Sample Integration Tests', () => {
it('should run quickstart.js', async () => {
execSync(
`node ./samples/quickstart.js ${PROJECT_ID} ${TRIGGER_ID} cloud-build-mvp`,
{cwd}
);
// confirm that a build has just been kicked off.
const [builds] = await cb.listBuilds({
projectId: PROJECT_ID,
});
const createTime = builds[0].createTime.seconds * 1000;
const delta = Date.now() - createTime;
const maxDelta = 20000; // last build was within 20s.
assert.ok(delta < maxDelta, `delta ${delta} was > ${maxDelta}`);
});

it('should run list-build-triggers.js', async () => {
const stdout = execSync(
`node ./samples/list-build-triggers.js ${PROJECT_ID} ${TRIGGER_ID} cloud-build-mvp`,
{cwd}
);
assert.include(stdout, 'Push-to-any-branch');
});
});

0 comments on commit 2c0c0a4

Please sign in to comment.