Build and create a Node.js job in Cloud Run

Learn how to create a simple Cloud Run job, then deploy from source, which automatically packages your code into a container image, uploads the container image to Artifact Registry, and then deploys to Cloud Run. You can use other languages in addition to the ones shown.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Install the Google Cloud CLI.
  5. To initialize the gcloud CLI, run the following command:

    gcloud init
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Install the Google Cloud CLI.
  9. To initialize the gcloud CLI, run the following command:

    gcloud init
  10. Enable the Cloud Run Admin API and the Cloud Build API:

    gcloud services enable run.googleapis.com \
        cloudbuild.googleapis.com

    After the Cloud Run Admin API is enabled, the Compute Engine default service account is automatically created.

  11. For Cloud Build to be able to build your sources, grant the Cloud Build Service Account role to the Compute Engine default service account by running the following:

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \
        --role=roles/cloudbuild.builds.builder

    Replace PROJECT_NUMBER with your Google Cloud project number, and PROJECT_ID with your Google Cloud project ID. For detailed instructions on how to find your project ID, and project number, see Creating and managing projects.

    Granting the Cloud Build Service Account role to the Compute Engine default service account takes a couple of minutes to propagate.

Writing the sample job

To write a job in Node.js:

  1. Create a new directory named jobs and change directory into it:

    mkdir jobs
    cd jobs
    
  2. Create a package.json file with the following contents:

    {
        "name": "jobs",
        "version": "1.0.0",
        "description": "Node.js sample for Cloud Run jobs",
        "main": "index.js",
        "scripts": {
            "start": "node index.js"
        },
        "engines": {
            "node": ">=16.0.0"
        },
        "author": "Google LLC",
        "license": "Apache-2.0"
    }
    
  3. In the same directory, create a index.js file for the actual job code. Copy the following sample lines into it:

    // Retrieve Job-defined env vars
    const {CLOUD_RUN_TASK_INDEX = 0, CLOUD_RUN_TASK_ATTEMPT = 0} = process.env;
    // Retrieve User-defined env vars
    const {SLEEP_MS, FAIL_RATE} = process.env;
    
    // Define main script
    const main = async () => {
      console.log(
        `Starting Task #${CLOUD_RUN_TASK_INDEX}, Attempt #${CLOUD_RUN_TASK_ATTEMPT}...`
      );
      // Simulate work
      if (SLEEP_MS) {
        await sleep(SLEEP_MS);
      }
      // Simulate errors
      if (FAIL_RATE) {
        try {
          randomFailure(FAIL_RATE);
        } catch (err) {
          err.message = `Task #${CLOUD_RUN_TASK_INDEX}, Attempt #${CLOUD_RUN_TASK_ATTEMPT} failed.\n\n${err.message}`;
          throw err;
        }
      }
      console.log(`Completed Task #${CLOUD_RUN_TASK_INDEX}.`);
    };
    
    // Wait for a specific amount of time
    const sleep = ms => {
      return new Promise(resolve => setTimeout(resolve, ms));
    };
    
    // Throw an error based on fail rate
    const randomFailure = rate => {
      rate = parseFloat(rate);
      if (!rate || rate < 0 || rate > 1) {
        console.warn(
          `Invalid FAIL_RATE env var value: ${rate}. Must be a float between 0 and 1 inclusive.`
        );
        return;
      }
    
      const randomFailure = Math.random();
      if (randomFailure < rate) {
        throw new Error('Task failed.');
      }
    };
    
    // Start script
    main().catch(err => {
      console.error(err);
      process.exit(1); // Retry Job Task by exiting the process
    });

    Cloud Run jobs allows users to specify the number of tasks the job is to execute. This sample code shows how to use the built-in CLOUD_RUN_TASK_INDEX environment variable. Each task represents one running copy of the container. Note that tasks are usually executed in parallel. Using multiple tasks is useful if each task can independently process a subset of your data.

    Each task is aware of its index, stored in the CLOUD_RUN_TASK_INDEX environment variable. The built-in CLOUD_RUN_TASK_COUNT environment variable contains the number of tasks supplied at job execution time via the --tasks parameter.

    The code shown also shows how to retry tasks, using the built-in CLOUD_RUN_TASK_ATTEMPT environment variable, which contains the number of times this task has been retried, starting at 0 for the first attempt and incrementing by 1 for every successive retry, up to --max-retries.

    The code also lets you generate failures as a way to test retries and to generate error logs so you can see what those look like.

  4. Create a Procfile with the following contents:

    # Define the application's entrypoint to override default, `npm start`
    # https://github.com/GoogleCloudPlatform/buildpacks/issues/160
    web: node index.js
    

Your code is complete and ready to be packaged in a container.

Build jobs container, send it to Artifact Registry and deploy to Cloud Run

Important: This quickstart assumes that you have owner or editor roles in the project you are using for the quickstart. Otherwise, refer to the Cloud Run Source Developer role for the required permissions for deploying a Cloud Run resource from source.

This quickstart uses deploy from source, which builds the container, uploads it to Artifact Registry, and deploys the job to Cloud Run:

gcloud run jobs deploy job-quickstart \
    --source . \
    --tasks 50 \
    --set-env-vars SLEEP_MS=10000 \
    --set-env-vars FAIL_RATE=0.1 \
    --max-retries 5 \
    --region REGION \
    --project=PROJECT_ID

where PROJECT_ID is your project ID and REGION is your region, for example, us-central1. Note that you can change the various parameters to whatever values you want to use for your testing purposes. SLEEP_MS simulates work and FAIL_RATE causes X% of tasks to fail so you can experiment with parallelism and retrying failing tasks.

Execute a job in Cloud Run

To execute the job you just created:

gcloud run jobs execute job-quickstart --region REGION

Replace REGION with the region you used when you created and deployed the job, for example us-central1.

What's next

For more information on building a container from code source and pushing to a repository, see: