tests.yaml 7.6 KB

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