Code Monkey home page Code Monkey logo

drone-gcf's Introduction

Build Status Coverage Status

Drone-GCF - a Drone.io plugin to deploy Google Cloud Functions

Overview

The plugin supports deploying, listing, calling, and deleting of multiple functions at once. See below for an example drone.yml configuration that deploys several functions to GCP Cloud Functions and later deletes two of them. A service account is needed for authentication to GCP and should be provided as json string via drone secrets. In the configuration below, the json of the service account key file is stored in the drone secret called token.

Deploying Cloud Functions

Example .drone.yml file (drone.io 1.0 format):

kind: pipeline
name: default

steps:
  - name: test
    image: golang
    commands:
      - "go test -v"
    when:
      event:
        - push


  - name: deploy-cloud-functions
    image: oliver006/drone-gcf
    settings:
      action: deploy
      project: myproject
      token:
        from_secret: token
      runtime: go111
      env_secret_db_password:
        from_secret: db_password_prod
      env_secret_user_api_key:
        from_secret: user_api_key_prod
      functions:
        - TransferFileToGCS:
          - trigger: http
            memory: 2048MB
            allow_unauthenticated: true
            environment:
              - ENV_VAR: env_var_value_123
        - HandleEvents:
          - trigger: topic
            trigger_resource: "projects/myproject/topics/mytopic"
            memory: 512MB
        - ProcessEmails:
          - trigger: http
            memory: 512MB
            runtime: python37
            source: ./python/src/functions/
            vpcconnector: vpc-connector
            env_vars_file: ".env.yaml"


    when:
      event: push
      branch: master


  - name: delete-cloud-functions
    image: oliver006/drone-gcf
    settings:
      action: delete
      functions:
        - TransferFileToGCS
        - HandleEvents
    when:
      event: tag

The plugin supports several types of Google Cloud Function triggers:

  • http - triggered for every request to an HTTP endpoint, no other parameters are needed. (see gcloud output for URL of the endpoint).
  • bucket - triggered for every change in files in a GCS bucket. Supply the name of the bucket via trigger_resource.
  • topic - triggered for every message published to a PubSub topic. Supply the name of the topic via trigger_resource.
  • event - triggered for every event of the type specified via trigger_event of the resource specified via trigger_resource.

See the output of gcloud functions deploy --help for more information regarding the setup of triggers.

When deploying a function, there is the option to deploy as a public function. This can be configured by setting allow_unauthenticated to true. This adds the --allow-unauthenticated flag described here to the deploy command. Please note that this expects a boolean value, either true or false.

By default, the plugin will use the GCP project ID of the service account provided but you can override it by setting the project parameter.

The runtime can be set on a per-function basis or for all functions at once. In the example above, the runtime is set to go111 for all functions and then overwritten with python37 for just ProcessEmails. This will result in the plugin deploying three functions, two in Golang and one in Python.
If no runtime setting is provided at all, the plugin will fail.

Similarly, you can set the source location of each function in case you keep the code in separate folders.

There are two ways to set environment variables when deploying cloud functions:

  • from a secret
  • putting the value directly into the drone.yml file

To pull in an environment variable value from a secret, add an entry to the settings that starts with env_secret_ followed by the name as which the variable will be made available to the cloud function. In the config example above, drone will pull the values from the secrets db_password_prod and user_api_key_prod and make them available as DB_PASSWORD and USER_API_KEY. (env variable names will be upper-case) \

Environment variables from secrets will be made available to all functions that you deploy within one drone step. If, for whatever reasons, this is not acceptable and you need to keep them separate then you have to use multiple drone steps, one for each function.

If you run into issues when setting environment variables with special characters in their values, there's a setting you can use to specify a delimiter string to be used as separation between variables. Normally, gcloud would use a comma (,), but we've set the default to something more unlikely to cause any issue (:|:). If you still need to change it, you can use the environment_delimiter setting.

Calling Cloud Functions

You can also trigger a cloud function by using call as the action. Optionally, you can supply a json data string that will be passed to the function.

  - name: call-cloud-function
    image: oliver006/drone-gcf
    settings:
      action: call
      project: myproject
      token:
        from_secret: token
      functions:
        - UpdateDatabase:
          - data: '{"key": "value"}'
    when:
      event: tag

drone-gcf's People

Contributors

anagas avatar aromaniuk-postclick avatar leanazulyoro avatar oliver006 avatar raybb avatar sduncan-nyt avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.