|
| 1 | +# Docker Build and Push Reusable Workflow |
| 2 | + |
| 3 | +This reusable GitHub Actions workflow encapsulates an end-to-end process for building and pushing a |
| 4 | +container image for a MozCloud service. |
| 5 | + |
| 6 | +The inputs of this workflow are mostly used to configure [`docker-build`](../docker-build/README.md) |
| 7 | +and [`docker-push`](../docker-push/README.md). See their documentation for more details on some of |
| 8 | +these inputs. |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +- Runs a `prebuild_script` to prepare the build environment (e.g. generate `version.json`) |
| 13 | +- Builds and tags the Docker image using [`docker-build`](../docker-build/README.md) |
| 14 | +- Provides an optional hook to run a `postbuild_script` after building (e.g. for running tests) |
| 15 | +- Pushes the image to GCP Artifact Registry (and optionally GitHub Container Registry) using [docker-push](../docker-push/README.md) |
| 16 | + |
| 17 | + |
| 18 | +## Inputs |
| 19 | + |
| 20 | +| Name | Required | Type | Description | |
| 21 | +| --------------------------------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------- | |
| 22 | +| `image_name` | true | string | The name of the Docker image to build. | |
| 23 | +| `gar_name` | true | string | Name of the GAR repository. (typically `<tenant>-prod`) | |
| 24 | +| `project_id` | true | string | GCP project ID where the Artifact Registry is located. (typically `moz-fx-<tenant>-prod`) | |
| 25 | +| `workload_identity_pool_project_number` | false | string | GCP workload identity pool project number. (default: `${{ vars.GCPV2_WORKLOAD_IDENTITY_POOL_PROJECT_NUMBER }}`) | |
| 26 | +| `prebuild_script` | false | string | Shell script (either inline or path to script) to run before building the image. (default: *see below)* | |
| 27 | +| `postbuild_script` | false | string | Shell script (either inline or path to script) to run after building the image. | |
| 28 | +| `image_build_context` | false | string | Build context path. (default: `"./"`) | |
| 29 | +| `image_tag_metadata` | false | string | Additional metadata for image tagging. | |
| 30 | +| `should_tag_ghcr` | false | boolean | Whether to also tag and push the image to GHCR. (default: `false`) | |
| 31 | +| `should_tag_latest` | false | boolean | Whether to tag the image as `latest`. (default: `false`) | |
| 32 | +| `gar_location` | false | string | Artifact Registry location. (default: `"us"`) | |
| 33 | +| `service_account_name` | false | string | GCP service account for pushing to registry. (default: `"artifact-writer"`) | |
| 34 | + |
| 35 | +### Default `prebuild_script` |
| 36 | + |
| 37 | +```bash |
| 38 | +IMAGE_BUILD_CONTEXT_ABSPATH=$(realpath "$IMAGE_BUILD_CONTEXT") |
| 39 | + |
| 40 | +printf '{"commit":"%s","version":"%s","source":"%s","build":"%s"}\n' \ |
| 41 | +"$GITHUB_SHA" \ |
| 42 | +"$GITHUB_REF_NAME" \ |
| 43 | +"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" \ |
| 44 | +"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" > "$IMAGE_BUILD_CONTEXT_ABSPATH/version.json" |
| 45 | +``` |
| 46 | + |
| 47 | +This script generates a `version.json` file with commit, version, and build metadata. |
| 48 | + |
| 49 | +## Permissions |
| 50 | + |
| 51 | +- `contents: read` (required) |
| 52 | +- `id-token: write` (required: for GCP authentication) |
| 53 | +- `packages: write` (optional: for GHCR publishing) |
| 54 | + |
| 55 | +## Usage |
| 56 | + |
| 57 | +### Minimal configuration |
| 58 | +```yaml |
| 59 | +on: |
| 60 | + push: |
| 61 | + branches: |
| 62 | + - main |
| 63 | + tags: |
| 64 | + - v20[0-9][0-9].[01][0-9].[0-3][0-9] # e.g. v2023.12.04 |
| 65 | + - v20[0-9][0-9].[01][0-9].[0-3][0-9]-[0-9] # e.g. v2023.12.04-2 |
| 66 | + |
| 67 | +jobs: |
| 68 | + build-and-push: |
| 69 | + secrets: inherit |
| 70 | + permissions: |
| 71 | + contents: read |
| 72 | + id-token: write |
| 73 | + uses: mozilla-it/deploy-actions/.github/workflows/docker-build-and-push.yml@main |
| 74 | + with: |
| 75 | + image_name: your-service |
| 76 | + gar_name: your-tenant-prod |
| 77 | + project_id: moz-fx-you-tenant-prod |
| 78 | +``` |
| 79 | +
|
| 80 | +### Push to GHCR |
| 81 | +```yaml |
| 82 | +on: |
| 83 | + push: |
| 84 | + branches: |
| 85 | + - main |
| 86 | + tags: |
| 87 | + - v20[0-9][0-9].[01][0-9].[0-3][0-9] # e.g. v2023.12.04 |
| 88 | + - v20[0-9][0-9].[01][0-9].[0-3][0-9]-[0-9] # e.g. v2023.12.04-2 |
| 89 | + |
| 90 | +jobs: |
| 91 | + build: |
| 92 | + secrets: inherit |
| 93 | + permissions: |
| 94 | + contents: read |
| 95 | + id-token: write |
| 96 | + packages: write |
| 97 | + uses: mozilla-it/deploy-actions/.github/workflows/docker-build-and-push.yml@main |
| 98 | + with: |
| 99 | + image_name: your-service |
| 100 | + gar_name: your-tenant-prod |
| 101 | + project_id: moz-fx-you-tenant-prod |
| 102 | + should_tag_ghcr: true |
| 103 | +``` |
0 commit comments