tests.yaml 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. # Exceptions. See /LICENSE for license information.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. name: Tests
  5. on:
  6. push:
  7. branches: [trunk, action-test]
  8. pull_request:
  9. merge_group:
  10. permissions:
  11. contents: read # For actions/checkout.
  12. pull-requests: read # For dorny/paths-filter to read pull requests.
  13. # Cancel previous workflows on the PR when there are multiple fast commits.
  14. # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#concurrency
  15. concurrency:
  16. group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
  17. cancel-in-progress: true
  18. jobs:
  19. test:
  20. strategy:
  21. matrix:
  22. # On PRs and in the merge queue test a recent version of each supported
  23. # OS. On push (post-submit), also run on `macos-12` to get Intel macOS
  24. # coverage.
  25. runner:
  26. ${{ fromJSON(github.event_name != 'push' && '["ubuntu-22.04",
  27. "macos-14"]' || '["ubuntu-22.04", "macos-14", "macos-12"]') }}
  28. build_mode: [fastbuild, opt]
  29. include:
  30. # The clang-tidy config doesn't work on macos (missing `truncate`).
  31. - runner: ubuntu-22.04
  32. build_mode: clang-tidy
  33. runs-on: ${{ matrix.runner }}
  34. steps:
  35. - name: Harden Runner
  36. uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1
  37. with:
  38. egress-policy: block
  39. # When adding endpoints, see README.md.
  40. # prettier-ignore
  41. allowed-endpoints: >
  42. *.dl.sourceforge.net:443
  43. api.github.com:443
  44. bcr.bazel.build:443
  45. downloads.sourceforge.net:443
  46. github.com:443
  47. mirrors.kernel.org:443
  48. nodejs.org:443
  49. oauth2.googleapis.com:443
  50. objects.githubusercontent.com:443
  51. pypi.org:443
  52. releases.bazel.build:443
  53. sourceforge.net:443
  54. storage.googleapis.com:443
  55. # Checkout the pull request head or the branch.
  56. - name: Checkout pull request
  57. if: github.event_name == 'pull_request'
  58. uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
  59. with:
  60. ref: ${{ github.event.pull_request.head.sha }}
  61. - name: Checkout branch
  62. if: github.event_name != 'pull_request'
  63. uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
  64. # Tests should only run on applicable paths, but we still need to have an
  65. # action run for the merge queue. We filter steps based on the paths here,
  66. # and condition steps on the output.
  67. - id: filter
  68. uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
  69. with:
  70. filters: |
  71. has_code:
  72. - '!{**/*.md,LICENSE,CODEOWNERS,.git*}'
  73. # Disable uploads when the remote cache is read-only.
  74. - name: Set up remote cache access (read-only)
  75. if:
  76. steps.filter.outputs.has_code == 'true' && github.event_name ==
  77. 'pull_request'
  78. run: |
  79. echo "remote_cache_upload=--remote_upload_local_results=false" \
  80. >> $GITHUB_ENV
  81. # Provide a cache key when the remote cache is read-write.
  82. - name: Set up remote cache access (read-write)
  83. if:
  84. steps.filter.outputs.has_code == 'true' && github.event_name !=
  85. 'pull_request'
  86. env:
  87. REMOTE_CACHE_KEY: ${{ secrets.CARBON_BUILDS_GITHUB }}
  88. run: |
  89. echo "$REMOTE_CACHE_KEY" | base64 -d > $HOME/remote_cache_key.json
  90. echo "remote_cache_upload=--google_credentials=$HOME/remote_cache_key.json" \
  91. >> $GITHUB_ENV
  92. - uses: ./.github/actions/build-setup-common
  93. if: steps.filter.outputs.has_code == 'true'
  94. with:
  95. matrix_runner: ${{ matrix.runner }}
  96. remote_cache_upload: ${{ env.remote_cache_upload }}
  97. # Just for visibility, print space before and after the build.
  98. - name: Disk space before build
  99. if: steps.filter.outputs.has_code == 'true'
  100. run: df -h
  101. - name: Verify MODULE.bazel.lock
  102. if: steps.filter.outputs.has_code == 'true'
  103. run: |
  104. exit_code=0
  105. ./scripts/run_bazel.py \
  106. --attempts=5 \
  107. mod deps --lockfile_mode=error || exit_code=$?
  108. if (( $exit_code != 0 )); then
  109. ./scripts/run_bazel.py \
  110. --attempts=5 \
  111. mod deps --lockfile_mode=update
  112. echo "MODULE.bazel.lock is out of date! Use below file for update."
  113. echo "Platforms may require merging output, for example by applying"
  114. echo "an update, re-running triggers, and applying the next update."
  115. echo "============================================================"
  116. cat MODULE.bazel.lock
  117. echo "============================================================"
  118. exit 1
  119. fi
  120. # Build and run all targets on branch pushes to ensure we always have a
  121. # clean tree. We don't expect this to be an interactive path and so don't
  122. # optimize the latency of this step.
  123. - name: Compute impacted pull request targets (for push)
  124. if:
  125. steps.filter.outputs.has_code == 'true' && github.event_name == 'push'
  126. env:
  127. TARGETS_FILE: ${{ runner.temp }}/targets
  128. run: |
  129. echo "//..." >$TARGETS_FILE
  130. # Compute the set of possible rules impacted by this change using
  131. # Bazel-based diffing. This lets PRs and the merge queue have a much more
  132. # efficient test CI action by avoiding even enumerating (and downloading)
  133. # all of the unaffected Bazel targets.
  134. - name: Compute impacted pull request targets
  135. if:
  136. steps.filter.outputs.has_code == 'true' && github.event_name != 'push'
  137. env:
  138. # Compute the base SHA from the different event structures.
  139. GIT_BASE_SHA:
  140. ${{ github.event_name == 'pull_request' &&
  141. github.event.pull_request.base.sha ||
  142. github.event.merge_group.base_sha }}
  143. TARGETS_FILE: ${{ runner.temp }}/targets
  144. run: |
  145. # First fetch the relevant base into the git repository.
  146. git fetch --depth=1 origin $GIT_BASE_SHA
  147. # Then use `target-determinator` as wrapped by our script.
  148. ./scripts/target_determinator.py $GIT_BASE_SHA >$TARGETS_FILE
  149. # Bazel requires a test target to run the test command. There may be
  150. # no targets or there may only be non-test targets that we want to
  151. # build, so simply inject an explicit no-op test target.
  152. echo "//scripts:no_op_test" >> $TARGETS_FILE
  153. # Build and run just the tests impacted by the PR or merge group.
  154. - name: Test (${{ matrix.build_mode }})
  155. if:
  156. steps.filter.outputs.has_code == 'true' && matrix.build_mode !=
  157. 'clang-tidy'
  158. env:
  159. # 'libtool_check_unique failed to generate' workaround.
  160. # https://github.com/bazelbuild/bazel/issues/14113#issuecomment-999794586
  161. BAZEL_USE_CPP_ONLY_TOOLCHAIN: 1
  162. TARGETS_FILE: ${{ runner.temp }}/targets
  163. run: |
  164. # Decrease the jobs sharply if we see repeated failures to try to
  165. # work around transient network errors even if it makes things
  166. # slower.
  167. ./scripts/run_bazel.py \
  168. --attempts=5 --jobs-on-last-attempt=4 \
  169. test -c ${{ matrix.build_mode }} \
  170. --target_pattern_file=$TARGETS_FILE
  171. # Run in the clang-tidy config. This is done as part of tests so that we
  172. # aren't duplicating bazel/llvm setup.
  173. #
  174. # The `-k` flag is used to print all clang-tidy errors.
  175. - name: clang-tidy
  176. if:
  177. steps.filter.outputs.has_code == 'true' && matrix.build_mode ==
  178. 'clang-tidy'
  179. env:
  180. TARGETS_FILE: ${{ runner.temp }}/targets
  181. run: |
  182. ./scripts/run_bazel.py \
  183. --attempts=5 \
  184. build --config=clang-tidy -k \
  185. --target_pattern_file=$TARGETS_FILE
  186. # See "Disk space before build".
  187. - name: Disk space after build
  188. if: steps.filter.outputs.has_code == 'true'
  189. run: df -h