|
| 1 | +#! /usr/bin/env bash |
| 2 | + |
| 3 | +set -e |
| 4 | +set -ux |
| 5 | + |
| 6 | +seed="$(date)-$RANDOM" |
| 7 | + |
| 8 | +log="${MAGIC_NIX_CACHE_DAEMONDIR}/daemon.log" |
| 9 | + |
| 10 | +flakehub_binary_cache=https://cache.flakehub.com |
| 11 | +gha_binary_cache=http://127.0.0.1:37515 |
| 12 | + |
| 13 | +is_gh_throttled() { |
| 14 | + grep 'GitHub Actions Cache throttled Magic Nix Cache' "${log}" |
| 15 | +} |
| 16 | + |
| 17 | +# Check that the action initialized correctly. |
| 18 | +grep 'FlakeHub cache is enabled' "${log}" |
| 19 | +grep 'Using cache' "${log}" |
| 20 | + |
| 21 | +if [ "$EXPECT_GITHUB_CACHE" == "true" ]; then |
| 22 | + grep 'GitHub Action cache is enabled' "${log}" |
| 23 | +else |
| 24 | + grep 'Native GitHub Action cache is disabled' "${log}" |
| 25 | +fi |
| 26 | + |
| 27 | +# Build something. |
| 28 | +outpath=$(nix-build .github/workflows/cache-tester.nix --argstr seed "$seed") |
| 29 | + |
| 30 | +# Wait until it has been pushed succesfully. |
| 31 | +found= |
| 32 | +for ((i = 0; i < 60; i++)); do |
| 33 | + sleep 1 |
| 34 | + if grep "✅ $(basename "${outpath}")" "${log}"; then |
| 35 | + found=1 |
| 36 | + break |
| 37 | + fi |
| 38 | +done |
| 39 | +if [[ -z $found ]]; then |
| 40 | + echo "FlakeHub push did not happen." >&2 |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +if [ "$EXPECT_GITHUB_CACHE" == "true" ]; then |
| 45 | + found= |
| 46 | + for ((i = 0; i < 60; i++)); do |
| 47 | + sleep 1 |
| 48 | + if grep "Uploaded '${outpath}' to the GitHub Action Cache" "${log}"; then |
| 49 | + found=1 |
| 50 | + break |
| 51 | + fi |
| 52 | + done |
| 53 | + if [[ -z $found ]]; then |
| 54 | + echo "GitHub Actions Cache push did not happen." >&2 |
| 55 | + |
| 56 | + if ! is_gh_throttled; then |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + fi |
| 60 | +fi |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +# Check the FlakeHub binary cache to see if the path is really there. |
| 65 | +nix path-info --store "${flakehub_binary_cache}" "${outpath}" |
| 66 | + |
| 67 | +if [ "$EXPECT_GITHUB_CACHE" == "true" ] && ! is_gh_throttled; then |
| 68 | + # Check the GitHub binary cache to see if the path is really there. |
| 69 | + nix path-info --store "${gha_binary_cache}" "${outpath}" |
| 70 | +fi |
| 71 | + |
| 72 | +rm ./result |
| 73 | +nix store delete "${outpath}" |
| 74 | +if [ -f "$outpath" ]; then |
| 75 | + echo "$outpath still exists? can't test" |
| 76 | + exit 1 |
| 77 | +fi |
| 78 | + |
| 79 | +rm -rf ~/.cache/nix |
| 80 | + |
| 81 | +echo "-------" |
| 82 | +echo "Trying to substitute the build again..." |
| 83 | +echo "if it fails, the cache is broken." |
| 84 | + |
| 85 | +# Check the FlakeHub binary cache to see if the path is really there. |
| 86 | +nix path-info --store "${flakehub_binary_cache}" "${outpath}" |
| 87 | + |
| 88 | +if [ "$EXPECT_GITHUB_CACHE" == "true" ] && ! is_gh_throttled; then |
| 89 | + # Check the FlakeHub binary cache to see if the path is really there. |
| 90 | + nix path-info --store "${gha_binary_cache}" "${outpath}" |
| 91 | +fi |
| 92 | + |
| 93 | +nix-store --realize -vvvvvvvv "$outpath" |
0 commit comments