Skip to content

Latest commit

 

History

History
 
 

README.md

Google Cloud Platform logo

Google Cloud Run Node.js Samples

Cloud Run runs stateless containers on a fully managed environment or in your own GKE cluster.

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
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 -
Hello Broken Something is wrong, how do you fix it? Run on Google Cloud
Pub/Sub - Events Event-driven service with Events for Cloud Run for Pub/Sub -
Pub/Sub - Anthos Events Event-driven service with Events for Cloud Run on Anthos for Pub/Sub -
Cloud Storage - Events Event-driven service with Events for Cloud Run for GCS -
Cloud Storage - Anthos Events Event-driven service with Events for Cloud Run on Anthos 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
    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 & integration tests.
npm test

# Run system tests.
SAMPLE=[SAMPLE_TO_TEST]
CONTAINER_IMAGE=gcr.io/$GOOGLE_CLOUD_PROJECT/${SAMPLE}:manual
gcloud builds submit --tag $CONTAINER_IMAGE
SERVICE_NAME=${SAMPLE} npm run system-test
gcloud container images delete gcr.io/$GOOGLE_CLOUD_PROJECT/${SAMPLE}:manual

Deploying

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

See Building containers and Deploying container images for more information.