Skip to main content

Google Cloud Functions serverless deployments

This tutorial will get you started with Harness Continuous Delivery (CD). We will guide you through deploying new Google Cloud Functions to Google Cloud using a Harness CD pipeline.

Harness CD pipelines help you to orchestrate and automate your Google Cloud Function deployments and push updated functions to Google Cloud.

Pipelines allow extensive control over how you want to progress functions through various dev/test/stage/prod environments while running a variety of scans and tests to ensure your quality and stability standards.

Before you begin

Verify that you have the following:

  1. Obtain GitHub personal access token with the repo scope. See the GitHub documentation on creating a personal access token.
  2. A Kubernetes cluster. Use your own Kubernetes cluster or we recommend using K3D for installing a Harness Delegate.
  3. Install the Docker in order to install the Docker delegate.
  4. Fork the harnesscd-example-apps repository through the GitHub website.
    • For details on Forking a GitHub repository, go to GitHub docs for more information on forking a GitHub repository.
  5. ** Google Cloud Function and Google Cloud Storage permissions**. Harness supports Google Cloud Functions 1st gen and 2nd gen. There are minor differences in the permissions required by each generation. For a detailed breakdown, go to Access control with IAM from Google.

Google Cloud Function Support

Cloud Functions offers two product versions: Cloud Functions 1st gen, the original version, and Cloud Functions 2nd gen, a new version built on Cloud Run and Eventarc to provide an enhanced feature set. Harness supports deploying Google Functions 1st gen and 2nd gen.

To get more information on Google Functions 1st gen and 2nd gen, go to Cloud Functions version comparison.

We suggest opting for Cloud Functions 2nd gen whenever feasible for new functions, as it offers a range of additional features. For more information, go to New in Cloud Functions(2nd Gen).

Cloud Functions Limitations

  • For Google Cloud Functions 2nd gen, Harness does not support Google Cloud Source Repository at this time. Only Google Cloud Storage is supported.
  • For Google Cloud Functions 1st gen, Harness supports both Google Cloud Storage and Google Cloud Source.

Steps to perform on Google Cloud

  • Make sure your Google Cloud account credentials have the required permissions for Google Cloud Function and Google Cloud Storage, as mentioned above.
  • Download the hello_world.zip for 1st gen and helloHttp.zip for 2nd gen files from GitHub.
  • Log into the Google Cloud console and create a Google Cloud Storage bucket. For details, go to Create a new bucket.
  • Once the bucket is created, select UPLOAD FILES and upload the file hello_world.zip for 1st gen or helloHttp.zip for 2nd gen.

Getting Started with Harness CD

  1. Log into Harness.
  2. Select Projects, and then select Default Project.
warning

For the pipeline to run successfully, please follow the remaining steps as they are, including the naming conventions.

Delegate

What is the Harness Delegate?

The Harness Delegate is a service that runs in your local network or VPC to establish connections between the Harness Manager and various providers such as artifacts registries, cloud platforms, etc. The delegate is installed in the target infrastructure, for example, a Kubernetes cluster, and performs operations including deployment and integration. Learn more about the delegate in the Delegate Overview.

  1. Under Project Setup, select Delegates.
    • Select Delegates.
      • Select New Delegate.
      • Select Docker. For this tutorial, let's explore how to install a delegate using Docker.
      • In the command provided, ACCOUNT_ID and MANAGER_ENDPOINT are auto-populated values that you can obtain from the delegate installation wizard.
      • Replace DELEGATE_TOKEN in the command with the token from the wizard.
      docker run -dit --cpus=1 --memory=2g \
      -e DELEGATE_NAME=docker-delegate \
      -e NEXT_GEN="true" \
      -e DELEGATE_TYPE="DOCKER" \
      -e ACCOUNT_ID=ACCOUNT_ID \
      -e DELEGATE_TOKEN=DELEGATE_TOKEN \
      -e LOG_STREAMING_SERVICE_URL=https://app.harness.io/gratis/log-service/ \
      -e MANAGER_HOST_AND_PORT=https://app.harness.io/gratis harness/delegate:23.05.79310
      • Select Verify to verify that the delegate is installed successfully and can connect to the Harness Manager.
note

You can also follow the Install Harness Delegate on Kubernetes or Docker steps to install the delegate using the Harness Terraform Provider or a Kubernetes manifest.

Secrets

What are Harness secrets?

Harness offers built-in secret management for encrypted storage of sensitive information. Secrets are decrypted when needed, and only the private network-connected Harness Delegate has access to the key management system. You can also integrate your own secret manager. To learn more about secrets in Harness, go to Harness Secret Manager Overview.

  1. Under Project Setup, select Secrets.
    • Select New Secret, and then select Text.
    • Enter the secret name harness_gitpat.
    • For the secret value, paste the GitHub personal access token you saved earlier.
    • Select Save.
  2. Under Project Setup, select Secrets.
    • Select New Secret, and then select File.
    • Enter the secret name gcpsecret.
    • For the secret value, upload the Google Cloud's account service key file for your service account, which you can download from your GCP project.
    • Select Save.
  1. Download and configure the Harness CLI.
curl -LO https://github.com/harness/harness-cli/releases/download/v0.0.24-Preview/harness-v0.0.24-Preview-darwin-amd64.tar.gz
tar -xvf harness-v0.0.24-Preview-darwin-amd64.tar.gz
export PATH="$(pwd):$PATH"
echo 'export PATH="'$(pwd)':$PATH"' >> ~/.bash_profile
source ~/.bash_profile
harness --version
  1. Clone the Forked harnesscd-example-apps repo and change directory.

    git clone https://github.com/GITHUB_ACCOUNTNAME/harnesscd-example-apps.git
    cd harnesscd-example-apps
    note

    Replace GITHUB_ACCOUNTNAME with your GitHub account name.

  2. Log into Harness from the CLI.

    harness login --api-key  --account-id HARNESS_API_TOKEN
    note

    Replace HARNESS_API_TOKEN with Harness API token that you obtained during the prerequisite section of this tutorial.

What are connectors?

Connectors in Harness enable integration with 3rd party tools, providing authentication and operations during pipeline runtime. For instance, a GitHub connector facilitates authentication and fetching files from a GitHub repository within pipeline stages. For more details, go to Connectors.

  1. Create the GitHub connector.
    1. In projectIdentifier, verify that the project identifier is correct. You can see the Id in the browser URL (after account). If it is incorrect, the Harness YAML editor will suggest the correct Id.
    2. Now create the GitHub connector using the following CLI command:
      harness connector --file "/google_cloud_function/github-connector.yml" apply
  2. Create the GCP connector.
    1. In projectIdentifier, verify that the project identifier is correct. You can see the Id in the browser URL (after account). If it is incorrect, the Harness YAML editor will suggest the correct Id.
    2. Now create the AWS Connector using the following CLI command:
      harness connector --file "/google_cloud_function/gcp-connector.yml" apply

Environment

What are Harness environments?

Environments define the deployment location, categorized as Production or Pre-Production. Each environment includes infrastructure definitions for serverless functions, VMs, Kubernetes clusters, or other target infrastructures. To learn more about environments, go to Environments overview.

  1. Use the following CLI command to create Environments in your Harness project:

    harness environment --file "/google_cloud_function/environment.yml" apply
  2. In your new environment, add an Infrastructure Definitions using the following CLI command:

    harness infrastructure --file "/google_cloud_function/infrastructure-definition.yml" apply

Services

What are Harness services?

In Harness, services represent what you deploy to environments. You use services to configure variables, manifests, functions, and artifacts. The Services dashboard provides service statistics like deployment frequency and failure rate. To learn more about services, go to Services overview.

  1. Use the following CLI command to create Services in your Harness project.

    harness service -file "/google_cloud_function/2nd_gen/service.yml" apply

Pipeline

What are Harness pipelines?

A pipeline is a comprehensive process encompassing integration, delivery, operations, testing, deployment, and monitoring. It can utilize CI for code building and testing, followed by CD for artifact/function deployment in production. A CD Pipeline is a series of stages where each stage deploys a service to an environment. To learn more about CD pipeline basics, go to CD pipeline basics.

What are Canary deployments?

A canary deployment updates nodes in a single environment gradually, allowing you to use gates between increments. Canary deployments allow incremental updates and ensure a controlled rollout process. For more information, go to When to use Canary deployments.

  1. Enter this CLI command to create a canary deployment using the example YAML file:
    harness pipeline --file "/google_cloud_function/2nd_gen/canary-pipeline.yml" apply
  2. In Harness, you can switch to the Visual editor and confirm the pipeline stage and execution steps as shown below.

Finally, it's time to execute your pipeline.

  1. Select Run, and then select Run Pipeline to initiate the deployment.
  • Observe the execution logs as Harness deploys the function.
  • After a successful execution, you can check the deployment on your Google Cloud Function console.

Congratulations!🎉

You've just learned how to use Harness CD to deploy a Google Cloud Function to Google Cloud.

Keep learning about Harness CD. For example, add Triggers to your pipeline that initiate pipeline deployments in response to Git events.