The OpenFaaS CLI supports various options for building a function.
For details and examples run
faas-clibuild--help
Build images with Docker
The faas-cli build command builds a Docker image into your local Docker library, which can then be used locally or pushed into a remote Docker registry. Each change of your function requires a new faas-cli build command to be issued.
How to do CI/CD
When it comes to continuous integration and delivery you can use the faas-cli tool on your build server to build and deploy your code using the built-in commands.
Generate a Dockerfile with --shrinkwrap
If you are using an alternative container image builder or are automating the faas-cli then you can use the --shrinkwrap flag which will produce a folder named ./build/function-name with a Dockerfile. This bundle can be used with any container builder.
This is an experimental feature which means that it may change in the future.
When using Docker's buildkit project to build your containers, faas-cli can pass in the arguments to mount different secrets into the build process.
Any other mechanism should be considered insecure because it will leak into the final image or the local image in one way or another.
For Go users, make use of vendoring. It's what we use and it means you do not have to resort to insecure practices like sharing Personal Access Tokens (PAT) between users.
Below we have an example for Python using the pip package manager and for node modules with npm. The approach is similar for different package managers.
Download and enable the OpenFaaS Pro plugin
Create a local file in the format required
Update a build_secret in stack.yml so it gets mounted into the container
Run faas-cli pro build or faas-cli pro publish, faas-cli pro up is not available at this time
You will need to create an authentication token to install private npm modules. These instructions will differ depending on the registry you want to use:
You'll also need an updated version of the node template to mount the secret passed in from the OpenFaaS Pro plugin. Update template/node22/Dockerfile and replace the second npm i command with:
The OpenFaaS CLI enables functions to be built with different options, e.g. dev, debug, etc.
By default all templates provide a minimal build as this optimizes function image sizes. Where appropriate, 3rd-party dependencies can be specified via requirements.txt. In scenarios where third-party dependencies also require native (e.g. C/C++) modules,
like libssh in Ruby and numpy or pandas in Python, then --build-option can be used.
How to use
The OpenFaaS CLI provides a --build-option flag which enables named sets of native modules to be specified for inclusion in the function build.
Where multiple functions are being built, the YAML configuration is recommended over use of the CLI flag, as the CLI flag applies the --build-option to all functions involved in the build activity.
Currently, of the official templates, Python and Ruby templates include named build options.
Edit templates to support additional build options
It is possible to amend build options in both official and custom templates.
Altering of official templates should be carefully considered in the context of repeatable builds
In order to modify a template to support further build options, edit the template.yml using the following pattern:
build_options:-name:devpackages:# A list of required packages-make-automake-gcc#- etc.-name:debugpackages:-mg-iw#- etc.
and if not already present edit Dockerfile with:
# Add the following lineARGADDITIONAL_PACKAGE# Edit `RUN apk --no-cache add curl \` to the followingRUNapk--no-cacheaddcurl${ADDITIONAL_PACKAGE}\
There may be scenarios where a single native module needs to be added to a build. A single-package build option could be added as described above. Alternatively a package could be specified through a --build-arg.
Performing a build action against a stack.yml which contains a large suite of serverless function definitions will result in each of the defined functions being built. The CLI makes available facilities that assist in this scenario.
The --parallel flag aims to reduce total build time by enabling more than one function build action to take place concurrently. Additionally, there may be situations where building all the defined functions is undesirable - for example where only one of the functions has had its code updated. In this instance the --filter and --regex flags can be used.
Consider a project with fn1, fn2, fn3, fn22, fn33 functions all defined within a single YAML file.
Parallel enables the user to specify how many concurrent function build actions should be performed. The default is that functions will be built serially, one after the other.
The following will see all the project functions' build actions performed concurrently:
faas-clibuild--parallel5
Note
Remember to add -f if using a non-default yaml file: faas-cli build --parallel 5 -f projectfile.yml
Parallel can be combined with either of the --filter and --regex flags to parallel build a subset of the functions.
Regex performs a similar action to --filter but allows for more complex patterns to be defined through regular expressions.
The following regex would result in fn1, fn2 & fn3 being built from the earlier project's stack.yml:
faas-clibuild--regex"fn[0-9]$"
Building multi-arch images for ARM and Raspberry Pi¶
If you're Raspberry Pi or ARM servers to run your OpenFaaS on Kubernetes or with faasd server, then you will need to use the publish command instead which uses emulation and in some templates cross-compilation to build an ARM image from your PC.
It is important that you do not install Docker or any build tools on your faasd instance. faasd is a server to serve your functions, and should be treated as such.
The technique used for cross-compilation relies on Docker's buildx extension and buildkit project. This is usually pre-configured with Docker Desktop, and Docker CE when installed on an Ubuntu system.
First install the QEMU utilities which allow for cross-compilation: