|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +name: Stack CI |
| 16 | + |
| 17 | +on: |
| 18 | + push: |
| 19 | + branches: |
| 20 | + - main |
| 21 | + pull_request: |
| 22 | + types: |
| 23 | + - opened |
| 24 | + - reopened |
| 25 | + - synchronize |
| 26 | + |
| 27 | +# Cancel earlier runs for pushes to the same branch or updates to the same PR. |
| 28 | +concurrency: |
| 29 | + group: ci-${{ github.ref }} |
| 30 | + cancel-in-progress: true |
| 31 | + |
| 32 | +jobs: |
| 33 | + prepare: |
| 34 | + name: Determine Build Matrix |
| 35 | + runs-on: ubuntu-latest |
| 36 | + timeout-minutes: 2 |
| 37 | + |
| 38 | + outputs: |
| 39 | + ci-builds: ${{ steps.read-builds.outputs.ci-builds }} |
| 40 | + |
| 41 | + steps: |
| 42 | + - name: Checkout |
| 43 | + uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f |
| 44 | + with: |
| 45 | + submodules: true |
| 46 | + |
| 47 | + - name: Read matrix.yaml |
| 48 | + id: read-builds |
| 49 | + run: | |
| 50 | + cat matrix.yaml |
| 51 | + CI_BUILDS=$(yq eval -o json -I 0 matrix.yaml) |
| 52 | + echo "::set-output name=ci-builds::$CI_BUILDS" |
| 53 | +
|
| 54 | + build: |
| 55 | + name: Build and Test |
| 56 | + runs-on: ubuntu-latest |
| 57 | + needs: prepare |
| 58 | + timeout-minutes: 45 |
| 59 | + |
| 60 | + strategy: |
| 61 | + fail-fast: false |
| 62 | + matrix: ${{ fromJson(needs.prepare.outputs.ci-builds) }} |
| 63 | + |
| 64 | + env: |
| 65 | + STACK: "stack --stack-yaml ${{ matrix.build.stackyaml }} --lock-file read-write --resolver ${{ matrix.build.resolver }} --compiler ${{ matrix.build.compiler }}" |
| 66 | + |
| 67 | + ghc-cache-ver: v1 |
| 68 | + dep-cache-ver: v1 |
| 69 | + mod-cache-ver: v1 |
| 70 | + |
| 71 | + steps: |
| 72 | + - name: Checkout |
| 73 | + uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f |
| 74 | + with: |
| 75 | + submodules: true |
| 76 | + |
| 77 | + # Installs the recent protoc, for now unpinned to gain some experience |
| 78 | + - name: Install Protoc |
| 79 | + uses: arduino/setup-protoc@v1 |
| 80 | + with: |
| 81 | + version: '3.x' |
| 82 | + |
| 83 | + # Install Stack without GHC first, so we have an opportunity to cache the |
| 84 | + # Pantry package index. |
| 85 | + - name: Install Stack |
| 86 | + uses: haskell/actions/setup@eb3073b623d53d0cdd38e4d4c2408f14e3fc71c5 |
| 87 | + id: setup-stack |
| 88 | + with: |
| 89 | + enable-stack: true |
| 90 | + stack-no-global: true |
| 91 | + |
| 92 | + # The cache key for the Pantry package index. |
| 93 | + - name: Fetch Hackage Stamp |
| 94 | + run: curl -O https://hackage.haskell.org/timestamp.json |
| 95 | + |
| 96 | + # Try to avoid rebuilding the whole Pantry index, since it takes over a |
| 97 | + # minute to parse and index all the Cabal files on Hackage from scratch. |
| 98 | + # |
| 99 | + # Pantry will try to incrementally extend its index if it gets an |
| 100 | + # out-of-date one, so we benefit from using restore-keys to bootstrap it |
| 101 | + # with a previous index. |
| 102 | + - name: Cache Pantry Package Index |
| 103 | + uses: actions/cache@c64c572235d810460d0d6876e9c705ad5002b353 |
| 104 | + with: |
| 105 | + # Everything except the decompressed Hackage index tarball. Why is |
| 106 | + # that even materialized to disk? |
| 107 | + path: | |
| 108 | + ${{ steps.setup-stack.outputs.stack-root }}/pantry/pantry* |
| 109 | + ${{ steps.setup-stack.outputs.stack-root }}/pantry/hackage/* |
| 110 | + !${{ steps.setup-stack.outputs.stack-root }}/pantry/hackage/*.tar |
| 111 | + !${{ steps.setup-stack.outputs.stack-root }}/pantry/hackage/*.tar.gz |
| 112 | + key: ${{ runner.os }}-${{ matrix.build.compiler }}-${{ hashFiles('timestamp.json') }} |
| 113 | + restore-keys: | |
| 114 | + ${{ runner.os }}-${{ matrix.build.compiler }}- |
| 115 | + ${{ runner.os }}- |
| 116 | +
|
| 117 | + # GHC takes a while to gunzip and configure; let's try caching it |
| 118 | + # pre-configured with zstd. |
| 119 | + - name: Cache Configured GHC |
| 120 | + uses: actions/cache@c64c572235d810460d0d6876e9c705ad5002b353 |
| 121 | + with: |
| 122 | + # Omit the tarball itself; it'll double the cache size assuming |
| 123 | + # similar compression ratios. |
| 124 | + path: | |
| 125 | + ${{ steps.setup-stack.outputs.stack-root }}/programs/*/* |
| 126 | + !${{ steps.setup-stack.outputs.stack-root }}/programs/*/*.tar.xz |
| 127 | + key: ${{ env.ghc-cache-ver}}-${{ runner.os }}-${{ matrix.build.compiler }} |
| 128 | + |
| 129 | + # Now do stack setup, which should do as little cabal-file-indexing work |
| 130 | + # as possible. |
| 131 | + - name: Pre-install GHC with Stack |
| 132 | + run: $STACK setup |
| 133 | + |
| 134 | + # Have stack construct a build plan and lock file for use as a cache key. |
| 135 | + - name: Determine Build Plan |
| 136 | + run: | |
| 137 | + $STACK build --test --dry-run |
| 138 | + echo Lock files: |
| 139 | + echo stack*.lock ${{ hashFiles('stack*.lock') }} |
| 140 | +
|
| 141 | + # Cache pre-built dependencies from the same lock file; restore from |
| 142 | + # other builds with the same GHC version to benefit from any packages in |
| 143 | + # common between two lockfiles. |
| 144 | + - name: Cache Dependencies |
| 145 | + uses: actions/cache@c64c572235d810460d0d6876e9c705ad5002b353 |
| 146 | + with: |
| 147 | + # The Stack global work directory, minus GHC installations (already |
| 148 | + # extracted from a tarball) and the Pantry package index (already |
| 149 | + # cached). |
| 150 | + # |
| 151 | + # Excluding subdirectories of an included directory doesn't work, so |
| 152 | + # glob everything inside $STACK_ROOT and then exclude the |
| 153 | + # subdirectories we don't want. |
| 154 | + path: | |
| 155 | + ${{ steps.setup-stack.outputs.stack-root }}/* |
| 156 | + !${{ steps.setup-stack.outputs.stack-root }}/programs |
| 157 | + !${{ steps.setup-stack.outputs.stack-root }}/pantry |
| 158 | + key: ${{ env.dep-cache-ver }}-${{ runner.os }}-${{ matrix.build.compiler }}-${{ hashFiles('stack*.lock') }} |
| 159 | + restore-keys: | |
| 160 | + ${{ env.dep-cache-ver }}-${{ runner.os }}-${{ matrix.build.compiler }}- |
| 161 | +
|
| 162 | + # We just overwrote some Stack internal files with a cached version that |
| 163 | + # has an older timestamp for the GHC binary, which will confuse it on the |
| 164 | + # next command. Quarantine the warnings in their own step for |
| 165 | + # aesthetics, even though they wouldn't be harmful if we just let them |
| 166 | + # happen in the next build step. |
| 167 | + - name: Refresh GHC Metadata |
| 168 | + run: | |
| 169 | + echo "Re-running setup to let Stack update GHC's timestamp." |
| 170 | + echo "A warning about mismatched GHC metadata is expected." |
| 171 | + echo |
| 172 | + $STACK setup |
| 173 | +
|
| 174 | + # Build the dependencies, hopefully doing little to no work. |
| 175 | + - name: Build Dependencies |
| 176 | + run: $STACK build --test --only-dependencies |
| 177 | + |
| 178 | + # Cache pre-built modules for incremental builds, making sure to keep |
| 179 | + # builds with different dependencies separate. |
| 180 | + - name: Cache Modules |
| 181 | + uses: actions/cache@c64c572235d810460d0d6876e9c705ad5002b353 |
| 182 | + with: |
| 183 | + path: "**/.stack-work" |
| 184 | + key: | |
| 185 | + ${{ env.mod-cache-ver }}-${{ runner.os }}-${{ matrix.build.compiler }}-${{ hashFiles('stack*.lock') }}-${{hashFiles('**/*.hs', '**/package.yaml') }} |
| 186 | + restore-keys: | |
| 187 | + ${{ env.mod-cache-ver }}-${{ runner.os }}-${{ matrix.build.compiler }}-${{ hashFiles('stack*.lock') }}- |
| 188 | + ${{ env.mod-cache-ver }}-${{ runner.os }}-${{ matrix.build.compiler }}- |
| 189 | +
|
| 190 | + # Build, Haddock, and test, taking care to build in only one |
| 191 | + # configuration to make caching work as well as possible. In practice, |
| 192 | + # this means building modules, tests, and documentation all at once, then |
| 193 | + # running tests under the same configuration afterwards. Separating them |
| 194 | + # isn't crucial; it's just to make the CI results easier to understand. |
| 195 | + # |
| 196 | + # Using --no-haddock-deps somehow forces it to rebuild from scratch every |
| 197 | + # time, so leave that out -- rebuilding unconditionally seems worse than |
| 198 | + # building (and caching) dependencies' haddock. |
| 199 | + - name: Build |
| 200 | + run: $STACK build --test --no-run-tests |
| 201 | + |
| 202 | + # Run tests with concurrency disabled; see |
| 203 | + # https://github.com/commercialhaskell/stack/issues/5024. |
| 204 | + - name: Test |
| 205 | + run: $STACK build --test -j 1 |
| 206 | + |
| 207 | + - name: Haddock |
| 208 | + run: $STACK build --haddock --test --no-run-tests |
0 commit comments