From c138b8bc7eb77e41b314b1fec336050a37a14f44 Mon Sep 17 00:00:00 2001 From: Sasha Bogicevic Date: Sun, 13 Aug 2023 19:53:28 +0200 Subject: [PATCH] Steal Hydra's ci --- .../actions/setup_dev_ubuntu/action.yaml | 81 +++++++++++++++++++ .../actions/setup_dev_ubuntu/clear_caches | 38 +++++++++ .../setup_dev_ubuntu/install_libraries | 38 +++++++++ .../actions/setup_dev_ubuntu/install_packages | 38 +++++++++ .github/workflows/ci.yaml | 80 ++++++++++++++++++ 5 files changed, 275 insertions(+) create mode 100644 .github/workflows/actions/setup_dev_ubuntu/action.yaml create mode 100644 .github/workflows/actions/setup_dev_ubuntu/clear_caches create mode 100644 .github/workflows/actions/setup_dev_ubuntu/install_libraries create mode 100644 .github/workflows/actions/setup_dev_ubuntu/install_packages create mode 100644 .github/workflows/ci.yaml diff --git a/.github/workflows/actions/setup_dev_ubuntu/action.yaml b/.github/workflows/actions/setup_dev_ubuntu/action.yaml new file mode 100644 index 0000000..ec99720 --- /dev/null +++ b/.github/workflows/actions/setup_dev_ubuntu/action.yaml @@ -0,0 +1,81 @@ +name: 'Setup dev environment' +description: 'Setup a dev environment for gh-action Ubuntu' +inputs: + version_ghc: + description: 'GHC version needed' + required: false + default: 9.2.8 + version_cabal: + description: 'Cabal version needed' + required: false + default: 3.10.1.0 + clean_caches: + description: 'Should we clean old caches for this branch and keep only the last one?' + require: false + default: false +runs: + using: "composite" + steps: + + - name: "Clear old caches" + if: ${{ inputs.clean_caches == 'true' }} + shell: bash + run: | + .github/workflows/actions/setup_dev_ubuntu/clear_caches + env: + GH_TOKEN: ${{ github.token }} + + - name: Cache environment setup + id: cache_env + uses: actions/cache@v3 + with: + path: | + /usr/local/.ghcup/ghc/${{ inputs.version_ghc }} + /usr/local/.ghcup/bin/cabal-${{ inputs.version_cabal }} + /usr/local/.ghcup/bin/cabal + /usr/local/.ghcup/bin/ghc + ${{ github.workspace }}/srv/ + key: env-${{ runner.os }}-${{ hashFiles('.github/workflows/actions/setup_dev_ubuntu/install_libraries') }} + + - name: Cache cabal storage + id: cache_cabal + uses: actions/cache@v3 + with: + path: | + ~/.cache/cabal/packages + ~/.local/state/cabal + key: cabal-${{ runner.os }}-${{ hashFiles('**/*.cabal', '**/cabal.project', '**/cabal.project.freeze') }} + restore-keys: cabal-${{ runner.os }}- + + - name: Cache cabal compilation directory + id: cache_dist + uses: actions/cache@v3 + with: + path: | + dist-newstyle + key: dist-${{ runner.os }}-${{ github.run_id }} + restore-keys: dist-${{ runner.os }}- + + - name: "Install packages" + shell: bash + run: | + .github/workflows/actions/setup_dev_ubuntu/install_packages + env: + VERSION_GHC: ${{ inputs.version_ghc }} + + - name: "Setup build environment" + if: ${{ steps.cache_env.outputs.cache-hit != 'true' }} + shell: bash + run: | + .github/workflows/actions/setup_dev_ubuntu/install_libraries + + - name: "Setup Environment" + shell: bash + run: | + echo "$(realpath $PWD/srv)/bin" >> $GITHUB_PATH + + - name: cabal update + if: ${{ steps.cache_cabal.outputs.cache-hit != 'true' }} + shell: bash + run: | + cabal update diff --git a/.github/workflows/actions/setup_dev_ubuntu/clear_caches b/.github/workflows/actions/setup_dev_ubuntu/clear_caches new file mode 100644 index 0000000..c9544c3 --- /dev/null +++ b/.github/workflows/actions/setup_dev_ubuntu/clear_caches @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +set -euxo pipefail + +main() { + gh extension install --force actions/gh-actions-cache + + local repo="$GITHUB_REPOSITORY" + local branch="$GITHUB_REF" + + list_caches $repo $branch dist \ + | all_but_the_first_one \ + | delete_cache $repo $branch +} + +list_caches() { + local repo="$1" + local branch="$2" + local cache="$3" + gh actions-cache list -R "$repo" -B "$branch" --key "$cache" --order desc | cut -f 1 +} + +all_but_the_first_one() { + sed 1d +} + +delete_cache() { + local repo="$1" + local branch="$2" + + while read cache + do + echo "I could delete this cache (but I won't) if needed: $cache" >&2 + echo gh actions-cache delete "$cache" -R "$repo" -B "$branch" --confirm + done +} + +main "$@" diff --git a/.github/workflows/actions/setup_dev_ubuntu/install_libraries b/.github/workflows/actions/setup_dev_ubuntu/install_libraries new file mode 100644 index 0000000..c9544c3 --- /dev/null +++ b/.github/workflows/actions/setup_dev_ubuntu/install_libraries @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +set -euxo pipefail + +main() { + gh extension install --force actions/gh-actions-cache + + local repo="$GITHUB_REPOSITORY" + local branch="$GITHUB_REF" + + list_caches $repo $branch dist \ + | all_but_the_first_one \ + | delete_cache $repo $branch +} + +list_caches() { + local repo="$1" + local branch="$2" + local cache="$3" + gh actions-cache list -R "$repo" -B "$branch" --key "$cache" --order desc | cut -f 1 +} + +all_but_the_first_one() { + sed 1d +} + +delete_cache() { + local repo="$1" + local branch="$2" + + while read cache + do + echo "I could delete this cache (but I won't) if needed: $cache" >&2 + echo gh actions-cache delete "$cache" -R "$repo" -B "$branch" --confirm + done +} + +main "$@" diff --git a/.github/workflows/actions/setup_dev_ubuntu/install_packages b/.github/workflows/actions/setup_dev_ubuntu/install_packages new file mode 100644 index 0000000..c9544c3 --- /dev/null +++ b/.github/workflows/actions/setup_dev_ubuntu/install_packages @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +set -euxo pipefail + +main() { + gh extension install --force actions/gh-actions-cache + + local repo="$GITHUB_REPOSITORY" + local branch="$GITHUB_REF" + + list_caches $repo $branch dist \ + | all_but_the_first_one \ + | delete_cache $repo $branch +} + +list_caches() { + local repo="$1" + local branch="$2" + local cache="$3" + gh actions-cache list -R "$repo" -B "$branch" --key "$cache" --order desc | cut -f 1 +} + +all_but_the_first_one() { + sed 1d +} + +delete_cache() { + local repo="$1" + local branch="$2" + + while read cache + do + echo "I could delete this cache (but I won't) if needed: $cache" >&2 + echo gh actions-cache delete "$cache" -R "$repo" -B "$branch" --confirm + done +} + +main "$@" diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..b77fa39 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,80 @@ +name: "CI" + +# Limit concurrent runs of this workflow within a single PR +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +on: + push: + branches: + - master + - release + pull_request: + +env: + LD_LIBRARY_PATH: ${{ github.workspace }}/srv/lib + PKG_CONFIG_PATH: ${{ github.workspace }}/srv/lib/pkgconfig + +jobs: + build: + name: "Build" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: setup dev environment + uses: ./.github/workflows/actions/setup_dev_ubuntu + with: + clean_caches: true + version_ghc: 9.2.8 + + - name: cabal build + run: | + cabal build all + + test: + name: "Test" + needs: [build] + runs-on: ubuntu-latest + strategy: + matrix: + include: + - package: cardano-lab + steps: + - uses: actions/checkout@v3 + + - name: setup dev environment + uses: ./.github/workflows/actions/setup_dev_ubuntu + with: + version_ghc: 9.2.8 + + - name: ❓ Test + if: ${{ matrix.package != 'cardano-lab' }} + run: | + cabal test ${{ matrix.package }} + + + haddock: + name: "Haddock" + needs: [build] + + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: setup dev environment + uses: ./.github/workflows/actions/setup_dev_ubuntu + with: + version_ghc: 9.2.8 + + - name: 📚 Documentation (Haddock) + run: | + .github/workflows/ci-haddock.sh + + - name: 💾 Upload build & test artifacts + uses: actions/upload-artifact@v3 + with: + name: haddocks + path: ./docs/static/haddock +