Skip to content

Commit 99106f6

Browse files
committed
Edits for Utsav's GitHub Actions blog post
* 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>
1 parent 1c85e47 commit 99106f6

File tree

7 files changed

+57
-59
lines changed

7 files changed

+57
-59
lines changed

_posts/2020-10-08-openfaas-functions-with-github-actions.md

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: "Deploy your functions to OpenFaaS with Github Actions"
3-
description: "Bring CI/CD to your OpenFaaS deployment with Github Actions."
4-
date: 2020-10-08
5-
image: /images/2020-10-08-openfaas-functions-with-github-actions/lego.jpg
2+
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
66
categories:
77
- faas-netes
88
- faasd
@@ -13,66 +13,76 @@ author_staff_member: utsav
1313
dark_background: true
1414
---
1515

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
1717

18-
## Who should read this?
18+
## Introduction: automating function updates
1919

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.
2221

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.
2423

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.
2625

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.
2727

28-
## Prerequisites
28+
### Prerequisites
2929

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.
3131

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.
3339

3440
* faas-cli
3541

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/)
3844

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)
4046
47+
### Create a function using the Golang template
4148

42-
## Create an OpenFaaS function
49+
Create a repository on GitHub called `hello` and clone it.
4350

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.
4552

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
5058
faas-cli template store pull golang-http
51-
# create a new function with faas-cli
59+
60+
# Create a new function with faas-cli
5261
faas-cli new hello --lang golang-http
5362
```
5463

5564
If you look into the root directory of the project, faas-cli has created two additional files:
5665

57-
```
66+
```bash
5867
./hello/handler.go
5968
./hello.yml
6069
```
6170

6271
The content of our handler looks something like this:
63-
![./hello/handler.go](/images/2020-10-08-openfaas-functions-with-github-actions/handler-go.jpg)
72+
![./hello/handler.go](/images/2020-10-github-actions/handler-go.jpg)
6473

6574
The YAML file is used to configure the deployment and the build and runtime environment of the function
66-
![./hello.yml](/images/2020-10-08-openfaas-functions-with-github-actions/hello-yaml.jpg)
75+
![./hello.yml](/images/2020-10-github-actions/hello-yaml.jpg)
76+
77+
### Setup a workflow file for GitHub Actions
6778

68-
## Setup a workflow file for Github Actions
79+
[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.
6980

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.
7181
A workflow can be composed of multiple steps, each executing a particular action.
7282
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.
7383

7484
Our workflow is a simple linear process:
75-
`Checkout` -> `Pull Templates` -> `ShrinkWrap Build` -> `OpenFaaS Login` -> `Docker Login` -> `Docker Buildx Setup` -> `Build & Push Function` -> `Deploy to OpenFaaS`
85+
`Checkout` -> `Pull Templates` -> `Shrink-wrap a Docker context` -> `OpenFaaS Login` -> `Docker Login` -> `Docker Buildx Setup` -> `Build & Push Function` -> `Deploy to OpenFaaS`
7686

7787
In the root directory of the project run:
7888

@@ -137,59 +147,43 @@ jobs:
137147
args: deploy -f hello.yml
138148
```
139149
140-
> 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.
141151

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)
143153

144154
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.
145155
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`.
146156

147-
## Create and configure a Github Repo with Github Actions
157+
### Add secrets to your GitHub repo for the build
148158

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.
150160

151-
```sh
152-
# Initialize a git repo
153-
git init
154-
# Add a remote
155-
git remote add origin https://github.com/<Your-Github-Username>/hello.git
156-
# Stage and commit your changes
157-
git add .
158-
git commit -m "Initial Commit"
159-
```
160-
161-
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.
162-
163-
![secrets](/images/2020-10-08-openfaas-functions-with-github-actions/secrets.jpg)
161+
![secrets](/images/2020-10-github-actions/secrets.jpg)
164162

165-
## Push the repo to Github to trigger Github Actions
163+
Now trigger a build by editing one of the files and running `git push`.
166164

167-
```sh
168-
git push -u origin master
169-
```
170-
171-
## Check the status of your Github Actions build
165+
### Check the status of your GitHub Actions build
172166

173167
Under the `Actions` tab we can check the status of the workflow
174168

175-
![workflow](/images/2020-10-08-openfaas-functions-with-github-actions/workflow.jpg)
169+
![workflow](/images/2020-10-github-actions/workflow.jpg)
176170

177171
Since we have successfully built and deployed our functions let's invoke it with curl.
178172

179-
## Invoke the function with curl
173+
### Invoke the function with curl
180174

181175
```sh
182176
export OPENFAAS_GATEWAY=<The OPENFAAS_GATEWAY of your OpenFaaS deployment>
183177
curl http://$OPENFAAS_GATEWAY/function/hello
184178
```
185179

186-
![curl](/images/2020-10-08-openfaas-functions-with-github-actions/curl.jpg)
180+
![curl](/images/2020-10-github-actions/curl.jpg)
187181

188182
## Take it further
189183

190184
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.
191185

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.
193187

194188
```yaml
195189
-
@@ -216,12 +210,16 @@ Just add a step to setup `QEMU` and pass a `platforms` key in the build step, ma
216210
tags: utsavanand2/hello:latest
217211
```
218212

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
220216

217+
Have you got questions, comments, or suggestions? Join the community on [Slack](https://slack.openfaas.io).
221218

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.
222220

223-
## Acknowledgements
221+
### Acknowledgements
224222

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.
226224

227225
Thanks to [Dan Burton](https://unsplash.com/@single_lens_reflex) for the background picture.

0 commit comments

Comments
 (0)