MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

batch/create/create_using_secret_manager.js:19–104  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

17'use strict';
18
19async function main() {
20 // [START batch_create_using_secret_manager]
21 // Imports the Batch library
22 const batchLib = require('@google-cloud/batch');
23 const batch = batchLib.protos.google.cloud.batch.v1;
24
25 // Instantiates a client
26 const batchClient = new batchLib.v1.BatchServiceClient();
27
28 /**
29 * TODO(developer): Update these variables before running the sample.
30 */
31 // Project ID or project number of the Google Cloud project you want to use.
32 const projectId = await batchClient.getProjectId();
33 // Name of the region you want to use to run the job. Regions that are
34 // available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
35 const region = 'europe-central2';
36 // The name of the job that will be created.
37 // It needs to be unique for each project and region pair.
38 const jobName = 'batch-job-secret-manager';
39 // The name of the secret variable.
40 // This variable name is specified in this job's runnables
41 // and is accessible to all of the runnables that are in the same environment.
42 const secretVariableName = 'secretVariableName';
43 // The name of an existing Secret Manager secret.
44 const secretName = 'secretName';
45 // The version of the specified secret that contains the data you want to pass to the job.
46 // This can be the version number or latest.
47 const version = 'version';
48
49 // Define what will be done as part of the job.
50 const runnable = new batch.Runnable({
51 script: new batch.Runnable.Script({
52 commands: ['-c', `echo This is the secret: ${secretVariableName}`],
53 }),
54 });
55
56 // Construct the resource path to the secret's version.
57 const secretValue = `projects/${projectId}/secrets/${secretName}/versions/${version}`;
58
59 // Set the secret as an environment variable.
60 const environment = new batch.Environment();
61 environment.secretVariables[secretVariableName] = secretValue;
62
63 const task = new batch.TaskSpec({
64 runnables: [runnable],
65 environment,
66 maxRetryCount: 2,
67 maxRunDuration: {seconds: 3600},
68 });
69
70 // Tasks are grouped inside a job using TaskGroups.
71 const group = new batch.TaskGroup({
72 taskCount: 3,
73 taskSpec: task,
74 });
75
76 const job = new batch.Job({

Callers 1

Calls 2

getProjectIdMethod · 0.80

Tested by

no test coverage detected