diff --git a/.github/workflows/cloudbuild.yaml b/.github/workflows/cloudbuild.yaml new file mode 100644 index 0000000000..96c434b448 --- /dev/null +++ b/.github/workflows/cloudbuild.yaml @@ -0,0 +1,67 @@ +name: cloudbuild +on: + push: + branches: + - main + paths: + - 'cloudbuild/**' + pull_request: + paths: + - 'cloudbuild/**' + pull_request_target: + types: [labeled] + schedule: + - cron: '0 0 * * 0' +jobs: + test: + if: ${{ github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' }} + runs-on: ubuntu-latest + timeout-minutes: 60 + permissions: + contents: 'write' + pull-requests: 'write' + id-token: 'write' + steps: + - uses: actions/checkout@v3.1.0 + with: + ref: ${{github.event.pull_request.head.ref}} + repository: ${{github.event.pull_request.head.repo.full_name}} + - uses: 'google-github-actions/auth@v0.8.3' + with: + workload_identity_provider: 'projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider' + service_account: 'kokoro-system-test@long-door-651.iam.gserviceaccount.com' + create_credentials_file: 'true' + access_token_lifetime: 600s + - uses: actions/setup-node@v3.5.1 + with: + node-version: 16 + - run: npm install + working-directory: cloudbuild + - run: npm test + working-directory: cloudbuild + env: + MOCHA_REPORTER_SUITENAME: cloudbuild + MOCHA_REPORTER_OUTPUT: cloudbuild_sponge_log.xml + MOCHA_REPORTER: xunit + - if: ${{ github.event.action == 'labeled' && github.event.label.name == 'actions:force-run' }} + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + try { + await github.rest.issues.removeLabel({ + name: 'actions:force-run', + owner: 'GoogleCloudPlatform', + repo: 'nodejs-docs-samples', + issue_number: context.payload.pull_request.number + }); + } catch (e) { + if (!e.message.includes('Label does not exist')) { + throw e; + } + } + - if: ${{ github.event_name == 'schedule'}} + run: | + curl https://github.com/googleapis/repo-automation-bots/releases/download/flakybot-1.1.0/flakybot -o flakybot -s -L + chmod +x ./flakybot + ./flakybot --repo GoogleCloudPlatform/nodejs-docs-samples --commit_hash ${{github.sha}} --build_url https://github.com/${{github.repository}}/actions/runs/${{github.run_id}} diff --git a/.github/workflows/workflows.json b/.github/workflows/workflows.json index c1d81aa6e3..852a6be0d3 100644 --- a/.github/workflows/workflows.json +++ b/.github/workflows/workflows.json @@ -17,6 +17,7 @@ "appengine/typescript", "appengine/websockets", "appengine/twilio", + "cloudbuild", "cloud-language", "cloud-tasks/tutorial-gcf/app", "cloud-tasks/tutorial-gcf/function", diff --git a/CODEOWNERS b/CODEOWNERS index 23746c9490..85d85eba5b 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -16,6 +16,7 @@ run @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/nodejs-samples-reviewers cloud-tasks @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/nodejs-samples-reviewers workflows @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/nodejs-samples-reviewers eventarc @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/nodejs-samples-reviewers +cloudbuild @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/nodejs-samples-reviewers # Infrastructure orgpolicy @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/nodejs-samples-reviewers diff --git a/cloudbuild/listBuildTriggers.js b/cloudbuild/listBuildTriggers.js new file mode 100644 index 0000000000..0f7116521a --- /dev/null +++ b/cloudbuild/listBuildTriggers.js @@ -0,0 +1,43 @@ +// 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'; + +// sample-metadata: +// title: List Triggers. +// description: List available build triggers. +// usage: node list-build-triggers.js + +// [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, null, 2)); +} +// [END cloudbuild_list_build_triggers] + +const args = process.argv.slice(2); +listBuildTriggers(...args).catch(console.error); diff --git a/cloudbuild/package.json b/cloudbuild/package.json new file mode 100644 index 0000000000..eff1440160 --- /dev/null +++ b/cloudbuild/package.json @@ -0,0 +1,25 @@ +{ + "name": "nodejs-cloudbuild-samples", + "private": true, + "license": "Apache-2.0", + "files": [ + "*.js", + "resources" + ], + "author": "Google Inc.", + "repository": "googleapis/nodejs-cloudbuild", + "engines": { + "node": ">=12.0.0" + }, + "scripts": { + "test": "c8 mocha system-test --timeout=800000" + }, + "dependencies": { + "@google-cloud/cloudbuild": "^3.1.2" + }, + "devDependencies": { + "c8": "^7.0.0", + "chai": "^4.2.0", + "mocha": "^8.0.0" + } +} diff --git a/cloudbuild/quickstart.js b/cloudbuild/quickstart.js new file mode 100644 index 0000000000..b1a3fb2a23 --- /dev/null +++ b/cloudbuild/quickstart.js @@ -0,0 +1,66 @@ +// 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 +// +// https://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(); + + // Note: for Private Pools, you'll have to specify an API endpoint value + // For example: + // const cb = new CloudBuildClient({ apiEndpoint: '-cloudbuild.googleapis.com' }); + + // Starts a build against the branch provided. + const [resp] = await cb.runBuildTrigger({ + projectId, + triggerId, + source: { + projectId, + dir: './', + branchName, + }, + }); + console.info(`triggered build for ${triggerId}`); + const [build] = await resp.promise(); + + const STATUS_LOOKUP = [ + 'UNKNOWN', + 'Queued', + 'Working', + 'Success', + 'Failure', + 'Error', + 'Timeout', + 'Cancelled', + ]; + for (const step of build.steps) { + console.info( + `step:\n\tname: ${step.name}\n\tstatus: ${STATUS_LOOKUP[build.status]}` + ); + } +} +// [END cloudbuild_quickstart] + +const args = process.argv.slice(2); +quickstart(...args).catch(console.error); diff --git a/cloudbuild/system-test/.eslintrc.yml b/cloudbuild/system-test/.eslintrc.yml new file mode 100644 index 0000000000..b1e44f8a75 --- /dev/null +++ b/cloudbuild/system-test/.eslintrc.yml @@ -0,0 +1,3 @@ +--- +rules: + node/no-extraneous-require: off diff --git a/cloudbuild/system-test/samples.js b/cloudbuild/system-test/samples.js new file mode 100644 index 0000000000..afdb0f8a81 --- /dev/null +++ b/cloudbuild/system-test/samples.js @@ -0,0 +1,49 @@ +// 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 {describe, it} = require('mocha'); +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'; + +describe('Sample Integration Tests', () => { + it('should run quickstart.js', async () => { + const stdout = execSync( + `node ./quickstart.js ${PROJECT_ID} ${TRIGGER_ID} cloud-build-mvp`, + {cwd} + ); + // build should have exited with success status. + assert.include(stdout, 'status: Success'); + }); + + it('should run list-build-triggers.js', async () => { + const stdout = execSync( + `node ./listBuildTriggers.js ${PROJECT_ID} ${TRIGGER_ID} cloud-build-mvp`, + {cwd} + ); + assert.include(stdout, 'Push-to-any-branch'); + }); +});