Skip to content

Latest commit

 

History

History

run

Google Cloud Platform logo

Google Cloud Run Node.js Samples

Cloud Run runs stateless containers on a fully managed environment.

Samples

Sample Description Deploy
Hello World Quickstart Run on Google Cloud
System Packages Use system-installed binaries in your service. Run on Google Cloud
Pub/Sub Processing messages from a Pub/Sub push subscription Run on Google Cloud
Image Processing Cloud Storage & Pub/Sub-driven image analysis & transformation Run on Google Cloud
Manual Logging Structured logging without client library Run on Google Cloud
Hello Broken Something is wrong, how do you fix it? Run on Google Cloud
Identity Platform Authenticate users and connect to a Cloud SQL postgreSQL databases Run on Google Cloud
Markdown Preview Create a secure two-service application running on Cloud Run -
Cloud SQL (MySQL) Use MySQL with Cloud Run -
Cloud SQL (Postgres) Use Postgres with Cloud Run -
Service to Service Requests Create requests to authenticated-only services -
Eventarc: Pub/Sub Event-driven service with Events for Cloud Run for Pub/Sub -
Eventarc: Cloud Storage Event-driven service with Events for Cloud Run for GCS -

For more Cloud Run samples beyond Node.js, see the main list in the Cloud Run Samples repository.

Setup

  1. Set up for Cloud Run development

  2. Clone this repository:

    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git

    Note: Some samples in the list above are hosted in other repositories. They are noted with the symbol "➥".

How to run a sample locally

  1. Install docker locally

  2. Build the sample container:

    export SAMPLE=<SAMPLE_NAME>
    cd $SAMPLE
    docker build --tag $SAMPLE .
  3. Run containers locally

    With the built container:

    PORT=8080 && docker run --rm -p 8080:${PORT} -e PORT=${PORT} $SAMPLE

    Overriding the built container with local code:

    PORT=8080 && docker run --rm \
        -p 8080:${PORT} -e PORT=${PORT} \
        -v $PWD:/usr/src/app $SAMPLE

    Injecting your service account key:

    export SA_KEY_NAME=my-key-name-123
    PORT=8080 && docker run --rm \
        -p 8080:${PORT} -e PORT=${PORT} \
        -e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/${SA_KEY_NAME}.json \
        -v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/${SA_KEY_NAME}.json:ro \
        -v $PWD:/usr/src/app $SAMPLE

    Opening a shell in the container (e.g., updating the package-lock.json):

    1. Build the container.

    2. Run the container with a shell:

    PORT=8080 && docker run --rm \
        --interactive --tty \
        -p 8080:${PORT} -e PORT=${PORT} \
        -v $PWD:/usr/src/app $SAMPLE \
        /bin/bash
    1. Re-generate the package-lock.json:
    rm package-lock.json
    npm install

    Because we're using a read/write volume mount, the revised file will be written to the host's local filesystem. Once you exit the container you can add the file to version control.

    1. Exit the container: Ctrl-D

Running the Tests

Run unit tests:

npm test

Run system tests:

export GOOGLE_CLOUD_PROJECT=<YOUR_PROJECT_ID>
npm run system-test

Note: See sample READMEs for specific environment variables needed for testing.

Deploying

gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
gcloud run deploy ${SAMPLE} \
  # Needed for Manual Logging sample.
  --set-env-vars GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT} \
  --image gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}

See Building containers and Deploying container images for more information.