|
| 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. |
| 39 | + |
| 40 | +Update `import-csv/handler.py` so it echos the request body: |
| 41 | + |
| 42 | +```python |
| 43 | +def handle(event, context): |
| 44 | + return { |
| 45 | + "statusCode": 200, |
| 46 | + "body": event.body |
| 47 | + } |
| 48 | +``` |
| 49 | + |
| 50 | +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. |
| 51 | + |
| 52 | +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. |
| 53 | + |
| 54 | +```yaml |
| 55 | +version: 1.0 |
| 56 | +provider: |
| 57 | + name: openfaas |
| 58 | + gateway: http://127.0.0.1:8080 |
| 59 | +functions: |
| 60 | + import-csv: |
| 61 | + lang: python3-http |
| 62 | + handler: ./import-csv |
| 63 | + image: ${REGISTRY:-docker.io}/${NAMESPACE:-username}/import-csv:latest |
| 64 | +configuration: |
| 65 | + templates: |
| 66 | + - name: python3-http |
| 67 | + source: https://github.com/openfaas/python-flask-template |
| 68 | +``` |
| 69 | +
|
| 70 | +### Create the pipeline |
| 71 | +
|
| 72 | +Create a `bitbucket-pipelines.yml` file in the root of your repository: |
| 73 | + |
| 74 | +```yaml |
| 75 | +image: atlassian/default-image:3 |
| 76 | +
|
| 77 | +pipelines: |
| 78 | + branches: |
| 79 | + main: |
| 80 | + - step: |
| 81 | + name: Build and push |
| 82 | + services: |
| 83 | + - docker |
| 84 | + script: |
| 85 | + - curl -sLS https://cli.openfaas.com | sh |
| 86 | +
|
| 87 | + # Login to the container registry |
| 88 | + - >- |
| 89 | + echo "$DOCKER_PASSWORD" | |
| 90 | + docker login --username "$DOCKER_USERNAME" --password-stdin |
| 91 | +
|
| 92 | + # Build and push function images |
| 93 | + - faas-cli template pull stack |
| 94 | + - faas-cli build --tag=sha |
| 95 | + - faas-cli push --tag=sha |
| 96 | +
|
| 97 | + - step: |
| 98 | + name: Deploy |
| 99 | + script: |
| 100 | + - curl -sLS https://cli.openfaas.com | sh |
| 101 | +
|
| 102 | + # Login to the OpenFaaS gateway |
| 103 | + - >- |
| 104 | + echo "$OPENFAAS_PASSWORD" | |
| 105 | + faas-cli login --username admin --password-stdin |
| 106 | +
|
| 107 | + # Deploy functions |
| 108 | + - faas-cli template pull stack |
| 109 | + - faas-cli deploy --tag=sha |
| 110 | +``` |
| 111 | + |
| 112 | +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. |
| 113 | + |
| 114 | +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. |
| 115 | + |
| 116 | +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/). |
| 117 | + |
| 118 | +## Optional: validate functions before merge |
| 119 | + |
| 120 | +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. |
| 121 | + |
| 122 | +```yaml |
| 123 | +image: atlassian/default-image:3 |
| 124 | +
|
| 125 | +pipelines: |
| 126 | + pull-requests: |
| 127 | + '**': |
| 128 | + - step: |
| 129 | + name: Build functions |
| 130 | + services: |
| 131 | + - docker |
| 132 | + script: |
| 133 | + - curl -sLS https://cli.openfaas.com | sh |
| 134 | + - faas-cli template pull stack |
| 135 | + - faas-cli build |
| 136 | +``` |
| 137 | + |
| 138 | +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. |
0 commit comments