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.  
  9. stage("Docker info") {
  10. container("dind") {
  11. sh "apk add --no-cache curl git && curl -sLS cli.openfaas.com | sh"
  12. sh "dockerd &"
  13. sh "docker info"
  14. sh "faas-cli template store pull node8-express"
  15. sh "faas-cli build"
  16. sh "killall dockerd"
  17. }
  18. }
  19. }
  20. }

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.  
  10. stage("shrinkwrap") {
  11. container("faascli") {
  12. sh "apk add --no-cache git"
  13. sh "faas-cli template store pull node8-express"
  14. sh "faas-cli build --shrinkwrap"
  15. }
  16. }
  17.  
  18. stage("build") {
  19. container(name: "kaniko", shell: '/busybox/sh') {
  20. // Remove --no-push to push to a registry
  21. // See also: https://github.com/GoogleContainerTools/kaniko#pushing-to-different-registries
  22. withEnv(['PATH+EXTRA=/busybox:/kaniko']) {
  23.  
  24. sh '''#!/busybox/sh
  25. /kaniko/executor -c /home/jenkins/workspace/Kaniko/build/timezone-shift --no-push
  26. '''
  27. }
  28. }
  29. }
  30. }
  31. }

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.