ci-round-build.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env bash
  2. # Build the current work tree for Windows on GitHub Actions and put the result in OUT (default build-win).
  3. # Run it from the root of a checkout. The build covers committed work, changes to tracked files and new files under
  4. # the source folders; nothing else in the tree is sent. Exits with the run's result.
  5. # The pushed tree is public: don't use this for anything that must stay private.
  6. set -euo pipefail
  7. repo="${LYNX_CI_REPO:-Ka1-S/LYNX-SHARE}"
  8. out="${OUT:-build-win}"
  9. limit="${LYNX_CI_LIMIT:-10}"
  10. workflow="${LYNX_CI_WORKFLOW:-$(dirname "$(readlink -f "$0")")/../.github/workflows/round-build.yml}"
  11. branch="ci/$(basename "$PWD")-$$"
  12. # the tree to build, made with a private index so the work tree and its staging stay as they are
  13. index="$(mktemp)"
  14. trap 'rm -f "$index"' EXIT
  15. cp "$(git rev-parse --git-path index)" "$index"
  16. export GIT_INDEX_FILE="$index"
  17. git add -u
  18. for dir in src tests resources scripts tools third_party cmake; do
  19. [[ -d "$dir" ]] && git add "$dir"
  20. done
  21. if ! git cat-file -e "HEAD:.github/workflows/round-build.yml" 2>/dev/null; then
  22. blob="$(git hash-object -w "$workflow")"
  23. git update-index --add --cacheinfo "100644,$blob,.github/workflows/round-build.yml"
  24. fi
  25. tree="$(git write-tree)"
  26. unset GIT_INDEX_FILE
  27. # LYNX_CI_RELEASE_OPT=1: the release's code generation (real -O3 + ThinLTO, round 190kz) instead of the fast default
  28. msg="Round build of $(git rev-parse --short HEAD)"
  29. [[ "${LYNX_CI_RELEASE_OPT:-0}" == 1 ]] && msg="$msg [release-opt]"
  30. sha="$(git commit-tree "$tree" -p HEAD -m "$msg")"
  31. # LYNX_CI_FORBIDDEN (optional, set on the machine): a text that must never leave it. Never put its value in this file.
  32. if [[ -n "${LYNX_CI_FORBIDDEN:-}" ]] && git grep -qIi -e "$LYNX_CI_FORBIDDEN" "$sha" --; then
  33. echo "round build: the tree holds a forbidden string, not pushing" >&2
  34. exit 4
  35. fi
  36. running_builds() {
  37. gh run list -R "$repo" --limit 60 --json status,workflowName \
  38. -q '[.[] | select(.workflowName == "Round build" and .status != "completed")] | length'
  39. }
  40. # leave room for the project's own CI
  41. until [[ "$(running_builds 2>/dev/null || echo "$limit")" -lt "$limit" ]]; do sleep 60; done
  42. trap 'rm -f "$index"; git push -q origin --delete "$branch" 2>/dev/null || true' EXIT
  43. # large or busy pushes can time out (HTTP 408): retry, and say so plainly if it never goes through
  44. pushed=""
  45. for attempt in 1 2 3 4 5; do
  46. if git -c http.postBuffer=524288000 push -q origin "$sha:refs/heads/$branch" 2>"$index.err"; then pushed=1; break; fi
  47. tail -2 "$index.err" >&2
  48. sleep $((attempt * 30))
  49. done
  50. rm -f "$index.err"
  51. [[ -n "$pushed" ]] || { echo "round build: push failed, NOTHING WAS BUILT, $out is unchanged" >&2; exit 5; }
  52. run=""
  53. for _ in $(seq 60); do
  54. run="$(gh run list -R "$repo" --branch "$branch" --limit 5 --json databaseId,workflowName \
  55. -q '[.[] | select(.workflowName == "Round build")][0].databaseId // empty' 2>/dev/null || true)"
  56. [[ -n "$run" ]] && break
  57. sleep 5
  58. done
  59. [[ -n "$run" ]] || { echo "round build: no run started for $branch" >&2; exit 3; }
  60. echo "https://github.com/$repo/actions/runs/$run"
  61. until [[ "$(gh run view "$run" -R "$repo" --json status -q .status 2>/dev/null || true)" == completed ]]; do sleep 30; done
  62. conclusion="$(gh run view "$run" -R "$repo" --json conclusion -q .conclusion)"
  63. if [[ "$conclusion" != success ]]; then
  64. gh run view "$run" -R "$repo" --log-failed 2>/dev/null | grep -E 'error:|Error |FAILED|fatal' | tail -40 >&2 || true
  65. echo "round build: $conclusion" >&2
  66. exit 1
  67. fi
  68. mkdir -p "$out"
  69. gh run download "$run" -R "$repo" -n build-win -D "$out.dl$$"
  70. for f in "$out.dl$$"/*; do mv -f "$f" "$out/"; done
  71. rmdir "$out.dl$$"
  72. ls -la "$out/LYNX.exe"