From 627cee8c8371bc101704372beb5425c4c83171be Mon Sep 17 00:00:00 2001 From: Paul-Elliot Date: Tue, 9 Jan 2024 00:28:20 +0100 Subject: [PATCH] Prepare for GH release with static binary Signed-off-by: Paul-Elliot --- .github/workflows/build.yaml | 140 ++++++++++++++++++ compiler/src/bin/dune | 3 + compiler/src/bin/static-linking-flags/dune | 4 + .../static_linking_flags.ml | 7 + dune-project | 7 + release/Dockerfile | 9 ++ release/release.sh | 10 ++ slipshow.opam | 7 + 8 files changed, 187 insertions(+) create mode 100644 .github/workflows/build.yaml create mode 100644 compiler/src/bin/static-linking-flags/dune create mode 100644 compiler/src/bin/static-linking-flags/static_linking_flags.ml create mode 100644 release/Dockerfile create mode 100644 release/release.sh diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..c94f539 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,140 @@ +name: CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + linux-build: + strategy: + matrix: + platform: + - { name: "linux/amd64", filename_suffix: "linux-amd64", target_arch: "x86_64" } + - { name: "linux/arm64", filename_suffix: "linux-arm64", target_arch: "arm64" } + runAllTests: + - ${{ startsWith(github.ref, 'refs/tags/') }} + # exclude: + # - platform: { name: "linux/arm64", filename_suffix: "linux-arm64", target_arch: "arm64" } + # runAllTests: false + fail-fast: false + runs-on: ubuntu-latest + + env: + SLIPSHOW_PLATFORM: ${{ matrix.platform.name }} + + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 # Needed to fetch tags + + # The latest tag is needed to build the release tarball but + # actions/checkout fails to fetch it properly. + # https://github.com/actions/checkout/issues/882 + - name: Fetch tags + run: git fetch --tags --force origin + + - name: Set up QEMU + id: qemu + uses: docker/setup-qemu-action@v1 + with: + image: tonistiigi/binfmt:latest + platforms: all + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v1 + with: + driver: docker + + - name: Check images + run: docker image ls + + - name: Build release tarball + # if: startsWith(github.ref, 'refs/tags/') + run: | + mkdir -p new_release; chmod a+rw new_release; + docker buildx build --platform ${{ matrix.platform.name }} --load \ + -f release/Dockerfile \ + -t slipshow-release-${{ matrix.platform.name }} . + docker run --platform ${{ matrix.platform.name }} -v $PWD/new_release:/release \ + -e VERSION=$GITHUB_REF_NAME \ + -e TARGETOS=linux \ + -e TARGETARCH=${{ matrix.platform.target_arch }} \ + -e OUTPUT=/release \ + slipshow-release-${{ matrix.platform.name }} \ + opam exec -- bash release/release.sh + + - name: upload archives + # if: startsWith(github.ref, 'refs/tags/') + uses: actions/upload-artifact@v3 + with: + name: archives + path: new_release/* + + mac-build: + strategy: + fail-fast: false + matrix: + os: + - macos-latest + ocaml-compiler: + - 4.14.x + runs-on: ${{ matrix.os }} + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + # See the same step in the linux build + - name: Fetch tags + run: git fetch --tags --force origin + + - name: Use OCaml ${{ matrix.ocaml-compiler }} + uses: ocaml/setup-ocaml@v2 + with: + ocaml-compiler: ${{ matrix.ocaml-compiler }} + dune-cache: ${{ matrix.os != 'macos-latest' }} + + - name: Install dependencies + run: opam install --deps-only --with-test --with-doc -y . + + - name: Build release tarball + # if: startsWith(github.ref, 'refs/tags/') + run: | + mkdir -p new_release; + export OUTPUT=$PWD/new_release; + export TARGETOS=macos; + export TARGETARCH=x86_64; + export VERSION=$GITHUB_REF_NAME; + opam exec -- bash release/release.sh + + - name: upload archives + # if: startsWith(github.ref, 'refs/tags/') + uses: actions/upload-artifact@v3 + with: + name: archives + path: new_release/* + + release: + if: startsWith(github.ref, 'refs/tags/') + needs: [linux-build, mac-build] + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Download build artifacts + uses: actions/download-artifact@v3 + with: + name: archives + path: archives + + - name: Release + uses: softprops/action-gh-release@v1 + with: + files: | + archives/* diff --git a/compiler/src/bin/dune b/compiler/src/bin/dune index 361fcf4..cf8938b 100644 --- a/compiler/src/bin/dune +++ b/compiler/src/bin/dune @@ -3,6 +3,9 @@ (name main) (modes native) (modules main compile) + (flags + (:standard + (:include static-linking-flags/flags))) (libraries slipshow cmdliner diff --git a/compiler/src/bin/static-linking-flags/dune b/compiler/src/bin/static-linking-flags/dune new file mode 100644 index 0000000..124317a --- /dev/null +++ b/compiler/src/bin/static-linking-flags/dune @@ -0,0 +1,4 @@ +(rule + (with-stdout-to + flags + (run ocaml %{dep:static_linking_flags.ml} %{ocaml-config:target}))) diff --git a/compiler/src/bin/static-linking-flags/static_linking_flags.ml b/compiler/src/bin/static-linking-flags/static_linking_flags.ml new file mode 100644 index 0000000..90e7ccb --- /dev/null +++ b/compiler/src/bin/static-linking-flags/static_linking_flags.ml @@ -0,0 +1,7 @@ +let () = + print_endline + @@ + match Sys.argv.(1) with + | "aarch64-unknown-linux-musl" | "x86_64-pc-linux-musl" -> + "(-cclib -static -cclib -no-pie)" + | _ -> "()" diff --git a/dune-project b/dune-project index fe527ce..e3bd8a6 100644 --- a/dune-project +++ b/dune-project @@ -29,6 +29,13 @@ base64 bos cmarkit + lwt + inotify + js_of_ocaml-compiler + magic-mime + dream + fpath + dream-livereload (odoc :with-doc) (ocamlformat (and :with-dev-setup (= 0.26.1)))) diff --git a/release/Dockerfile b/release/Dockerfile new file mode 100644 index 0000000..e8d2096 --- /dev/null +++ b/release/Dockerfile @@ -0,0 +1,9 @@ +FROM ocaml/opam:alpine-ocaml-4.14 +RUN sudo apk add libev-dev openssl-dev +RUN sudo apk add openssl-libs-static +WORKDIR slipshow/ + +COPY --chown=opam *.opam . +RUN opam install -y --deps-only --with-test --with-doc . + +COPY --chown=opam . . diff --git a/release/release.sh b/release/release.sh new file mode 100644 index 0000000..2140357 --- /dev/null +++ b/release/release.sh @@ -0,0 +1,10 @@ +#/usr/bin/env bash +set -xeuo pipefail + +archive_name=$OUTPUT/slipshow-$TARGETOS-$TARGETARCH.tar + +dune subst + +dune build -p slipshow +# Executables are symlinks, follow with -h. +tar hcf "$archive_name" -C _build/install/default bin/slipshow diff --git a/slipshow.opam b/slipshow.opam index f59a03f..9a16a00 100644 --- a/slipshow.opam +++ b/slipshow.opam @@ -17,6 +17,13 @@ depends: [ "base64" "bos" "cmarkit" + "lwt" + "inotify" + "js_of_ocaml-compiler" + "magic-mime" + "dream" + "fpath" + "dream-livereload" "odoc" {with-doc} "ocamlformat" {with-dev-setup & = "0.26.1"} ]