Jenkins for building OpenFaaS Functions

Setup Jenkins on Kubernetes

Use helm to install Jenkins

  1. $ helm install --name cicd stable/jenkins --set rbac.install=true

The chart will give you a command to fetch your login password and public URL. By default it will also create a LoadBalancer in Kubernetes.

Create a pipeline with Docker in Docker

Create a new Jenkins Pipeline job:

  1. def label = "mypod-${UUID.randomUUID().toString()}"
  2. podTemplate(label: label,
  3. containers: [
  4. containerTemplate(privileged: true, name: 'dind', image: 'docker:dind', ttyEnabled: true, command: 'cat')
  5. ]) {
  6. node(label) {
  7. checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/alexellis/openfaas-cloud-test']]])
  8. stage("Docker info") {
  9. container("dind") {
  10. sh "apk add --no-cache curl git && curl -sLS cli.openfaas.com | sh"
  11. sh "dockerd &"
  12. sh "docker info"
  13. sh "faas-cli template store pull node8-express"
  14. sh "faas-cli build"
  15. sh "killall dockerd"
  16. }
  17. }
  18. }
  19. }

You should replace https://github.com/alexellis/openfaas-cloud-test with your own Git repository.

If you have custom templates then run faas-cli template store pull <template> before faas-cli build.

Note: the following example should not be used in a production cluster due to the use of a privileged container to build the Docker image. A tool such as Kaniko from Google could be used do perform a non-privileged build, but is still not suitable for building untrusted code.

Create a pipeline with Kaniko

  1. def label = "mypod-${UUID.randomUUID().toString()}"
  2. podTemplate(label: label,
  3. containers: [
  4. containerTemplate(privileged: false, name: 'faascli', image: 'openfaas/faas-cli:0.8.6', ttyEnabled: true, command: 'cat'),
  5. containerTemplate(name: "kaniko", image:"gcr.io/kaniko-project/executor:debug", ttyEnabled: true, command: "/busybox/cat")
  6. ]) {
  7. node(label) {
  8. checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/alexellis/openfaas-cloud-test']]])
  9. stage("shrinkwrap") {
  10. container("faascli") {
  11. sh "apk add --no-cache git"
  12. sh "faas-cli template store pull node8-express"
  13. sh "faas-cli build --shrinkwrap"
  14. }
  15. }
  16. stage("build") {
  17. container(name: "kaniko", shell: '/busybox/sh') {
  18. // Remove --no-push to push to a registry
  19. // See also: https://github.com/GoogleContainerTools/kaniko#pushing-to-different-registries
  20. withEnv(['PATH+EXTRA=/busybox:/kaniko']) {
  21. sh '''#!/busybox/sh
  22. /kaniko/executor -c /home/jenkins/workspace/Kaniko/build/timezone-shift --no-push
  23. '''
  24. }
  25. }
  26. }
  27. }
  28. }

You will need to change the name of the workspace used in the build stage.

You should also update the Git URL in the checkout section.