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. # Cache the templates and build-context to speed things up
  4. cache:
  5. key: ${CI_COMMIT_REF_SLUG} # i.e. master
  6. paths:
  7. - ./faas-cli
  8. - ./template
  9. # Build the whole stack using only the faas-cli
  10. docker-build:
  11. stage: build
  12. image: docker:dind
  13. script:
  14. - apk add --no-cache git
  15. - if [ -f "./faas-cli" ] ; then cp ./faas-cli /usr/local/bin/faas-cli || 0 ; fi
  16. - 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
  17. # Build Docker image
  18. - /usr/local/bin/faas-cli build --tag=sha --parallel=2
  19. # Login & Push Docker image to private repo
  20. - echo -n "$CI_DOCKER_LOGIN" | docker login --username admin --password-stdin ae-reg.team-serverless.xyz
  21. - /usr/local/bin/faas-cli push --tag=sha
  22. - echo -n "$CI_OPENFAAS_PASSWORD" | /usr/local/bin/faas-cli login --username admin --password-stdin
  23. # Deploy function from private repo
  24. - /usr/local/bin/faas-cli deploy --tag=sha --send-registry-auth
  25. only:
  26. - 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