Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build multi OS for Docker #452

Merged
merged 22 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
73 changes: 62 additions & 11 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 @@ -80,18 +80,74 @@ 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, as you suggested, add this to the instructions somewhere? I guess the docker info can be placed in a doc/docker.md file, to reduce the clutter in README.md.

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"
}
- {
BUILD_OS: "debian",
IMAGE_TAG_SUFFIX: ""
}
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 @@ -106,16 +162,11 @@ 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
ACTION: push
IMAGE_TAG: kcov/kcov:latest${{ matrix.IMAGE_TAG_SUFFIX }}
# All (build on the base image):
# - linux/386
# - linux/amd64
Expand Down
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} \
.
28 changes: 18 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,26 @@ 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"
}
- {
BUILD_OS: "debian",
IMAGE_TAG_SUFFIX: ""
}
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,16 +71,11 @@ 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 }}
ACTION: push
IMAGE_TAG: kcov/kcov:${{ env.RELEASE_TAG }}${{ matrix.IMAGE_TAG_SUFFIX }}
# All (build on the base image):
# - linux/386
# - linux/amd64
Expand All @@ -83,7 +91,7 @@ jobs:
# - 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: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/riscv64

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
williamdes marked this conversation as resolved.
Show resolved Hide resolved

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