You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Renames the folders / files for current date
* Clarify introduction and purpose of the post
* Update ordering of steps to make creating GitHub repo
simpler
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
title: "Build and deploy OpenFaaS functions with GitHub Actions"
3
+
description: "Build and deploy functions to OpenFaaS anywhere with GitHub Actions and multi-arch images"
4
+
date: 2020-10-13
5
+
image: /images/2020-10-github-actions/lego.jpg
6
6
categories:
7
7
- faas-netes
8
8
- faasd
@@ -13,66 +13,76 @@ author_staff_member: utsav
13
13
dark_background: true
14
14
---
15
15
16
-
Learn how to bring CI-CD to your OpenFaaS functions with Github Actions
16
+
Build and deploy functions to OpenFaaS anywhere with GitHub Actions and multi-arch images
17
17
18
-
## Who should read this?
18
+
## Introduction: automating function updates
19
19
20
-
OpenFaaS was created to have the freedom to run serverless functions anywhere, be it on a Raspberry Pi, in a data center, or the cloud. This started with faas-swarm to run on Docker Swarm and faas-netes to run on Kubernetes. faas-netes is the current recommendation for running OpenFaaS in production.
21
-
If you're not a large team, or just someone who doesn't want to deal with the complexities that comes with Kubernetes then you're much better off with faasd.
20
+
OpenFaaS was created to have the freedom to run serverless functions anywhere you want, whether that be within your on-premises environment, on AWS, or even on a Raspberry Pi in your home. Whichever way you're running OpenFaaS, you won't get far without a way to build and deploy functions, and at small scale, you may be doing this manually.
22
21
23
-
But whatever shape or form you're running OpenFaaS as, if you want more automation for how your functions are built and deployed with Github Actions, this will be a good read for you.
22
+
When you're ready to level-up your operations, and update functions after each commit is pushed, or each Pull Request is merged, then build systems like GitLab CI, Travis, CircleCI and GitHub Actions become essentials parts of your infrastructure.
24
23
25
-
In this tutorial, we'll see how we can get OpenFaaS functions up and running with Github Actions on a faas-netes deployment running on GKE, but the same approach is valid for all forms of OpenFaaS as it exists today whether it is faas-netes or faasd, running either in the cloud or on your desk.
24
+
Over the past few years this has meant installing Docker on the build system, and running the `docker` command to build an image, push it to a registry, and to then deploy it. Recently advancements in cross-compilation and the newer container builder `buildkit` means we can build images for multiple platforms with ease, so that an image can be deployed to an AWS Graviton instance with an ARM processor, and to a regular EC2 node using a `x86_64` (Intel) processor.
26
25
26
+
So in this tutorial I'll show you how to build and deploy functions anywhere using GitHub Actions and multi-arch images that can run on a cloud instance, or on your Raspberry Pi homelab.
27
27
28
-
## Prerequisites
28
+
###Prerequisites
29
29
30
-
* An OpenFaaS deployment that is accessible from the internet
30
+
Kubernetes is our recommendation for teams running at scale, but [faasd](https://www.openfaas.com/blog/faasd-tls-terraform/) provides a suitable alternative for smaller teams.
31
31
32
-
Get started with the official documentation [here](https://docs.openfaas.com/deployment/)
32
+
* An OpenFaaS deployment using Kubernetes or faasd
33
+
34
+
You can deploy OpenFaaS [here](https://docs.openfaas.com/deployment/)
35
+
36
+
* Publicly accessible network OpenFaaS gateway
37
+
38
+
Your OpenFaaS gateway should be accessible from the Internet and have TLS enabled for security. If you are running behind a firewall, or at home then checkout the [inlets-operator project](https://docs.inlets.dev/) to make your OpenFaaS gateway accessible from the Internet.
33
39
34
40
* faas-cli
35
41
36
-
faas-cli is a command line tool used to interact with OpenFaaS, from deploying your functions to viewing logs.
37
-
Follow the installation instructions [here](https://docs.openfaas.com/cli/install/)
42
+
faas-cli is a command line tool used to interact with OpenFaaS, from deploying your functions to viewing logs.
43
+
Follow the installation instructions [here](https://docs.openfaas.com/cli/install/)
38
44
39
-
> You can follow along with the code used in this tutorial from this [repo](https://github.com/utsavanand2/hello)
45
+
> All code samples are available in this repo: [github.com/utsavanand2/hello](https://github.com/utsavanand2/hello)
40
46
47
+
### Create a function using the Golang template
41
48
42
-
## Create an OpenFaaS function
49
+
Create a repository on GitHub called `hello` and clone it.
43
50
44
-
I'm going to create and deploy a Golang function for this tutorial.
51
+
I'm going to create and deploy a Golang function for this tutorial using the `golang-http` template, which closely resembles a HTTP handler in Go's standard library.
45
52
46
-
```sh
47
-
# create a new directory for our project
48
-
mkdir hello
49
-
# pull golang-http templates
53
+
```bash
54
+
# Change the directory into the repository
55
+
cd hello
56
+
57
+
# Pull golang-http templates
50
58
faas-cli template store pull golang-http
51
-
# create a new function with faas-cli
59
+
60
+
# Create a new function with faas-cli
52
61
faas-cli new hello --lang golang-http
53
62
```
54
63
55
64
If you look into the root directory of the project, faas-cli has created two additional files:
56
65
57
-
```
66
+
```bash
58
67
./hello/handler.go
59
68
./hello.yml
60
69
```
61
70
62
71
The content of our handler looks something like this:
[GitHub Action workflows](https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/introduction-to-github-actions) is one of the many core components of GitHub Actions that lets users add an automated workflow that executes on an event, and can be used to test, build and deploy our code.
69
80
70
-
[Github Action workflows](https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/introduction-to-github-actions) is one of the many core components of Github Actions that lets users add an automated workflow that executes on an event, and can be used to test, build and deploy our code.
71
81
A workflow can be composed of multiple steps, each executing a particular action.
72
82
There are many published [Actions](https://github.com/marketplace?type=actions) that provide nice wrappers for common actions and tools making them easier to use, but we can also use any published Docker image. The OpenFaaS team already publishes an image for `faas-cli` that is ready to use for any workflow.
> Note: Replace the Docker UserID from the image tag name with your own.
150
+
> Note: replace the `DOCKER_USERNAME` from the image tag name with your own.
141
151
142
-
Since Github Actions requires us to use a Docker image that has a root user invoking the commands using the Docker action, so we're using the root variant of the faas-cli Docker image. Read more about it [here](https://docs.github.com/en/free-pro-team@latest/actions/creating-actions/dockerfile-support-for-github-actions#user)
152
+
Since GitHub Actions requires us to use a Docker image that has a root user invoking the commands using the Docker action, so we're using the root variant of the faas-cli Docker image. Read more about it [here](https://docs.github.com/en/free-pro-team@latest/actions/creating-actions/dockerfile-support-for-github-actions#user)
143
153
144
154
The faas-cli:latest-root image has the faas-cli installed as the entrypoint, so everything set in args is passed to the faas-cli.
145
155
This will work with any of the faas-cli root tags, you can pin to any specific version of faas-cli, for example: `openfaas/faas-cli:0.12.14-root`.
146
156
147
-
## Create and configure a Github Repo with Github Actions
157
+
### Add secrets to your GitHub repo for the build
148
158
149
-
Initialize a git repo in the root directory of the project
159
+
Add the following secrets and their values to the repo for GitHub Actions to build push and deploy your OpenFaaS functions. Make sure that the secret names correspond with the GitHub workflow YAML file as defined above in the previous section.
Create a Github Repo named hello and add the following secrets and their values to the repo for Github Actions to build push and deploy your OpenFaaS functions. Make sure that the secret names correspond with the Github workflow YAML file as defined above in the previous section.
You can take this to the next level by leveraging multi-arch builds with Docker's [Buildx](https://docs.docker.com/buildx/working-with-buildx/) and the golang-http-templates which now support multi-arch builds. This means that you can deploy the same function to architectures like arm, arm64 of amd64 from a single build.
191
185
192
-
Just add a step to setup `QEMU` and pass a `platforms` key in the build step, making the last few steps concerned with Docker look something like this:
186
+
The two changes are to set up an emulation tool for Linux called [qemu](https://www.qemu.org/) and to provide a list of desired architectures for the images.
193
187
194
188
```yaml
195
189
-
@@ -216,12 +210,16 @@ Just add a step to setup `QEMU` and pass a `platforms` key in the build step, ma
216
210
tags: utsavanand2/hello:latest
217
211
```
218
212
219
-
You can checkout the workflow YAML supporting multi-arch builds [here](https://github.com/utsavanand2/hello/blob/multi-arch/.github/workflows/main.yml):
213
+
You can see the full workflow YAML file supporting multi-arch builds here: [main.yml](https://github.com/utsavanand2/hello/blob/multi-arch/.github/workflows/main.yml):
214
+
215
+
### Join the community
220
216
217
+
Have you got questions, comments, or suggestions? Join the community on [Slack](https://slack.openfaas.io).
221
218
219
+
Would you like help setting up your OpenFaaS installation, or someone to call when things don't quite go to plan? [Our Premium Subscription plan](https://www.openfaas.com/support/) gives you a say in the project roadmap, a support contact, and access to Enterprise-grade authentication with OIDC.
222
220
223
-
## Acknowledgements
221
+
### Acknowledgements
224
222
225
-
Special Thanks to [Lucas Rosler](https://twitter.com/TheAxeR) and [Alex Ellis](https://twitter.com/alexellisuk) for all the precious advice and helping me out with making faas-cli more portable for CI environments than ever before.
223
+
Special Thanks to [Lucas Rosler](https://twitter.com/TheAxeR) and [Alex Ellis](https://twitter.com/alexellisuk) for all guidance and for merging changes into OpenFaaS to better support this workflow.
226
224
227
225
Thanks to [Dan Burton](https://unsplash.com/@single_lens_reflex) for the background picture.
0 commit comments