|
1 | 1 | # GitHub Actions for building OpenFaaS Functions
|
2 | 2 |
|
3 |
| -You can build and / or deploy OpenFaaS functions using GitHub Actions. |
| 3 | +You can use GitHub Actions to build or publish container images for your OpenFaaS functions. As a final step, you may also wish to deploy the new images to the OpenFaaS gateway via the OpenFaaS CLI. |
| 4 | + |
| 5 | +If you'd like to deploy the function, check out a more comprehensive example of how to log in and deploy in [Serverless For Everyone Else](https://store.openfaas.com/l/serverless-for-everyone-else). |
| 6 | + |
| 7 | +## Publish multiple functions |
| 8 | + |
| 9 | +We will deploy alexellis' repository called [alexellis/autoscaling-functions](https://github.com/alexellis/autoscaling-functions). It contains multiple functions which can be deployed as a group. |
| 10 | + |
| 11 | +* The "Setup QEMU" and "Set up Docker Buildx" steps configure the builder to produce a multi-arch image. |
| 12 | +* The "OWNER" variable means this action can be run on any organisation without having to hard-code a username for GHCR. |
| 13 | +* Only the bcrypt function is being built with the `--filter` command added, remove it to build all functions in the stack.yml. |
| 14 | +* `--platforms linux/amd64,linux/arm64,linux/arm/v7` will build for regular Intel/AMD machines, 64-bit Arm and 32-bit Arm i.e. Raspberry Pi, most users can reduce this list to just "linux/amd64" for a speed improvement |
| 15 | + |
| 16 | +If you're using [actuated.dev](https://actuated.dev) for fast, secure self-hosted runners, then you should `runs-on:` to the amount of vCPU and RAM you want i.e. `runs-on: actuated-4cpu-16gb`. |
| 17 | + |
| 18 | +```yaml |
| 19 | +name: publish |
| 20 | + |
| 21 | +on: |
| 22 | + push: |
| 23 | + branches: |
| 24 | + - '*' |
| 25 | + pull_request: |
| 26 | + branches: |
| 27 | + - '*' |
| 28 | + |
| 29 | +permissions: |
| 30 | + actions: read |
| 31 | + checks: write |
| 32 | + contents: read |
| 33 | + packages: write |
| 34 | + |
| 35 | +jobs: |
| 36 | + publish: |
| 37 | + runs-on: ubuntu-latest |
| 38 | + steps: |
| 39 | + - uses: actions/checkout@master |
| 40 | + with: |
| 41 | + fetch-depth: 1 |
| 42 | + - name: Get faas-cli |
| 43 | + run: curl -sLSf https://cli.openfaas.com | sudo sh |
| 44 | + - name: Pull custom templates from stack.yml |
| 45 | + run: faas-cli template pull stack |
| 46 | + - name: Set up QEMU |
| 47 | + uses: docker/setup-qemu-action@v3 |
| 48 | + - name: Set up Docker Buildx |
| 49 | + uses: docker/setup-buildx-action@v3 |
| 50 | + - name: Get TAG |
| 51 | + id: get_tag |
| 52 | + run: echo ::set-output name=TAG::latest-dev |
| 53 | + - name: Get Repo Owner |
| 54 | + id: get_repo_owner |
| 55 | + run: > |
| 56 | + echo ::set-output name=repo_owner::$(echo ${{ github.repository_owner }} | |
| 57 | + tr '[:upper:]' '[:lower:]') |
| 58 | + - name: Docker Login |
| 59 | + run: > |
| 60 | + echo ${{secrets.GITHUB_TOKEN}} | |
| 61 | + docker login ghcr.io --username |
| 62 | + ${{ steps.get_repo_owner.outputs.repo_owner }} |
| 63 | + --password-stdin |
| 64 | + - name: Publish functions |
| 65 | + run: > |
| 66 | + OWNER="${{ steps.get_repo_owner.outputs.repo_owner }}" |
| 67 | + TAG="latest" |
| 68 | + faas-cli publish |
| 69 | + --extra-tag ${{ github.sha }} |
| 70 | + --build-arg GO111MODULE=on |
| 71 | + --platforms linux/amd64,linux/arm64,linux/arm/v7 |
| 72 | + --filter bcrypt |
| 73 | +``` |
| 74 | +
|
| 75 | +### Deploy functions from CI |
| 76 | +
|
| 77 | +To deploy functions, you can use the `faas-cli` in a `run` step. |
| 78 | + |
| 79 | +First run `faas-cli login` with the gateway's public URL, then run `faas-cli deploy`. |
| 80 | + |
| 81 | +If you do not have a public URL for your OpenFaaS gateway, or are running within a private VPC, there are a couple of options: |
| 82 | + |
| 83 | +1. Don't expose anything, but establish a private link as required - [Deploy to a private cluster from GitHub Actions without exposing it to the Internet](https://inlets.dev/blog/2021/12/06/private-deploy-from-github-actions.html) |
| 84 | +2. Keep the VPC private, but expose only the OpenFaaS gateway - [Expose your local OpenFaaS functions to the Internet](https://inlets.dev/blog/2020/10/15/openfaas-public-endpoints.html) |
| 85 | + |
| 86 | +By default, all functions in stack.yaml will be deployed, or you can add the `--filter` flag to a named function. |
| 87 | + |
| 88 | +If you are using OpenFaaS Standard, then you will need to create a password for the repository or organisation with the shared admin password for the gateway. |
| 89 | + |
| 90 | +If you're using IAM for OpenFaaS, then you can use Web Identity Federation, and a short-lived OIDC token from GitHub, see also: [GitHub Actions - Web Identity Federation](/openfaas-pro/iam/github-actions-federation/). This option is much more secure, and means that not only can the job be scoped to only what it needs to change - a single namespace, or a subset of objects, but it also means that no long-lived credentials are shared with the CI system. |
| 91 | + |
| 92 | +#### Dashboard integration |
| 93 | + |
| 94 | +There are various fields on the dashboard such as the Commit SHA, the repository owner and the repository URL. When metadata is populated via labels and annotations on your function, then these become links, and will show for your team. |
| 95 | + |
| 96 | +See the notes here for a full list of metadata options: [OpenFaaS Dashboard](/openfaas-pro/dashboard/) |
| 97 | + |
| 98 | +## Further reading |
4 | 99 |
|
5 | 100 | See our reference manual for OpenFaaS for recipes and examples:
|
6 | 101 |
|
|
0 commit comments