Spacebear Blog

Deploy OpenFaaS functions using Drone

Published: June 19, 2020

Drone is a CI/CD server that supports many different forges (GitHub, GitLab, Gitea, and more!) and OpenFaaS is a provider agnostic functions-as-a-service solution. Combining these two takes OpenFaaS to the next level with GitOps. An alternative to this guide is to use OpenFaaS cloud which provides multi-user capabilities and more than this guide can provide, but you are limited to GitHub, and GitLab as git providers.

Background

I use OpenFaaS functions to enhance my usage of Drone (via Drone extensions) and so I figure I would also use Drone to deploy OpenFaaS functions.

Prerequisites

This tutorial assumes you have OpenFaaS installed via Kubernetes, Docker Swarm, or faasd, and also the OpenFaaS CLI installed locally so that you can easily scaffold out base functions utilizing pre-built function templates. This guide assumes you are publishing your functions to Docker Hub, and your images are public.

User Guide

First you’ll need to create a git repo that will contain one, or more OpenFaaS functions. Next activate your repo in Drone, and add the secrets.

The secrets in drone you’ll need to add are:

  • docker_username
  • docker_password
  • openfaas_url
  • openfaas_password

Next, you’ll need to create your function. As an example create a new function called func with faas-cli new func --lang go. After running you’ll see a couple new files including func.yml. The newely created function yaml looks like:

version: 1.0
provider:
  name: openfaas
functions:
  func:
    lang: go
    handler: ./func
    image: func:latest

You’ll need to update the yaml image field, which is the name of the docker image that will be generated and will need to match in your drone yaml, to point to the image location on Docker Hub. In our case we are using spacebear/func:latest as an example.

As modifying the function to something more than a hello world is out of scope in this guide we will skip that part and leave that up as an exercise for the reader.

Then finally, we need to add the .drone.yml to your repo that will provide the instructions to Drone to build and deploy your function.

This Drone Yaml has three steps. The first step bundles your function all together in a package that docker can use to do a docker build. The second step is to take that package from the first step and build the docker image. In this step you’ll need to update the repo with your docker image name from above. After it is done building the docker image, it will also push it to docker hub. The last step logs into your OpenFaaS implementation, and then deploys your function. Please note that this step doesn’t push the docker image to OpenFaaS, but rather tells OpenFaaS metadata around the function, including which docker image to pull and deploy.

kind: pipeline
type: docker
name: default

steps:
- name: faas build
  image: openfaas/faas-cli:0.12.8
  user: root
  commands:
  - faas-cli build --shrinkwrap -f func.yml

- name: docker build
  image: plugins/docker
  settings:
    dockerfile: build/func/Dockerfile
    context: build/func/
    repo: spacebear/func
    username:
      from_secret: docker_username
    password:
      from_secret: docker_password

- name: faas deploy
  image: openfaas/faas-cli:0.12.8
  user: root
  commands:
  - echo $OPENFAAS_PASSWORD | faas-cli login -s
  - faas-cli deploy -f func.yml
  environment:
    OPENFAAS_PASSWORD:
      from_secret: openfaas_password
    OPENFAAS_URL:
      from_secret: openfaas_url

Future

Areas where you could expand include updating Drone yaml to deploy PRs on the git repository to a staging environment, or to simply to a dry-run build to ensure that everything compiles, and the docker image builds.