Build against musl in docker

This commit is contained in:
Caleb Xavier Berger 2021-01-18 22:47:12 -05:00
parent cecdec2dec
commit 2e473d83af
5 changed files with 62 additions and 39 deletions

13
.github/workflows/Dockerfile.builder vendored Normal file
View file

@ -0,0 +1,13 @@
FROM docker.io/golang:alpine AS builder
ARG GOARCH=amd64
RUN apk --update --no-cache add bash build-base
WORKDIR /build
COPY . /build
RUN bash /build/.github/workflows/get-compiler.sh pkgs
# Build!
RUN CC="$(/build/.github/workflows/get-compiler.sh ccomp)" bash /build/build.sh

View file

@ -1,11 +1,12 @@
FROM alpine:latest FROM alpine:latest
ARG DENDRITE_BINARY= ARG DENDRITE_BINARY=
ENV DENDRITE_BINARY=/usr/bin/${DENDRITE_BINARY} ARG BIN_DIRECTORY=
ENV DENDRITE_BINARY=${DENDRITE_BINARY}
COPY ./bin/* /usr/bin COPY --from=builder /build/bin/* /usr/bin
VOLUME /etc/dendrite VOLUME /etc/dendrite
WORKDIR /etc/dendrite WORKDIR /etc/dendrite
ENTRYPOINT ["${DENDRITE_BINARY}"] ENTRYPOINT "/usr/bin/${DENDRITE_BINARY}"

View file

@ -13,20 +13,29 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: actions/setup-go@v2 - uses: docker/setup-buildx-action@v1
- name: install (cross) compiler - name: build dendrite in Docker
run: sudo apt install $(./.github/workflows/get-compiler.sh pkgs) uses: docker/build-push-action@v2
- name: set CC env var with:
run: echo "CC=$(./.github/workflows/get-compiler.sh ccomp)" >> $GITHUB_ENV # https://stackoverflow.com/a/57969570/3551604 context: .
- run: go env file: ./.github/workflows/Dockerfile.builder
- run: ./build.sh load: true # load into local image store
- name: upload build artifacts tag: dendrite-builder:${{ matrix.target }}
uses: actions/upload-artifact@v2 - name: copy built files out of docker
run: docker -v "$(pwd)/bin:/build/bin-out" builder:${{ matrix.target }} "/bin/sh" "-c" "cp -a /build/bin/* /build/bin-out"
- uses: actions/upload-artifact@v2
with: with:
name: bin-${{ matrix.target }} name: bin-${{ matrix.target }}
path: ./bin path: ./bin
build-monolith: build-monolith:
needs: [compile] needs: [compile]
local-registry:
runs-on: ubuntu-latest
services:
registry:
image: registry:2
ports:
- 5000:5000
strategy: strategy:
matrix: matrix:
target: [amd64, arm64, arm] target: [amd64, arm64, arm]
@ -40,17 +49,14 @@ jobs:
name: bin-${{ matrix.target }} name: bin-${{ matrix.target }}
# - run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV # - run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- run: echo "DOCKER_TARGET=$(./.github/workflows/get-compiler.sh docker)" >> $GITHUB_ENV - run: echo "DOCKER_TARGET=$(./.github/workflows/get-compiler.sh docker)" >> $GITHUB_ENV
# For building docker images, QEMU and buildx is still easier than anything to do with docker manifest editing - uses: docker/setup-qemu-action@v1
- name: Set up QEMU - uses: docker/setup-buildx-action@v1
uses: docker/setup-qemu-action@v1 with:
- name: Set up Docker Buildx driver-opts: network=host
uses: docker/setup-buildx-action@v1 - uses: docker/build-push-action@v2
- name: Build image
uses: docker/build-push-action@v2
with: with:
context: . context: .
file: ./.github/workflows/Dockerfile.copyin file: ./.github/workflows/Dockerfile.copyin
platforms: ${{ env.PLATFORMS }} platforms: ${{ DOCKER_TARGET }}
push: false push: true
tags: | tags: localhost:5000/matrixdotorg/dendrite-monolith:latest
${{ env.DOCKER_NAMESPACE }}/dendrite-monolith:latest

View file

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
set -eu set -exu
MUSLCC_BASE="https://more.musl.cc/x86_64-linux-musl"
# Given a GOARCH target, return what Docker calls that target. # Given a GOARCH target, return what Docker calls that target.
function get_docker() { function get_docker() {
@ -23,38 +25,39 @@ function get_docker() {
function get_compiler() { function get_compiler() {
case "$GOARCH" in case "$GOARCH" in
"amd64") "amd64")
echo "x86_64-linux-gnu-gcc" echo "gcc"
;; ;;
"arm64") "arm64")
echo "aarch64-linux-gnu-gcc" echo "./aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc"
;; ;;
"arm") "arm")
echo "arm-linux-gnueabihf-gcc" echo "./arm-linux-musleabi-cross/bin/arm-linux-musleabi-gcc"
;; ;;
*) *)
echo "gcc" # Send us a pull request if RISC-V ever takes off exit 1 # Send us a pull request if RISC-V ever takes off
;; ;;
esac esac
} }
# Given a GOARCH target, return a list of Ubuntu packages needed to compile for that target. function download_musl() {
function get_pkgs() {
case "$GOARCH" in case "$GOARCH" in
"arm64") "arm64")
echo "gcc-aarch64-linux-gnu libc6-dev-arm64-cross" curl "${MUSLCC_BASE}/aarch64-linux-musl-cross.tgz" -o musl.tgz
tar xzf musl.tgz
;; ;;
"arm") "arm")
echo "gcc-arm-linux-gnueabihf libc6-dev-armhf-cross" curl "${MUSLCC_BASE}/arm-linux-musleabi-cross.tgz" -o musl.tgz
tar xzf musl.tgz
;; ;;
"amd64" | *) "amd64")
# We (currently) don't need to install more packages on amd64. echo "nothing to do"
;; ;;
esac esac
} }
case "$1" in case "$1" in
"pkgs") "pkgs")
get_pkgs download_musl
;; ;;
"ccomp") "ccomp")
get_compiler get_compiler