Skip to content

Commit 8e3aed9

Browse files
authored
Merge pull request #7 from hathitrust/action-updates
Added several feature enhancements: - Default multi-platform support for amd64 and arm64 docker images. - Updated versions for actions/checkout, actions/github-script, and docker/login-action - Added img_tag field to separate github tags and container image tags. - Added a core output for the id:latest_push. This way, if it's not the latest, we default to unstable while preserving the ability to add tags to the docker image as needed. - Moved logic from the template build.yml file to this shared action.
2 parents a5b473b + e28420e commit 8e3aed9

1 file changed

Lines changed: 99 additions & 64 deletions

File tree

build/action.yml

Lines changed: 99 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
---
21
name: "Build Docker Image"
32
description: "Build Docker image for arbitrary revision/branch/tag and push to registry"
43

54
inputs:
65
tag:
76
description: Revision, tag, or branch to build
87
required: true
8+
default: ${{ github.event.repository.default_branch }}
9+
img_tag:
10+
description: Tag applied to the container image.
911
image:
1012
description: >-
1113
Image to build, e.g. ghcr.io/organization/image.
@@ -16,13 +18,11 @@ inputs:
1618
default: ghcr.io
1719
registry_username:
1820
description: >-
19-
Username for logging in to registry. For GHCR (and by default),
20-
set to github.actor.
21+
Username for logging in to registry. For GHCR (and by default), set to github.actor.
2122
default: ${{ github.actor }}
2223
registry_token:
2324
description: >-
24-
Token for logging in to registry. For GHCR (and by default),
25-
set to github.token.
25+
Token for logging in to registry. For GHCR (and by default), set to github.token.
2626
default: ${{ github.token }}
2727
build-args:
2828
description: >-
@@ -38,72 +38,107 @@ inputs:
3838
default: false
3939
submodules:
4040
description: >-
41-
Whether to checkout submodules (as for actions@v3) and thence build them
42-
into the image. Set to false by default as for checkout. If you use submodules,
43-
you probably want to set this.
41+
Whether to checkout submodules (as for actions@v3) and thence build them into the image. Set to false by default as for checkout. If you use submodules, you probably want to set this.
4442
default: false
43+
platforms:
44+
description: >-
45+
Build images for cross platform support.
46+
default: "linux/amd64,linux/arm64"
47+
rebuild:
48+
description: Rebuild the image manually?
49+
default: false
50+
gh_event:
51+
description: The name of the event that triggered the parent workflow.
4552

4653
runs:
4754
using: composite
4855
steps:
49-
- name: Clone latest repository
50-
uses: actions/checkout@v3
51-
with:
52-
ref: ${{ inputs.tag }}
53-
submodules: ${{ inputs.submodules }}
56+
- name: Set Inputs & Outputs
57+
id: set_inputs
58+
shell: bash
59+
run: |
60+
if [ "${{ github.event_name }}" == "workflow_run" ]; then
61+
echo "ref=${{ github.sha }}" >> $GITHUB_OUTPUT
62+
echo "push_latest=true" >> $GITHUB_OUTPUT
63+
else
64+
# workflow_dispatch or other trigger
65+
echo "ref=${{ inputs.tag }}" >> $GITHUB_OUTPUT
66+
echo "push_latest=${{ inputs.push_latest }}" >> $GITHUB_OUTPUT
67+
echo "img_tag=${{ inputs.img_tag }}" >> $GITHUB_OUTPUT
68+
fi
5469
55-
- name: Find commit for tag
56-
id: tag_check
57-
uses: hathitrust/github_actions/validate-tag@v1
58-
with:
59-
tag: ${{ inputs.tag }}
70+
- name: Clone latest repository
71+
uses: actions/checkout@v4
72+
with:
73+
ref: ${{ steps.set_inputs.outputs.ref }}
74+
submodules: ${{ inputs.submodules }}
6075

61-
- name: Log into container registry
62-
uses: docker/login-action@v2
63-
with:
64-
registry: ${{ inputs.registry }}
65-
username: ${{ inputs.registry_username }}
66-
password: ${{ inputs.registry_token }}
76+
- name: Find commit for tag
77+
id: tag_check
78+
uses: hathitrust/github_actions/validate-tag@v1
79+
with:
80+
tag: ${{ steps.set_inputs.outputs.ref }}
6781

68-
- name: Check if revision exists in container registry
69-
id: image_check
70-
shell: bash
71-
run: |
72-
echo Checking whether Docker image "$IMAGE":"$TAG" exists
73-
if docker manifest inspect "$IMAGE":"$TAG" > /dev/null; then
74-
echo 'image_exists=true' >> $GITHUB_OUTPUT
75-
echo "image exists!"
76-
else
77-
echo "image doesn't exist; Starting to Build and push image"
78-
fi
79-
env:
80-
IMAGE: ${{ inputs.image }}
81-
TAG: ${{ steps.tag_check.outputs.tag }}
82+
- name: Log into container registry
83+
uses: docker/login-action@v3
84+
with:
85+
registry: ${{ inputs.registry }}
86+
username: ${{ inputs.registry_username }}
87+
password: ${{ inputs.registry_token }}
88+
89+
- name: Check if revision exists in container registry
90+
id: image_check
91+
shell: bash
92+
run: |
93+
echo Checking whether Docker image "$IMAGE":"$TAG" exists
94+
if docker manifest inspect "$IMAGE":"$TAG" > /dev/null; then
95+
echo 'image_exists=true' >> $GITHUB_OUTPUT
96+
echo "image exists!"
97+
else
98+
echo "image doesn't exist; Starting to Build and push image"
99+
fi
100+
env:
101+
IMAGE: ${{ inputs.image }}
102+
TAG: ${{ steps.tag_check.outputs.tag }}
103+
104+
- name: Check whether to push latest tag
105+
if: ${{ steps.image_check.outputs.image_exists != 'true' || inputs.rebuild }}
106+
id: latest_push
107+
uses: actions/github-script@v7
108+
env:
109+
INPUT_PUSH_LATEST: ${{ steps.set_inputs.outputs.push_latest }}
110+
INPUT_IMAGE: ${{ inputs.image }}
111+
with:
112+
script: |
113+
if ( core.getInput('push_latest') == 'true' ) {
114+
core.setOutput('latest_tag',core.getInput('image') + ':latest')
115+
} else {
116+
core.setOutput('latest_tag',core.getInput('image') + ':unstable')
117+
}
118+
119+
- name: Set up QEMU
120+
if: ${{ steps.image_check.outputs.image_exists != 'true' || inputs.rebuild }}
121+
# QEMU emulation for the Arm portion of our multi-platform Docker image build. Intel Machines
122+
uses: docker/setup-qemu-action@v3
82123

83-
- name: Check whether to push latest tag
84-
if: ${{ steps.image_check.outputs.image_exists != 'true' }}
85-
id: latest_push
86-
uses: actions/github-script@v6
87-
env:
88-
INPUT_PUSH_LATEST: ${{ inputs.push_latest }}
89-
INPUT_IMAGE: ${{ inputs.image }}
90-
with:
91-
script: |
92-
if ( core.getInput('push_latest') == 'true' ) {
93-
core.setOutput('latest_tag',core.getInput('image') + ':latest')
94-
} else {
95-
core.setOutput('latest_tag','')
96-
}
124+
- name: Set up Docker Buildx
125+
if: ${{ steps.image_check.outputs.image_exists != 'true' || inputs.rebuild }}
126+
# Required for multi-platform Docker image builds in GitHub Actions that use docker/build-push-action.
127+
uses: docker/setup-buildx-action@v3
97128

98-
- name: Build image and push to GHCR
99-
if: ${{ steps.image_check.outputs.image_exists != 'true' }}
100-
uses: docker/build-push-action@v3
101-
with:
102-
context: .
103-
push: true
104-
tags: |
105-
${{ inputs.image }}:${{ inputs.tag }}
106-
${{ inputs.image }}:${{ steps.tag_check.outputs.tag }}
107-
${{ steps.latest_push.outputs.latest_tag }}
108-
file: ${{ inputs.dockerfile }}
109-
build-args: ${{ inputs.build-args }}
129+
- name: Build image and push to GHCR
130+
if: ${{ steps.image_check.outputs.image_exists != 'true' || inputs.rebuild }}
131+
uses: docker/build-push-action@v5
132+
with:
133+
context: .
134+
push: true
135+
tags: |
136+
${{ inputs.image }}:${{ steps.set_inputs.outputs.img_tag && inputs.img_tag || steps.tag_check.outputs.tag }}
137+
${{ inputs.image }}:${{ steps.tag_check.outputs.tag }}
138+
${{ steps.latest_push.outputs.latest_tag }}
139+
file: ${{ inputs.dockerfile }}
140+
build-args: ${{ inputs.build-args }}
141+
platforms: ${{ inputs.platforms}}
142+
cache-from: type=gha
143+
cache-to: type=gha,mode=max
144+
provenance: false

0 commit comments

Comments
 (0)