Skip to content

Commit

Permalink
Merge pull request #452 from williamdes/multi-dist
Browse files Browse the repository at this point in the history
Build multi OS for Docker
  • Loading branch information
SimonKagstrom authored Jul 14, 2024
2 parents 41cca56 + c7a2700 commit 1e383e5
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 67 deletions.
84 changes: 69 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI
permissions:
contents: read

on: [push, pull_request]
on: [push, pull_request, workflow_dispatch]

jobs:
build-linux:
Expand Down Expand Up @@ -87,18 +87,76 @@ jobs:
.github/workflows/freebsd-build.sh x86_64
.github/workflows/ci-run-tests.sh
build-alpine:
runs-on: ubuntu-22.04
container:
image: alpine:3.20
# Needed to attach to a binary
options: --security-opt seccomp=unconfined
name: Build and test Alpine executable
steps:
- uses: actions/checkout@v4
- name: Install tools and dependencies
run: |
apk add --update --no-cache \
libcurl \
libdw \
zlib \
libgcc \
libstdc++ \
\
bash \
patchelf \
binutils \
python3 \
coreutils-env \
\
binutils-dev \
build-base \
cmake \
git \
curl-dev \
curl-static \
libdw \
openssl-dev \
ninja-build \
python3 \
zlib-dev \
elfutils-dev \
libstdc++-dev
- name: Run the tests
run: |
chmod u+x .github/workflows/freebsd-build.sh .github/workflows/ci-run-tests.sh
.github/workflows/freebsd-build.sh x86_64
.github/workflows/ci-run-tests.sh
build-and-push-docker-image:
name: Build and push to Docker Hub
needs: [build-linux, build-osx, build-freebsd]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- {
BUILD_OS: "alpine",
IMAGE_TAG_SUFFIX: "-alpine",
PLATFORM: "linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/riscv64"
}
- {
BUILD_OS: "debian",
IMAGE_TAG_SUFFIX: "",
PLATFORM: "linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8"
}
environment:
name: "Docker Hub"
url: https://hub.docker.com/r/kcov/kcov
if: github.ref == 'refs/heads/master'
if: ${{ github.ref == 'refs/heads/master' }} or ${{ github.event_name == 'workflow_dispatch' }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true

- name: Docker Hub login
uses: docker/login-action@v3
Expand All @@ -113,29 +171,25 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push the docker image (latest)
# https://github.com/docker/buildx#building
run: |
docker buildx build \
--tag $IMAGE_TAG \
--platform $PLATFORM \
--push \
.
- name: Build and push the docker image (latest/${{ matrix.BUILD_OS }})
run: .github/workflows/docker-build.sh ${{ matrix.BUILD_OS }}
env:
IMAGE_TAG: kcov/kcov:latest
# All (build on the base image):
ACTION: push
IMAGE_TAG: kcov/kcov:latest${{ matrix.IMAGE_TAG_SUFFIX }}
# All (built on the base image):
# - linux/386
# - linux/amd64
# - linux/arm/v6
# - linux/arm/v6 -> Only Alpine
# - linux/arm/v5 -> Only Debian
# - linux/arm/v7
# - linux/arm64/v8
# - linux/ppc64le
# - linux/mips64le
# - linux/riscv64
# - linux/riscv64 -> Only Alpine
# - linux/s390x
# Does not build:
# - linux/s390x
# - linux/mips64le
# No users on this platform:
# - linux/ppc64le
PLATFORM: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/riscv64
PLATFORM: ${{ matrix.PLATFORM }}
38 changes: 38 additions & 0 deletions .github/workflows/docker-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/sh

set -eu

if [ "${1:-}" = "alpine" ]; then
BUILD_OS="alpine"
BUILD_BASE="3.20"
elif [ "${1:-}" = "debian" ]; then
BUILD_OS="debian"
BUILD_BASE="bookworm-slim"
else
echo "Usage: docker-build.sh alpine"
echo "Usage: docker-build.sh debian"
exit 1
fi

if [ -z "${RELEASE_VERSION:-}" ]; then
RELEASE_VERSION="$(git describe --abbrev=4 --tags HEAD || git describe --abbrev=4 HEAD || git rev-parse HEAD)"
fi

set -x

echo "Building for: ${BUILD_OS}"
echo "Release: ${RELEASE_VERSION}"

# https://github.com/docker/buildx#building
docker buildx build \
--build-arg VCS_REF="$(git rev-parse HEAD)" \
--build-arg BUILD_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
--build-arg BUILD_OS="${BUILD_OS}" \
--build-arg RELEASE_VERSION="${RELEASE_VERSION}" \
--build-arg BUILD_BASE="${BUILD_BASE}" \
--tag ${IMAGE_TAG:-kcov} \
--progress ${PROGRESS_MODE:-plain} \
--platform ${PLATFORM:-linux/amd64} \
--pull \
--${ACTION:-load} \
.
37 changes: 24 additions & 13 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,28 @@ jobs:
name: Build and push to Docker Hub
needs: [build]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- {
BUILD_OS: "alpine",
IMAGE_TAG_SUFFIX: "-alpine",
PLATFORM: "linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/riscv64"
}
- {
BUILD_OS: "debian",
IMAGE_TAG_SUFFIX: "",
PLATFORM: "linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8"
}
environment:
name: "Docker Hub"
url: https://hub.docker.com/r/kcov/kcov
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true

- name: Docker Hub login
uses: docker/login-action@v3
Expand All @@ -58,32 +73,28 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push the docker image (${{ env.RELEASE_TAG }})
# https://github.com/docker/buildx#building
run: |
docker buildx build \
--tag $IMAGE_TAG \
--platform $PLATFORM \
--push \
.
- name: Build and push the docker image (v${{ env.RELEASE_TAG }}/${{ matrix.BUILD_OS }})
run: .github/workflows/docker-build.sh ${{ matrix.BUILD_OS }}
env:
IMAGE_TAG: kcov/kcov:${{ env.RELEASE_TAG }}
# All (build on the base image):
ACTION: push
IMAGE_TAG: kcov/kcov:${{ env.RELEASE_TAG }}${{ matrix.IMAGE_TAG_SUFFIX }}
# All (built on the base image):
# - linux/386
# - linux/amd64
# - linux/arm/v6
# - linux/arm/v6 -> Only Alpine
# - linux/arm/v5 -> Only Debian
# - linux/arm/v7
# - linux/arm64/v8
# - linux/ppc64le
# - linux/mips64le
# - linux/riscv64
# - linux/riscv64 -> Only Alpine
# - linux/s390x
# Does not build:
# - linux/s390x
# - linux/mips64le
# No users on this platform:
# - linux/ppc64le
PLATFORM: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/riscv64
PLATFORM: ${{ matrix.PLATFORM }}

create_release:
name: Create release
Expand Down
112 changes: 92 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,55 +1,102 @@
FROM alpine:3.20 AS builder
ARG BUILD_OS
ARG BUILD_BASE

RUN apk add --update --no-cache \
FROM ${BUILD_OS}:${BUILD_BASE} AS builder

ARG BUILD_OS
ARG BUILD_BASE

RUN set -eux; \
echo "Building for: ${BUILD_OS}"; \
if [ "${BUILD_OS}" = "alpine" ]; then \
apk add --update --no-cache \
binutils-dev \
# Debian: build-essential
build-base \
cmake \
git \
# Debian: libcurl4-openssl-dev
curl-dev \
curl-static \
# Debian: libdw-dev
libdw \
# Debian, alpine bundled in binutils-dev
# libiberty-dev \
# Debian: libssl-dev
openssl-dev \
ninja-build \
python3 \
# Debian: zlib1g-dev
zlib-dev \
# Debian: libelf-dev
elfutils-dev \
# Debian: libstdc++-12-dev
libstdc++-dev \
;
; \
elif [ "${BUILD_OS}" = "debian" ]; then \
apt-get update; \
apt-get install -y --no-install-recommends --no-install-suggests \
binutils-dev \
build-essential \
cmake \
git \
libcurl4-openssl-dev \
libdw-dev \
# Alpine: bundled in binutils-dev
libiberty-dev \
libssl-dev \
ninja-build \
python3 \
zlib1g-dev \
libelf-dev \
libstdc++-12-dev \
; \
fi

ADD . /src/

RUN mkdir /src/build && \
cd /src/build && \
export PATH="$PATH:/usr/lib/ninja-build/bin/" && \
cmake -G 'Ninja' .. && \
cmake --build . && \
cmake --build . --target install
RUN set -eux; \
mkdir /src/build; \
cd /src/build; \
if [ "${BUILD_OS}" = "alpine" ]; then \
export PATH="$PATH:/usr/lib/ninja-build/bin/"; \
fi; \
cmake -G 'Ninja' ..; \
cmake --build .; \
cmake --build . --target install;

ARG BUILD_OS
ARG BUILD_BASE

FROM ${BUILD_OS}:${BUILD_BASE}

FROM alpine:3.20
ARG BUILD_OS
ARG BUILD_BASE

COPY --from=builder /usr/local/bin/kcov* /usr/local/bin/
COPY --from=builder /usr/local/share/doc/kcov /usr/local/share/doc/kcov

RUN set -eux; \
echo "Building for: ${BUILD_OS}"; \
if [ "${BUILD_OS}" = "alpine" ]; then \
apk add --update --no-cache \
bash \
libcurl \
libdw \
zlib \
libgcc \
libstdc++ \
# libbfd.so
binutils-dev \
# To avoid changing the value of --python-parser
python3 \
; \
elif [ "${BUILD_OS}" = "debian" ]; then \
apt-get update; \
apt-get install -y --no-install-recommends --no-install-suggests \
libcurl4 \
libdw1 \
zlib1g \
libbfd-dev \
# To avoid changing the value of --python-parser
python-is-python3 \
; \
apt-get clean; \
rm -rf /var/lib/apt/lists/*; \
fi; \
# Write a test script
echo -e '#!/usr/bin/env bash\nif [[ true ]]; then\necho "Hello, kcov!"\nfi' > /tmp/test-executable.sh; \
printf '#!/usr/bin/env bash\nif [[ true ]]; then\necho "Hello, kcov!"\nfi' > /tmp/test-executable.sh; \
# Test kcov
kcov --include-pattern=/tmp/test-executable.sh /tmp/coverage /tmp/test-executable.sh; \
# Display
Expand All @@ -60,4 +107,29 @@ RUN set -eux; \
# Cleanup
rm -r /tmp/coverage /tmp/test-executable.sh;

# Metadata
LABEL org.label-schema.vendor="Kcov" \
org.label-schema.url="https://github.com/SimonKagstrom/kcov#readme" \
org.label-schema.name="The docker image for kcov" \
org.label-schema.description="A docker image for kcov" \
org.label-schema.version=${RELEASE_VERSION} \
org.label-schema.vcs-url="https://github.com/SimonKagstrom/kcov.git" \
org.label-schema.vcs-ref=${VCS_REF} \
org.label-schema.build-date=${BUILD_DATE} \
org.label-schema.docker.schema-version="1.0" \
\
com.docker.extension.publisher-url="https://github.com/SimonKagstrom" \
\
org.opencontainers.image.title="The docker image for kcov" \
org.opencontainers.image.description="A docker image for kcov" \
org.opencontainers.image.authors="https://github.com/SimonKagstrom" \
org.opencontainers.image.url="https://github.com/SimonKagstrom/kcov#readme" \
org.opencontainers.image.documentation="https://github.com/SimonKagstrom/kcov#readme" \
org.opencontainers.image.source="https://github.com/SimonKagstrom/kcov" \
org.opencontainers.image.vendor="Kcov" \
org.opencontainers.image.licenses="GPL-2.0" \
org.opencontainers.image.created=${BUILD_DATE} \
org.opencontainers.image.version=${RELEASE_VERSION} \
org.opencontainers.image.revision=${VCS_REF}

CMD ["/usr/local/bin/kcov"]
Loading

0 comments on commit 1e383e5

Please sign in to comment.