Edit on GitHub
Report issue
Page history

Author(s): @{[ username ]}  Published: {[ TutorialCtrl.tutorial.date_published | date:'mediumDate' ]}

title: Cloud Functions GitHub Auto-deployer description: Learn how to automatically deploy HTTP Cloud Functions stored on GitHub when a commit is pushed. author: ancavulpe tags: Cloud Functions, Node.js date_published: 2017-07-26


Introduction

This simple tutorial demonstrates using an HTTP Cloud Function to automatically re-deploy another HTTP Cloud Function stored on GitHub when a commit is pushed.

Objectives

  • Prepare an HTTP Cloud Function in your GitHub repository.
  • Deploy an auto-deployer HTTP Cloud Function that watches changes in that repository.
  • Make a new commit to your GitHub repository and watch your HTTP Cloud Function be automatically re-deployed.

Costs

This tutorial uses billable components of [Cloud Platform][platform], including:

Use the Pricing Calculator to generate a cost estimate based on your projected usage.

Prerequisites

  1. Create a project in the Google Cloud Platform Console.
  2. Enable billing for your project.
  3. Install the Google Cloud SDK.

Deploying the auto-deployer function

  1. Fork an existing github repo with hello world function.
  2. Clone the repository which contains the auto-deployer function.

    git clone https://github.com/GoogleCloudPlatform/community
    
  3. Change into the cloned directory:

    cd community/tutorials/cloud-functions-github-auto-deployer/auto-deployer
    
  4. Update the config.js file with your own values:

    {
      "secretToken" : "[YOUR_SECRET_TOKEN]",
      "stageBucket" : "[YOUR_STAGING_BUCKET_NAME]",
      "location" : "[YOUR_REGION]",
      "deployments": [
        {
          "repository":"[YOUR_REPOSITORY_1]",
          "path":"[YOUR_FUNCTION_PATH_1]",
          "functionName":"[YOUR_FUNCTION_NAME_1]"
        },
        {
          "repository":"[YOUR_REPOSITORY_2]",
          "path":"[YOUR_FUNCTION_PATH_N]",
          "functionName":"[YOUR_FUNCTION_NAME_N]"
        }
      ]
    }
    

    where

    • [YOUR_SECRET_TOKEN] is the secret key you will configure in your GitHub webhook in order to validate the request.
    • [YOUR_STAGING_BUCKET_NAME] is the name of your staging Cloud Storage bucket.
    • [YOUR_REGION] is the region where your function is deployed.
    • [YOUR_REPOSITORY] is the owner/repo of the repository where the function to be deployed resides, e.g. GoogleCloudPlatform/nodejs-docs-samples.
    • [YOUR_FUNCTION_PATH] is the path of the HTTP Cloud Function in your GitHub repository, e.g. functions/helloworld
    • [YOUR_FUNCTION_NAME] is the name of the function that you want to be automatically deployed, e.g. helloGET.
  5. Run the following command to deploy the auto-deployer function:

    gcloud beta functions deploy githubAutoDeployer --stage-bucket [YOUR_STAGING_BUCKET_NAME] --trigger-http
    

    where [YOUR_STAGING_BUCKET_NAME] is the name of your staging Cloud Storage bucket.

Making a function in your repository auto-deployable

  1. To deploy an HTTP Cloud Function whenever a commit is pushed, create a webhook in your repository:

    Note: If you don't have an HTTP Cloud Function in a GitHub repository of your own, you can fork https://github.com/GoogleCloudPlatform/community and setup auto-deployment for the HTTP function found in the tutorials/cloud-functions-github-auto-deployer/sample-function directory.
  2. Go to your GitHub repository.

  3. Clink on the Settings tab.
  4. Click on Webhooks option in the left panel.
  5. Click on Add webhook button and confirm by reintroducing your credentials.
  6. Enter the HTTP trigger URL of the auto-deployer function you just deployed into the Payload URL field, i.e. https://[YOUR_REGION]-[YOUR_PROJECT_ID].cloudfunctions.net/gitHubAutoDeployer, where [YOUR_REGION] is the region where your function is deployed and [YOUR_PROJECT_ID] is your Google Cloud project ID.
  7. For Content type select application/json.
  8. In the Secret textbox fill in the secret token you used in the config.json file (see [YOUR_SECRET_TOKEN] above) for validating the request.
  9. Make sure that Just the push event is selected for triggering the webhook.
  10. Click an Add webhook button.

  11. Update your repository and watch your function being re-deployed by the auto-deployer function:

  12. Modify the function in your repository.

  13. Push commit to your repository.
  14. Observe that the auto-deployer re-deploys your function.
See more by @{[ username ]} and more tagged {[ tag ]}{[ $last ? '' : ', ' ]}

Submit a Tutorial

Share step-by-step guides

SUBMIT A TUTORIAL

Request a Tutorial

Ask for community help

SUBMIT A REQUEST

GCP Tutorials

Tutorials published by GCP

VIEW TUTORIALS

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.