Skip to content

Commit 1390716

Browse files
committed
Add Bitbucket Pipelines CI/CD reference page
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <[email protected]>
1 parent 5a818e3 commit 1390716

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

docs/openfaas-pro/iam/bitbucket-federation.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ You can set custom audiences at the step level or globally using `options`. See
105105

106106
In this example, a pipeline deploys or updates functions in the `etl` namespace. The Policy grants deploy, and update permissions for any function within that namespace, and the Role locks it down to a specific repository and branch.
107107

108+
This example covers only the deploy step. Functions must be built and pushed to a registry before they can be deployed. See [CI/CD with Bitbucket Pipelines](/reference/cicd/bitbucket/) for an example on how to set up the build and push steps.
109+
108110
### Create a Policy
109111

110112
The `etl-deployer` policy grants permission to create or update any function within `etl`:

docs/reference/cicd/bitbucket.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# CI/CD with Bitbucket Pipelines
2+
3+
You can use Bitbucket Pipelines to build and 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+
## Build and deploy
6+
7+
This pipeline builds and pushes function images, then deploys them to the OpenFaaS gateway. The build and deploy are defined as separate steps so they can be tracked independently.
8+
9+
Pre-requisites:
10+
11+
* A container registry accessible from the pipeline (e.g. Docker Hub, [Bitbucket Packages](https://support.atlassian.com/bitbucket-cloud/docs/getting-started-with-packages/), AWS ECR, or another private registry).
12+
* The OpenFaaS gateway must be accessible from the Bitbucket pipeline runner.
13+
14+
Add the following repository variables under **Repository settings > Pipelines > Repository variables**:
15+
16+
| Variable | Description | Secured |
17+
|---|---|---|
18+
| `DOCKER_USERNAME` | Username for the container registry e.g. Docker Hub username | No |
19+
| `DOCKER_PASSWORD` | Password or access token for the container registry | Yes |
20+
| `OPENFAAS_URL` | URL of the OpenFaaS gateway e.g. `https://gw.example.com` | No |
21+
| `OPENFAAS_PASSWORD` | Password for the OpenFaaS gateway | Yes |
22+
23+
!!! info "OpenFaaS for Enterprises"
24+
If you are using OpenFaaS for Enterprises, we recommend using [Web Identity Federation](/openfaas-pro/iam/bitbucket-federation/) using the OIDC token provided by Bitbucket Pipelines instead of sharing the admin password with your CI system. This avoids long-lived credentials and lets you scope access to specific namespaces, functions and actions.
25+
26+
See: [Variables and secrets](https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/)
27+
28+
### Create a function
29+
30+
If you don't already have a function, you can scaffold one with the `faas-cli`. The following example creates a Python function, but any supported language template can be used:
31+
32+
```bash
33+
export OPENFAAS_PREFIX="docker.io/username"
34+
35+
faas-cli new --lang python3-http import-csv
36+
```
37+
38+
This generates a `stack.yaml` and a handler directory. You can edit the image field in your `stack.yaml` to use [environment variable substitution](/reference/yaml/#yaml-environment-variable-substitution) so the registry and namespace aren't hard-coded.
39+
40+
The `configuration.templates` section declares the template repositories your functions depend on. In a CI pipeline, this means `faas-cli template pull stack` will automatically fetch the correct templates function templates.
41+
42+
```yaml
43+
version: 1.0
44+
provider:
45+
name: openfaas
46+
gateway: http://127.0.0.1:8080
47+
functions:
48+
import-csv:
49+
lang: python3-http
50+
handler: ./import-csv
51+
image: ${REGISTRY:-docker.io}/${NAMESPACE:-username}/import-csv:latest
52+
configuration:
53+
templates:
54+
- name: python3-http
55+
source: https://github.com/openfaas/python-flask-template
56+
```
57+
58+
### Create the pipeline
59+
60+
Create a `bitbucket-pipelines.yml` file in the root of your repository:
61+
62+
```yaml
63+
image: atlassian/default-image:3
64+
65+
pipelines:
66+
branches:
67+
main:
68+
- step:
69+
name: Build and push
70+
services:
71+
- docker
72+
script:
73+
- curl -sLS https://cli.openfaas.com | sh
74+
75+
# Login to the container registry
76+
- >-
77+
echo "$DOCKER_PASSWORD" |
78+
docker login --username "$DOCKER_USERNAME" --password-stdin
79+
80+
# Build and push function images
81+
- faas-cli template pull stack
82+
- faas-cli build --tag=sha
83+
- faas-cli push --tag=sha
84+
85+
- step:
86+
name: Deploy
87+
script:
88+
- curl -sLS https://cli.openfaas.com | sh
89+
90+
# Login to the OpenFaaS gateway
91+
- >-
92+
echo "$OPENFAAS_PASSWORD" |
93+
faas-cli login --username admin --password-stdin
94+
95+
# Deploy functions
96+
- faas-cli template pull stack
97+
- faas-cli deploy --tag=sha
98+
```
99+
100+
The `--tag=sha` flag appends the Git commit SHA to the image tag so that each build produces a unique, traceable image. Both steps must use the same flag. Alternatives like `--tag=branch` and `--tag=describe` are also available, see [Image tagging](/cli/tags/). You can also template the image field in your stack.yaml using [environment variable substitution](/reference/yaml/#yaml-environment-variable-substitution) to tag images in whatever format you need.
101+
102+
Only the build step requires the Docker service since it builds container images. The deploy step only needs the `faas-cli` to communicate with the gateway's REST API.
103+
104+
If you are using a private registry, the OpenFaaS cluster must be able to pull images from it. See [Configure OpenFaaS to pull from a private registry](/reference/private-registries/).
105+
106+
## Optional: validate functions before merge
107+
108+
You can optionally add a build step that runs on pull requests to validate that functions build correctly before merging. This only builds the images without pushing them to a registry.
109+
110+
```yaml
111+
image: atlassian/default-image:3
112+
113+
pipelines:
114+
pull-requests:
115+
'**':
116+
- step:
117+
name: Build functions
118+
services:
119+
- docker
120+
script:
121+
- curl -sLS https://cli.openfaas.com | sh
122+
- faas-cli template pull stack
123+
- faas-cli build
124+
```
125+
126+
When multiple functions are available in the stack.yaml file you can add `--parallel` to speed up the build by building multiple functions at once.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ nav:
203203
- GitHub Actions: ./reference/cicd/github-actions.md
204204
- Function Builder API: ./openfaas-pro/builder.md
205205
- GitLab: ./reference/cicd/gitlab.md
206+
- Bitbucket Pipelines: ./reference/cicd/bitbucket.md
206207
- Jenkins: ./reference/cicd/jenkins.md
207208
- Namespaces: ./reference/namespaces.md
208209
- Authentication: ./reference/authentication.md

0 commit comments

Comments
 (0)