CI/CD with GitLab

CI/CD can be achieved in a number of ways. This page outlines how to create a simple pipeline for automatically deploying new versions of your function every time there is a git push event on the repo.

Build with .gitlab-ci.yml

Pre-reqs:

  • At least one CI runner registered, available and capable of running Docker.To achieve CI/CD with GitLab you can create a file named .gitlab-ci.yml with the following contents:
  1. stages:
  2. - build
  3.  
  4. # Cache the templates and build-context to speed things up
  5. cache:
  6. key: ${CI_COMMIT_REF_SLUG} # i.e. master
  7. paths:
  8. - ./faas-cli
  9. - ./template
  10.  
  11. # Build the whole stack using only the faas-cli
  12. docker-build:
  13. stage: build
  14. image: docker:dind
  15. script:
  16. - apk add --no-cache git
  17. - if [ -f "./faas-cli" ] ; then cp ./faas-cli /usr/local/bin/faas-cli || 0 ; fi
  18. - if [ ! -f "/usr/local/bin/faas-cli" ] ; then apk add --no-cache curl git && curl -sSL cli.openfaas.com | sh && chmod +x /usr/local/bin/faas-cli && /usr/local/bin/faas-cli template pull && cp /usr/local/bin/faas-cli ./faas-cli ; fi
  19.  
  20. # Build Docker image
  21. - /usr/local/bin/faas-cli build --tag=sha --parallel=2
  22.  
  23. # Login & Push Docker image to private repo
  24. - echo -n "$CI_DOCKER_LOGIN" | docker login --username admin --password-stdin ae-reg.team-serverless.xyz
  25. - /usr/local/bin/faas-cli push --tag=sha
  26. - echo -n "$CI_OPENFAAS_PASSWORD" | /usr/local/bin/faas-cli login --username admin --password-stdin
  27.  
  28. # Deploy function from private repo
  29. - /usr/local/bin/faas-cli deploy --tag=sha --send-registry-auth
  30.  
  31. only:
  32. - master

Note: You must also change the value of ae-reg.team-serverless.xyz to your own registry.

For Kubernetes you do not need the flag of —send-registry-auth for the faas-cli deploy command.

Save this file in the git repo root of each set of functions you want to build and deploy.

Build functions for your team or whole instance

It may be tiresome to maintain individual files for each repo and project. OpenFaaS Cloud automates this task and supports both GitHub and GitLab.

See also: OpenFaaS Cloud