Skip to content

Commit

Permalink
Add docker support for multiplatform builds
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyknox committed Nov 15, 2023
1 parent 40aca52 commit 07ecec7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 94 deletions.
154 changes: 71 additions & 83 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -1,96 +1,84 @@
name: docker

on:
push:
tags:
- "v*.*.*"
schedule:
- cron: "0 0 * * *"
# Trigger without any parameters a proactive rebuild
workflow_dispatch: {}
workflow_call:
push:
tags:
- "v*.*.*"
schedule:
- cron: "0 0 * * *"
workflow_dispatch: {}
workflow_call:

env:
REGISTRY: ghcr.io
# Will resolve to foundry-rs/foundry
IMAGE_NAME: ${{ github.repository }}
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
container:
runs-on: ubuntu-20.04
# https://docs.github.com/en/actions/reference/authentication-in-a-workflow
permissions:
id-token: write
packages: write
contents: read
timeout-minutes: 120
steps:
- name: Checkout repository
id: checkout
uses: actions/checkout@v4
container:
runs-on: ubuntu-20.04
permissions:
id-token: write
packages: write
contents: read
timeout-minutes: 360
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Docker BuildX
uses: docker/setup-buildx-action@v2
id: buildx
with:
install: true
- name: Install Docker BuildX
uses: docker/setup-buildx-action@v2
with:
platforms: linux/amd64,linux/arm64
version: latest
buildkitd-flags: --debug

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
# Ensure this doesn't trigger on PR's
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Set lowercase image name
run: echo LOWERCASE_IMAGE_NAME=$(echo ${{ github.repository }} | tr 'A-Z' 'a-z') >> $GITHUB_ENV

# Creates an additional 'latest' or 'nightly' tag
# If the job is triggered via cron schedule, tag nightly and nightly-{SHA}
# If the job is triggered via workflow dispatch and on a master branch, tag branch and latest
# Otherwise, just tag as the branch name
- name: Finalize Docker Metadata
id: docker_tagging
run: |
if [[ "${{ github.event_name }}" == 'schedule' ]]; then
echo "cron trigger, assigning nightly tag"
echo "docker_tags=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:nightly,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:nightly-${GITHUB_SHA}" >> $GITHUB_OUTPUT
elif [[ "${GITHUB_REF##*/}" == "main" ]] || [[ ${GITHUB_REF##*/} == "master" ]]; then
echo "manual trigger from master/main branch, assigning latest tag"
echo "docker_tags=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${GITHUB_REF##*/},${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" >> $GITHUB_OUTPUT
else
echo "Neither scheduled nor manual release from main branch. Just tagging as branch name"
echo "docker_tags=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${GITHUB_REF##*/}" >> $GITHUB_OUTPUT
fi
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.LOWERCASE_IMAGE_NAME }}

# Log docker metadata to explicitly know what is being pushed
- name: Inspect Docker Metadata
run: |
echo "TAGS -> ${{ steps.docker_tagging.outputs.docker_tags }}"
echo "LABELS -> ${{ steps.meta.outputs.labels }}"
- name: Finalize Docker Metadata
id: docker_tagging
run: |
if [[ "${{ github.event_name }}" == 'schedule' ]]; then
echo "cron trigger, assigning nightly tag"
echo "docker_tags=${{ env.REGISTRY }}/${{ env.LOWERCASE_IMAGE_NAME }}:nightly,${{ env.REGISTRY }}/${{ env.LOWERCASE_IMAGE_NAME }}:nightly-${GITHUB_SHA}" >> $GITHUB_OUTPUT
elif [[ "${GITHUB_REF##*/}" == "main" ]] || [[ ${GITHUB_REF##*/} == "master" ]]; then
echo "manual trigger from master/main branch, assigning latest tag"
echo "docker_tags=${{ env.REGISTRY }}/${{ env.LOWERCASE_IMAGE_NAME }}:${GITHUB_REF##*/},${{ env.REGISTRY }}/${{ env.LOWERCASE_IMAGE_NAME }}:latest" >> $GITHUB_OUTPUT
else
echo "Neither scheduled nor manual release from main branch. Just tagging as branch name"
echo "docker_tags=${{ env.REGISTRY }}/${{ env.LOWERCASE_IMAGE_NAME }}:${GITHUB_REF##*/}" >> $GITHUB_OUTPUT
fi
# Build and push Docker image
# https://github.com/docker/build-push-action
# https://github.com/docker/build-push-action/blob/master/docs/advanced/cache.md
- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: ${{ steps.docker_tagging.outputs.docker_tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BUILDTIME=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
REVISION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
- name: Inspect Docker Metadata
run: |
echo "TAGS -> ${{ steps.docker_tagging.outputs.docker_tags }}"
echo "LABELS -> ${{ steps.meta.outputs.labels }}"
- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: ${{ steps.docker_tagging.outputs.docker_tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
# cache-from: type=gha
# cache-to: type=gha,mode=max
build-args: |
BUILDTIME=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
REVISION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
22 changes: 11 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ RUN --mount=type=cache,target=/root/.cargo/registry --mount=type=cache,target=/r
&& strip out/chisel \
&& strip out/anvil;

FROM docker.io/frolvlad/alpine-glibc:alpine-3.16_glibc-2.34 as foundry-client
# FROM docker.io/frolvlad/alpine-glibc:alpine-3.16_glibc-2.34 as foundry-client
FROM alpine:3.18 as foundry-client

RUN apk add --no-cache linux-headers git
RUN apk add --no-cache linux-headers git gcompat

COPY --from=build-environment /opt/foundry/out/forge /usr/local/bin/forge
COPY --from=build-environment /opt/foundry/out/cast /usr/local/bin/cast
Expand All @@ -40,13 +41,12 @@ RUN adduser -Du 1000 foundry

ENTRYPOINT ["/bin/sh", "-c"]


LABEL org.label-schema.build-date=$BUILD_DATE \
org.label-schema.name="Foundry" \
org.label-schema.description="Foundry" \
org.label-schema.url="https://getfoundry.sh" \
org.label-schema.vcs-ref=$VCS_REF \
org.label-schema.vcs-url="https://github.com/foundry-rs/foundry.git" \
org.label-schema.vendor="Foundry-rs" \
org.label-schema.version=$VERSION \
org.label-schema.schema-version="1.0"
org.label-schema.name="Foundry" \
org.label-schema.description="Foundry" \
org.label-schema.url="https://getfoundry.sh" \
org.label-schema.vcs-ref=$VCS_REF \
org.label-schema.vcs-url="https://github.com/foundry-rs/foundry.git" \
org.label-schema.vendor="Foundry-rs" \
org.label-schema.version=$VERSION \
org.label-schema.schema-version="1.0"

0 comments on commit 07ecec7

Please sign in to comment.