Skip to content

Commit bbc111c

Browse files
authored
Add composite action for Docker builds (#47)
1 parent 1228f96 commit bbc111c

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

docker-build/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# docker-build
2+
3+
Build a MozCloud service image
4+
5+
## Inputs
6+
7+
| Name | Required | Description |
8+
| --------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
9+
| `image_name` | Yes | Name to give to the built image |
10+
| `gar_location` | Yes | GCP region where GAR is located (default: `us`) |
11+
| `gar_name` | Yes | Name of the GAR repository |
12+
| `project_id` | Yes | GCP project ID |
13+
| `image_build_context` | Yes | Path to the Docker build context (default: `./`) |
14+
| `image_tag_metadata` | No | Metadata to append to the image tag.<br><br>For example, for a workflow triggred by a git tag `v1.2.3` and an `image_tag_metadata` value `dev`, the final image tag will be `v1.2.3--dev` |
15+
| `should_tag_ghcr` | No | Whether or not to generate image tags for Github Container Registry (default: `false`) |
16+
| `should_tag_latest` | No | Whether or not to tag the image(s) as `latest` (default: `false`) |
17+
18+
## Outputs
19+
20+
| Name | Description |
21+
| ------------ | -------------------------------------------- |
22+
| `image_tags` | The list of generated Docker images and tags |
23+
24+
## Example
25+
26+
```yaml
27+
- uses: mozilla/deploy-actions/docker-build@v4
28+
with:
29+
image_name: my-service
30+
gar_name: tenant-prod
31+
project_id: moz-fx-tenant-prod
32+
image_tag_metadata: dev
33+
should_tag_ghcr: true
34+
```

docker-build/action.yml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: docker-build
2+
description: Build a Mozcloud service image
3+
inputs:
4+
image_name:
5+
description: Name to give to the built image
6+
required: true
7+
gar_location:
8+
description: GCP region where GAR is located
9+
required: true
10+
default: us
11+
gar_name:
12+
description: GAR Name
13+
required: true
14+
project_id:
15+
description: GCP project id
16+
required: true
17+
image_build_context:
18+
description: Path to image context for `docker build`
19+
required: true
20+
default: ./
21+
image_tag_metadata:
22+
description: Optional metadata to append to image tag (e.g. for a tagged commit `v1.2.3` and metadata `dev`, the final image tag will be `v1.2.3--dev`)
23+
required: false
24+
should_tag_ghcr:
25+
description: Whether to tag images to be pushed to ghcr.io.
26+
required: false
27+
default: "false"
28+
should_tag_latest:
29+
description: Whether to tag images as `latest`.
30+
required: false
31+
default: "false"
32+
33+
outputs:
34+
image_tags:
35+
description: "Generated Docker image tags"
36+
value: ${{ steps.meta.outputs.tags }}
37+
38+
runs:
39+
using: composite
40+
steps:
41+
- uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 #v3.10.0
42+
- name: Generate MozCloud Tag
43+
id: mozcloud-tag
44+
shell: bash
45+
env:
46+
REF_TYPE: ${{ github.ref_type }}
47+
REF_NAME: ${{ github.ref_name }}
48+
IMAGE_TAG_METADATA: ${{ input.image_tag_metadata }}
49+
run: |
50+
if [[ "${REF_TYPE}" == "tag" ]]; then
51+
tag="${REF_NAME}"
52+
else
53+
tag="$(git rev-parse --short=10 HEAD)"
54+
fi
55+
# append metadata if present
56+
if [[ -n "$metadata" ]]; then
57+
tag="${tag}--${IMAGE_TAG_METADATA}"
58+
fi
59+
echo "Setting IMAGE_TAG=${tag} as output"
60+
echo "IMAGE_TAG=${tag}" >> "$GITHUB_OUTPUT"
61+
- name: Docker meta
62+
id: meta
63+
uses: docker/metadata-action@902fa8ec7d6ecbf8d84d538b9b233a880e428804 #v5.7.0
64+
with:
65+
images: |
66+
${{ inputs.gar_location }}-docker.pkg.dev/${{ inputs.project_id }}/${{ inputs.gar_name }}/${{ inputs.image_name }}
67+
${{ (inputs.should_tag_ghcr == 'true' && format('ghcr.io/{0}/{1}', github.repository, inputs.image_name)) || '' }}
68+
tags: |
69+
type=raw,value=${{ steps.mozcloud-tag.outputs.IMAGE_TAG }}
70+
type=raw,value=${{ (inputs.should_tag_latest == 'true' && latest) || ''}}
71+
- name: Build image
72+
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 #v6.18.0
73+
env:
74+
IMAGE_BUILD_CONTEXT: ${{ inputs.image_build_context }}
75+
with:
76+
context: ${IMAGE_BUILD_CONTEXT}
77+
tags: ${{ steps.meta.outputs.tags }}
78+
labels: ${{ steps.meta.outputs.labels }}
79+
annotations: ${{ steps.meta.outputs.annotations }}
80+
load: true
81+
push: false
82+
cache-from: type=gha
83+
cache-to: type=gha,mode=max

0 commit comments

Comments
 (0)