|
| 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