tests.yaml 7.9 KB

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