Skip to content

Commit

Permalink
refactor: match GHA to Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
adamblake committed Dec 18, 2024
1 parent 1f146d7 commit 786be39
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 100 deletions.
123 changes: 37 additions & 86 deletions .github/workflows/build-test-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ name: Build, Test, and Publish an Image
on:
workflow_call:
inputs:
runs-on:
description: The platform to run the action on
title:
description: The title of the image
type: string
required: false
default: ubuntu-latest
free-disk-space:
description: Whether to free disk space before building
type: boolean
required: false
default: false
required: true
description:
description: The description of the image
type: string
required: true
image:
description: The image to build
description: The name image to build (also corresponds to a Pixi environment)
type: string
required: true
target:
Expand All @@ -26,35 +24,12 @@ on:
description: Build arguments to pass to the image
type: string
required: false
publish:
description: Whether to publish the image to remote registries
type: boolean
required: false
default: false
tags:
description: Tags to add to the image
type: string
required: false
labels:
description: Labels to add to the image
type: string
required: false
no-cache:
description: Whether to disable the build cache
type: boolean
required: false
default: false
cache-from:
description: Docker images that might be able to contribute cached layers
type: string
required: false
registry-port:
description: The port to use for the local registry
type: number
required: false
default: 5003
skip-tests:
description: Whether to skip the test steps
free-disk-space:
description: Whether to free disk space before building
type: boolean
required: false
default: false
Expand All @@ -75,7 +50,7 @@ on:

jobs:
docker:
runs-on: ${{ inputs.runs-on }}
runs-on: ubuntu-latest

outputs:
image: ${{ inputs.image }}
Expand All @@ -84,7 +59,6 @@ jobs:
cache-from: ${{ steps.generate-cache-from.outputs.cache_from }}

steps:
# https://github.com/jlumbroso/free-disk-space
- name: Free Disk Space (Ubuntu)
if: inputs.free-disk-space
uses: jlumbroso/free-disk-space@main
Expand All @@ -94,113 +68,94 @@ jobs:
large-packages: ${{ inputs.free-disk-space }}
swap-storage: ${{ inputs.free-disk-space }}

# https://github.com/actions/checkout
- name: Checkout
uses: actions/checkout@v4

# https://github.com/docker/setup-qemu-action
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

# Start Docker Registry Manually
- name: Start Local Docker Registry
run: |
if [ "$(docker ps -aq -f name=actions-registry)" ]; then
docker rm -f actions-registry
fi
docker run -d --name actions-registry -p ${{ inputs.registry-port }}:5000 registry:2
# https://github.com/prefix-dev/setup-pixi
- uses: prefix-dev/[email protected]

- name: Update Conda packages
shell: bash
run: pixi update

# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
# network=host driver-opt needed to push to local registry
driver-opts: network=host

# https://github.com/docker/login-action
- name: Login to GitHub Container Registry
if: inputs.publish
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# https://github.com/docker/login-action
- name: Login to Docker Registry
if: inputs.publish
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}

# https://github.com/docker/build-push-action
- name: Build for linux/arm64
id: build-arm64
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
target: ${{ inputs.target }}
platforms: linux/arm64
build-args: ${{ inputs.build-args }}
secrets: "github_token=${{ secrets.GITHUB_TOKEN }}"
tags: localhost:${{ inputs.registry-port }}/${{ github.repository_owner }}/${{ inputs.image }}:arm64
tags: |
ghcr.io/${{ github.repository_owner }}/${{ inputs.image }}:arm64
cache-to: |
type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ inputs.image }}:arm64,mode=max
cache-from: |
type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ inputs.image }}
type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ inputs.image }}:arm64
${{ inputs.cache-from }}
cache-to: type=registry,ref=localhost:${{ inputs.registry-port }}/${{ github.repository_owner }}/${{ inputs.image }}:buildcache-arm64,mode=max
no-cache: ${{ inputs.no-cache }}
push: true
load: true

- name: Test for linux/arm64
if: inputs.skip-tests == false
shell: bash
run: |
IMAGE_NAME=$(echo ${{ steps.build-arm64.outputs.metadata }} | jq -r '.["image.name"]')
docker run --rm --platform=linux/arm64 \
--mount=type=bind,source="./tests/test-packages.sh",target=/tmp/test-packages.sh \
--mount=type=bind,source="./tests/packages.txt",target=/tmp/packages.txt \
--mount=type=bind,source="./tests/${{ inputs.image }}.sh",target=/tmp/test.sh \
localhost:${{ inputs.registry-port }}/${{ github.repository_owner }}/${{ inputs.image }}:arm64 \
${IMAGE_NAME} \
bash /tmp/test.sh
# https://github.com/docker/build-push-action
- name: Build for linux/amd64
id: build-amd64
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
target: ${{ inputs.target }}
platforms: linux/amd64
build-args: ${{ inputs.build-args }}
secrets: "github_token=${{ secrets.GITHUB_TOKEN }}"
tags: localhost:${{ inputs.registry-port }}/${{ github.repository_owner }}/${{ inputs.image }}:amd64
tags: |
ghcr.io/${{ github.repository_owner }}/${{ inputs.image }}:amd64
cache-to: |
type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ inputs.image }}:amd64,mode=max
cache-from: |
type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ inputs.image }}
type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ inputs.image }}:amd64
${{ inputs.cache-from }}
cache-to: type=registry,ref=localhost:${{ inputs.registry-port }}/${{ github.repository_owner }}/${{ inputs.image }}:buildcache-amd64,mode=max
no-cache: ${{ inputs.no-cache }}
push: true
load: true

- name: Test for linux/amd64
if: inputs.skip-tests == false
shell: bash
run: |
IMAGE_NAME=$(echo ${{ steps.build-amd64.outputs.metadata }} | jq -r '.["image.name"]')
docker run --rm --platform=linux/amd64 \
--mount=type=bind,source="./tests/test-packages.sh",target=/tmp/test-packages.sh \
--mount=type=bind,source="./tests/packages.txt",target=/tmp/packages.txt \
--mount=type=bind,source="./tests/${{ inputs.image }}.sh",target=/tmp/test.sh \
localhost:${{ inputs.registry-port }}/${{ github.repository_owner }}/${{ inputs.image }}:amd64 \
${IMAGE_NAME} \
bash /tmp/test.sh
# https://github.com/docker/metadata-action
- name: Image Metadata
id: meta
if: inputs.publish
uses: docker/metadata-action@v5
with:
images: |
Expand All @@ -210,27 +165,25 @@ jobs:
type=raw,value=latest,enable={{is_default_branch}}
type=schedule,pattern={{date 'YYYY-MM-DD'}}
type=sha
${{ inputs.tags }}
labels: |
org.opencontainers.image.title=${{ inputs.image }}
org.opencontainers.image.url=https://github.com/${{ github.repository_owner }}/docker-stacks/pkgs/container/${{ inputs.image }}
${{ inputs.labels }}
org.opencontainers.image.title=${{ inputs.title }}
org.opencontainers.image.description=${{ inputs.description }}
annotations: |
org.opencontainers.image.description=${{ inputs.description }}
- name: Generate cache-from entries
if: inputs.publish
id: generate-cache-from
shell: bash
run: |
{
echo 'cache_from<<EOF'
echo "$(echo '${{ steps.meta.outputs.tags }}' | sed 's/^/type=registry,ref=/g')"
echo EOF
echo "type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ inputs.image }}:amd64"
echo "type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ inputs.image }}:amd64"
} >> "$GITHUB_OUTPUT"
# https://github.com/docker/build-push-action
- name: Build and push multi-arch
id: build
if: inputs.publish
uses: docker/build-push-action@v6
with:
target: ${{ inputs.target }}
Expand All @@ -239,8 +192,6 @@ jobs:
secrets: "github_token=${{ secrets.GITHUB_TOKEN }}"
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: |
type=registry,ref=localhost:${{ inputs.registry-port }}/${{ github.repository_owner }}/${{ inputs.image }}:buildcache-amd64
type=registry,ref=localhost:${{ inputs.registry-port }}/${{ github.repository_owner }}/${{ inputs.image }}:buildcache-arm64
cache-to: type=inline
annotations: ${{ steps.meta.outputs.annotations }}
cache-from: ${{ steps.generate-cache-from.outputs.cache_from }}
push: true
21 changes: 7 additions & 14 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
name: Build, Test, and Publish Images
name: Publish Images

on:
workflow_dispatch:
Expand Down Expand Up @@ -33,14 +33,10 @@ jobs:
with:
image: foundation
target: base
no-cache: ${{ github.event_name == 'schedule' }}
labels: |
org.opencontainers.image.title=Foundational Image for CourseKata Notebook Images
org.opencontainers.image.description=System dependencies for CourseKata notebook images.
title: Foundational Image for CourseKata Notebook Images
description: System dependencies for CourseKata notebook images.
build-args: |
PIXI_ENV=default
publish: true
skip-tests: true
images:
needs: [foundation]
Expand Down Expand Up @@ -75,12 +71,9 @@ jobs:
secrets: inherit
with:
image: ${{ matrix.image }}
no-cache: ${{ github.event_name == 'schedule' }}
labels: |
org.opencontainers.image.title=${{ matrix.title }}
org.opencontainers.image.description=${{ matrix.description }}
build-args: |
PIXI_ENV=${{ matrix.pixi_env }}
publish: true
title: ${{ matrix.title }}
description: ${{ matrix.description }}
free-disk-space: ${{ matrix.free_disk_space }}
cache-from: ${{ needs.foundation.outputs.cache-from }}
build-args: |
PIXI_ENV=${{ matrix.pixi_env }}

0 comments on commit 786be39

Please sign in to comment.