Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,37 @@ on:

jobs:
build:
runs-on: ubuntu-latest
# Build on each platform the release publishes, on native runners.
strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
runner: ubuntu-24.04
- platform: linux/arm64
runner: ubuntu-24.04-arm
runs-on: ${{ matrix.runner }}
if: ${{ github.repository == 'codewars/haskell' }}
steps:
- uses: actions/checkout@v2
- uses: docker/setup-buildx-action@v2
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3

- name: Build image
uses: docker/build-push-action@v3
uses: docker/build-push-action@v6
with:
context: .
platforms: ${{ matrix.platform }}
push: false
# Make the image available in next step
load: true
tags: ghcr.io/codewars/haskell:latest
cache-from: type=gha
cache-to: type=gha,mode=max
cache-from: type=gha,scope=${{ matrix.platform }}
cache-to: type=gha,mode=max,scope=${{ matrix.platform }}

- name: Run Passing Example
run: bin/run passing

- name: Report Image Size
run: |
echo "## Image Size" >> $GITHUB_STEP_SUMMARY
echo "## Image Size (${{ matrix.platform }})" >> $GITHUB_STEP_SUMMARY
docker image inspect --format '{{.Size}}' ghcr.io/codewars/haskell:latest | numfmt --to=si --suffix=B >> $GITHUB_STEP_SUMMARY
94 changes: 79 additions & 15 deletions .github/workflows/push-image.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,103 @@
# Build and push a Docker image to GitHub Container Registry when
# a new tag is pushed.
# Build and push a multi-platform Docker image to GitHub Container Registry
# when a new tag is pushed. Each platform builds on a native runner, because
# compiling the Haskell packages under QEMU emulation is too slow.
name: Push Image

on:
push:
tags:
- "*"

env:
IMAGE: ghcr.io/codewars/haskell

jobs:
build-and-push-image:
build:
if: ${{ github.repository == 'codewars/haskell' }}
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
include:
- platform: linux/amd64
runner: ubuntu-24.04
- platform: linux/arm64
runner: ubuntu-24.04-arm
runs-on: ${{ matrix.runner }}
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
uses: docker/setup-buildx-action@v3

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
registry: ghcr.io
username: codewars
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push image
uses: docker/build-push-action@v3
- name: Build and push by digest
id: build
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: |
ghcr.io/codewars/haskell:latest
ghcr.io/codewars/haskell:${{ github.ref_name }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: ${{ matrix.platform }}
outputs: type=image,name=${{ env.IMAGE }},push-by-digest=true,name-canonical=true,push=true
cache-from: type=gha,scope=${{ matrix.platform }}
cache-to: type=gha,mode=max,scope=${{ matrix.platform }}

- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"

- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digest-${{ strategy.job-index }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1

merge:
needs: build
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digest-*
merge-multiple: true

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: codewars
password: ${{ secrets.GITHUB_TOKEN }}

- name: Create multi-platform tags
working-directory: /tmp/digests
run: |
sources=()
for digest in *; do sources+=("$IMAGE@sha256:$digest"); done
docker buildx imagetools create \
-t "$IMAGE:latest" \
-t "$IMAGE:${{ github.ref_name }}" \
"${sources[@]}"

- name: Check both platforms are published
run: |
manifest=$(docker buildx imagetools inspect --raw "$IMAGE:${{ github.ref_name }}")
echo "$manifest" | jq -e '[.manifests[].platform | "\(.os)/\(.architecture)"] | index("linux/amd64") and index("linux/arm64")'
docker buildx imagetools inspect "$IMAGE:${{ github.ref_name }}"
165 changes: 42 additions & 123 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,127 +1,46 @@
FROM debian:buster-slim
# Based on https://github.com/haskell/docker-haskell/blob/12cd297d7ccc2e97fe5e94548ae502b0cbb6735f/9.2/slim-buster/Dockerfile
# GHC 9.14.1, Cabal 3.16.1.0, Stack 3.11.1; Linux amd64 and arm64.
# https://github.com/haskell/docker-haskell/tree/870b06856b1f4dfc09cd710ab9c680060d25e202/9.14/slim-bookworm
FROM haskell:9.14.1-slim-bookworm@sha256:0ab6a64546d7cddfe30cea87eb1aeb9155f468c8aaf53a9b2d4d77c597a429ec

ENV LANG=C.UTF-8

# common haskell + stack dependencies
RUN set -ex; \
apt-get update; \
apt-get install -y --no-install-recommends \
ca-certificates \
curl \
dpkg-dev \
git \
gcc \
gnupg \
g++ \
libc6-dev \
libffi-dev \
libgmp-dev \
libnuma-dev \
libtinfo-dev \
make \
netbase \
xz-utils \
zlib1g-dev \
# for regex-pcre
libpcre3 \
libpcre3-dev \
pkg-config \
; \
rm -rf /var/lib/apt/lists/*;

ENV STACK=2.9.3 STACK_RELEASE_KEY=C5705533DA4F78D8664B5DC0575159689BEFB442
RUN set -eux; \
cd /tmp; \
STACK_URL="https://github.com/commercialhaskell/stack/releases/download/v${STACK}/stack-${STACK}-linux-x86_64.tar.gz"; \
# sha256 from https://github.com/commercialhaskell/stack/releases/download/v${STACK}/stack-${STACK}-linux-x86_64.tar.gz.sha256
STACK_SHA256='938f689dc45e2693ab1ca3ea215790b3786dfd531dcf6c0bf40842c24e579ae9'; \
curl -sSL "$STACK_URL" -o stack.tar.gz; \
echo "$STACK_SHA256 stack.tar.gz" | sha256sum --strict --check; \
\
curl -sSL "$STACK_URL.asc" -o stack.tar.gz.asc; \
GNUPGHOME="$(mktemp -d)"; export GNUPGHOME; \
gpg --batch --keyserver keyserver.ubuntu.com --receive-keys "$STACK_RELEASE_KEY"; \
gpg --batch --verify stack.tar.gz.asc stack.tar.gz; \
gpgconf --kill all; \
\
tar -xf stack.tar.gz -C /usr/local/bin --strip-components=1 "stack-$STACK-linux-x86_64/stack"; \
stack config set system-ghc --global true; \
stack config set install-ghc --global false; \
\
rm -rf /tmp/*; \
\
stack --version;

ENV CABAL_INSTALL=3.8.1.0 CABAL_INSTALL_RELEASE_KEY=E9EC5616017C3EE26B33468CCE1ED8AE0B011D8C
RUN set -eux; \
cd /tmp; \
CABAL_INSTALL_TAR="cabal-install-$CABAL_INSTALL-x86_64-linux-deb10.tar.xz"; \
CABAL_INSTALL_URL="https://downloads.haskell.org/~cabal/cabal-install-$CABAL_INSTALL/$CABAL_INSTALL_TAR"; \
CABAL_INSTALL_SHA256SUMS_URL="https://downloads.haskell.org/~cabal/cabal-install-$CABAL_INSTALL/SHA256SUMS"; \
# sha256 from https://downloads.haskell.org/~cabal/cabal-install-$CABAL_INSTALL/SHA256SUMS
CABAL_INSTALL_SHA256='c71a1a46fd42d235bb86be968660815c24950e5da2d1ff4640da025ab520424b'; \
curl -fSL "$CABAL_INSTALL_URL" -o cabal-install.tar.gz; \
echo "$CABAL_INSTALL_SHA256 cabal-install.tar.gz" | sha256sum --strict --check; \
\
curl -sSLO "$CABAL_INSTALL_SHA256SUMS_URL"; \
curl -sSLO "$CABAL_INSTALL_SHA256SUMS_URL.sig"; \
GNUPGHOME="$(mktemp -d)"; export GNUPGHOME; \
gpg --batch --keyserver keyserver.ubuntu.com --receive-keys "$CABAL_INSTALL_RELEASE_KEY"; \
gpg --batch --verify SHA256SUMS.sig SHA256SUMS; \
# confirm we are verifying SHA256SUMS that matches the release + sha256
grep "$CABAL_INSTALL_SHA256 $CABAL_INSTALL_TAR" SHA256SUMS; \
gpgconf --kill all; \
\
tar -xf cabal-install.tar.gz -C /usr/local/bin; \
\
rm -rf /tmp/*; \
\
cabal --version;

ENV GHC=9.2.5 GHC_RELEASE_KEY=88B57FCF7DB53B4DB3BFA4B1588764FBE22D19C4
RUN set -eux; \
cd /tmp; \
GHC_URL="https://downloads.haskell.org/~ghc/$GHC/ghc-$GHC-x86_64-deb10-linux.tar.xz"; \
# sha256 from https://downloads.haskell.org/~ghc/$GHC/SHA256SUMS
GHC_SHA256='89f2df47d86a45593d6ba3fd3a44b627d100588cd59be257570dbe3f92b17c48'; \
curl -sSL "$GHC_URL" -o ghc.tar.xz; \
echo "$GHC_SHA256 ghc.tar.xz" | sha256sum --strict --check; \
\
GNUPGHOME="$(mktemp -d)"; export GNUPGHOME; \
curl -sSL "$GHC_URL.sig" -o ghc.tar.xz.sig; \
gpg --batch --keyserver keyserver.ubuntu.com --receive-keys "$GHC_RELEASE_KEY"; \
gpg --batch --verify ghc.tar.xz.sig ghc.tar.xz; \
gpgconf --kill all; \
\
tar xf ghc.tar.xz; \
cd "ghc-$GHC"; \
./configure --prefix "/opt/ghc/$GHC"; \
make install; \
# remove profiling support to save space
find "/opt/ghc/$GHC/" \( -name "*_p.a" -o -name "*.p_hi" \) -type f -delete; \
# remove some docs
rm -rf "/opt/ghc/$GHC/share/"; \
\
rm -rf /tmp/*; \
\
"/opt/ghc/$GHC/bin/ghc" --version;

ENV PATH=/opt/ghc/${GHC}/bin:$PATH

RUN set -ex; \
useradd --create-home codewarrior; \
mkdir -p /workspace; \
chown codewarrior: /workspace;

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
libpcre3-dev=2:8.39-15 libsqlite3-dev=3.40.1-2+deb12u2 pkg-config=1.8.1-1 \
&& rm -rf /var/lib/apt/lists/* \
&& useradd --create-home --uid 1000 codewarrior \
&& mkdir -p /workspace /opt/haskell /opt/cabal \
&& chown codewarrior:codewarrior /workspace /opt/haskell /opt/cabal

WORKDIR /opt/haskell
USER codewarrior
ENV USER=codewarrior HOME=/home/codewarrior
ENV PATH=/home/codewarrior/.cabal/bin:/home/codewarrior/.local/bin:$PATH

COPY --chown=codewarrior:codewarrior workspace/ /workspace/
ENV USER=codewarrior HOME=/home/codewarrior CABAL_DIR=/opt/cabal
ENV PATH=/opt/cabal/bin:/opt/ghc/9.14.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# These are the existing public Codewars APIs, verified before extraction.
RUN curl -fsSL https://codeload.github.com/codewars/hspec-formatters-codewars/tar.gz/refs/tags/v0.3.0 -o formatter.tar.gz \
&& echo 'c7572cf25789bf5b153195813fbbb3b9509ba693bccfb1db521ee09364f8b173 formatter.tar.gz' | sha256sum -c - \
&& tar xzf formatter.tar.gz \
&& curl -fsSL https://codeload.github.com/codewars/hspec-codewars/tar.gz/refs/tags/v0.1.0 -o helpers.tar.gz \
&& echo '3c29ea2a255aef452e95655dbfbba2c168b2c44afbdad5b76729de3c0061d77b helpers.tar.gz' | sha256sum -c - \
&& tar xzf helpers.tar.gz \
&& rm formatter.tar.gz helpers.tar.gz

# A mirror may be selected for constrained networks; Cabal still verifies Hackage signatures.
ARG HACKAGE_URL=https://hackage.haskell.org/
RUN cabal user-config init \
&& sed -i "s|url: http://hackage.haskell.org/|url: ${HACKAGE_URL}|" /opt/cabal/config \
&& cabal update

COPY --chown=codewarrior:codewarrior rootfs/opt/haskell/challenge.cabal rootfs/opt/haskell/cabal.project rootfs/opt/haskell/cabal.project.freeze ./
COPY --chown=codewarrior:codewarrior rootfs/opt/haskell/src/ src/
RUN cabal build all --enable-tests \
&& cp .ghc.environment.* environment \
&& mkdir -p /opt/cabal/bin \
&& find /opt/cabal/store -type f -path '*/bin/hspec-discover' -exec cp '{}' /opt/cabal/bin/hspec-discover ';'

# Direct ghc/runghc calls use the prebuilt package set without a runtime resolver.
ENV GHC_ENVIRONMENT=/opt/haskell/environment
COPY --chown=codewarrior:codewarrior rootfs/workspace/ /workspace/
COPY --chown=codewarrior:codewarrior rootfs/opt/haskell/smoke/ smoke/
RUN sh /opt/haskell/smoke/run.sh
WORKDIR /workspace
RUN stack build
# Sanity check
RUN set -ex; \
stack ghc -- -O -isrc:test --make -o tests -outputdir /tmp test/Main.hs && ./tests; \
rm src/Example.hs test/ExampleSpec.hs tests /tmp/*;
Loading
Loading