Skip to content
This repository was archived by the owner on Mar 13, 2024. It is now read-only.

Commit 8d283aa

Browse files
AlexanderWells-diamondgilesknap
authored andcommitted
Improve container build workflow
This will test the container before it is pushed to GHCR
1 parent 64d2a03 commit 8d283aa

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

.github/workflows/code.yml

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ jobs:
115115
contents: read
116116
packages: write
117117

118+
env:
119+
TEST_TAG: "testing"
120+
118121
steps:
119122
- name: Checkout
120123
uses: actions/checkout@v3
@@ -136,42 +139,66 @@ jobs:
136139
username: ${{ github.actor }}
137140
password: ${{ secrets.GITHUB_TOKEN }}
138141

139-
- name: Docker meta
142+
- name: Set up Docker Buildx
143+
id: buildx
144+
uses: docker/setup-buildx-action@v2
145+
146+
- name: Build and export to Docker local cache
147+
uses: docker/build-push-action@v4
148+
with:
149+
# Note build-args, context, file, and target must all match between this
150+
# step and the later build-push-action, otherwise the second build-push-action
151+
# will attempt to build the image again
152+
build-args: |
153+
PIP_OPTIONS=-r lockfiles/requirements.txt dist/*.whl
154+
context: artifacts/
155+
file: ./Dockerfile
156+
target: runtime
157+
load: true
158+
tags: ${{ env.TEST_TAG }}
159+
# If you have a long docker build (2+ minutes), uncomment the
160+
# following to turn on caching. For short build times this
161+
# makes it a little slower
162+
#cache-from: type=gha
163+
#cache-to: type=gha,mode=max
164+
165+
- name: Test cli works in cached runtime image
166+
run: docker run docker.io/library/${{ env.TEST_TAG }} --version
167+
168+
- name: Create tags for publishing image
140169
id: meta
141170
uses: docker/metadata-action@v4
142171
with:
143172
images: ${{ env.IMAGE_REPOSITORY }}
144173
tags: |
145174
type=ref,event=tag
146-
type=raw,value=latest
175+
type=raw,value=latest, enable=${{ github.ref_type == 'tag' }}
176+
# type=edge,branch=main
177+
# Add line above to generate image for every commit to given branch,
178+
# and uncomment the end of if clause in next step
147179

148-
- name: Set up Docker Buildx
149-
id: buildx
150-
uses: docker/setup-buildx-action@v2
151-
152-
- name: Build runtime image
180+
- name: Push cached image to container registry
181+
if: github.ref_type == 'tag' # || github.ref_name == 'main'
153182
uses: docker/build-push-action@v3
183+
# This does not build the image again, it will find the image in the
184+
# Docker cache and publish it
154185
with:
186+
# Note build-args, context, file, and target must all match between this
187+
# step and the previous build-push-action, otherwise this step will
188+
# attempt to build the image again
155189
build-args: |
156190
PIP_OPTIONS=-r lockfiles/requirements.txt dist/*.whl
157-
push: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags') }}
158-
load: ${{ ! (github.event_name == 'push' && startsWith(github.ref, 'refs/tags')) }}
159-
tags: ${{ steps.meta.outputs.tags }}
160-
labels: ${{ steps.meta.outputs.labels }}
161191
context: artifacts/
162192
file: ./Dockerfile
163-
# If you have a long docker build, uncomment the following to turn on caching
164-
# For short build times this makes it a little slower
165-
#cache-from: type=gha
166-
#cache-to: type=gha,mode=max
167-
168-
- name: Test cli works in runtime image
169-
run: docker run ${{ env.IMAGE_REPOSITORY }} --version
193+
target: runtime
194+
push: true
195+
tags: ${{ steps.meta.outputs.tags }}
196+
labels: ${{ steps.meta.outputs.labels }}
170197

171198
release:
172199
# upload to PyPI and make a release on every tag
173200
needs: [lint, dist, test]
174-
if: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags') }}
201+
if: ${{ github.event_name == 'push' && github.ref_type == 'tag' }}
175202
runs-on: ubuntu-latest
176203
env:
177204
HAS_PYPI_TOKEN: ${{ secrets.PYPI_TOKEN != '' }}

0 commit comments

Comments
 (0)