🇬🇧 Manage and Deploy an OpenFaaS function with GitLab

OpenFaas is my favorite FaaS platform, because it's easy to setup and to use, the community is very friendly, and last but not least, deploying an OpenFaaS function with GitLab CI is very simple.

Today, I'm writting some steps to demonstrate it.

Prerequisites:

  • a GitLab instance with GitLab runner(s) (I use a private instance, but you can use GitLab.com too)
  • an OpenFaaS platform
  • the faas-cli installed on the GitLab runner (I use a shell runner)
  • the docker client installed on the GitLab runner

First, create a "hello" function

mkdir hello-project
cd hello-project
# get a template
faas template pull https://github.com/openfaas-incubator/node10-express-template
# create the function
faas new hello --lang node10-express

Update the hello.yml file

The faas-cli has created a hello.yml file to help you to build and deploy the "hello" function:

version: 1.0
provider:
  name: openfaas
  gateway: http://127.0.0.1:8080
functions:
  hello:
    lang: node10-express
    handler: ./hello
    image: hello:latest

Then, change it like that:

version: 1.0
provider:
  name: openfaas
  gateway: ${OPENFAAS_URL} #1
functions:
  hello-${CI_COMMIT_REF_SLUG}: #2
    lang: node10-express
    handler: ./hello
    image:  ${DOCKER_REGISTRY}/hello-${CI_COMMIT_REF_SLUG}:latest #3

The faas-cli can substitute environment variables, so, there are some variables that you will copy in your GitLab CI settings:

  • #1: OPENFAAS_URL the url of your OpenFaas platform (eg: http://openfaas.test:8080)
  • #2: CI_COMMIT_REF_SLUG this is the branch name of the project lowercased, shortened to 63 bytes (see: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html)

    it means that when you will deploy the "hello" function from the master branch, the name of the deployed function will be hello-master

  • #3: DOCKER_REGISTRY, I use a private registry (it's not mandatory) (eg: registry.test:5000)

You will need to create another environment variables settings: OPENFASS_TOKEN (the token is generated when installing OpenFaas)

Create the .gitlab-ci.yml file

To be able to build, push and deploy, redeploy your function, create review application of your function (another version of your function, running at the same time), you have to creata a .gitlab-ci.yml file:

stages:
  - 📦build
  - 🐳package
  - 🚀deploy
  - 🦄deploy

function_build:
  stage: 📦build
  only:
    - master
    - merge_requests
  script: |
    faas-cli build -f hello.yml

function_push:
  stage: 🐳package
  only:
    - master
    - merge_requests
  script: |
    faas-cli push -f hello.yml 

function_deploy:
  stage: 🚀deploy
  environment:
    name: production/hello-${CI_COMMIT_REF_SLUG}
    url:  $OPENFAAS_URL/function/hello-${CI_COMMIT_REF_SLUG}
  only:
    - master
  script: |
    export OPENFAAS_URL=${OPENFAAS_URL}
    echo -n ${OPENFASS_TOKEN} | \
    faas-cli login --username=admin --password-stdin
    faas-cli deploy -f hello.yml

preview_function_deploy: #1
  stage: 🦄deploy
  environment:
    name: preview/hello-${CI_COMMIT_REF_SLUG}
    url:  $OPENFAAS_URL/function/hello-${CI_COMMIT_REF_SLUG}
    on_stop: stop_preview_function #2
  only:
    - merge_requests
  script: |
    export OPENFAAS_URL=${OPENFAAS_URL}
    echo -n ${OPENFASS_TOKEN} | \
    faas-cli login --username=admin --password-stdin
    faas-cli deploy -f hello.yml

stop_preview_function:
  stage: 🦄deploy
  only:
    - merge_requests
  when: manual
  environment:
    name: preview/hello-${CI_COMMIT_REF_SLUG}
    action: stop
  script: |
    faas-cli remove -f hello.yml
  • #1: the preview_function_deploy job is executed only when there is a merge request
  • #2: when you merge on master and delete the source branch, the stop_preview_function job is triggered and the review function is deleted

Update hello/handler.js

You can change a little the source code of the function, for example, this one return html code:

"use strict"

module.exports = (event, context) => {

  context
    .headers({"Content-Type": "text/html"})
    .status(200)
    .succeed(`
      <h1>
        👋 Hello People 🌍
      </h1>
    `)
}

Change .gitignore

Remove the template line of the .gitignore file:

#template
build

"Push" your project to GitLab, and deploy from master

First, on your GitLab instance, create an empty project: hello-project

and locally, in your project folder, type these commands:

git init
git remote add origin git@gitlab.bots.garden:openfaas_demo/hello-project.git #1
git add .
git commit -m "Initial commit"
git push -u origin master
  • #1: replace with the information of your GitLab instance, group and project

Then, a pipeline will be triggered, and your function will be deployed automatically and you can reach it on http://openfaas.test:8080/function/hello-master (in my case).

Deploy a review application

Change something in the source code, and create a merge request with a feature branch named (for example) change-title

"use strict"

module.exports = (event, context) => {

  context
    .headers({"Content-Type": "text/html"})
    .status(200)
    .succeed(`
      <h1>
        👋 Hello World 🌍
      </h1>
    `)
}

Then, submit your merge request, ad it will trigger a new pipeline, creating a new "review application/function" with your changhe, and you can reach it on http://openfaas.test:8080/function/hello-change-title - you can note that the end of the url has changed from master to change-title.

And now, if you merge on master, the hello-change-title function will be deleted, and a new hello-master function will be re-deployed with your changes.

That's all and it's easy.

This was a quick blog post, so don't hesitate to ping me if you need more information - have a nice day.

Last Articles

Last Updated: 9/16/2019, 6:35:21 AM