|
| 1 | +# Contains jobs corresponding to stats, including build stats due to changes in a PR. |
| 2 | + |
| 3 | +name: Stats Checks & Reports |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_dispatch: |
| 7 | + schedule: |
| 8 | + - cron: "30 02 * * *" |
| 9 | + |
| 10 | +permissions: |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + find_open_pull_requests: |
| 15 | + name: Find open PRs |
| 16 | + runs-on: ubuntu-20.04 |
| 17 | + outputs: |
| 18 | + matrix: ${{ steps.compute-pull-request-matrix.outputs.matrix }} |
| 19 | + env: |
| 20 | + GH_TOKEN: ${{ github.token }} |
| 21 | + steps: |
| 22 | + - uses: actions/checkout@v4 |
| 23 | + |
| 24 | + - name: Compute PR matrix |
| 25 | + id: compute-pull-request-matrix |
| 26 | + # Remove spaces to ensure the matrix output is on one line. Reference: |
| 27 | + # https://stackoverflow.com/a/3232433. |
| 28 | + run: | |
| 29 | + CURRENT_OPEN_PR_INFO="$(gh pr list --json number,baseRefName,headRefName,headRepository,headRepositoryOwner | tr -d '[:space:]')" |
| 30 | + echo "matrix={\"prInfo\": $CURRENT_OPEN_PR_INFO}" >> "$GITHUB_OUTPUT" |
| 31 | +
|
| 32 | + build_stats: |
| 33 | + name: Build Stats |
| 34 | + needs: find_open_pull_requests |
| 35 | + runs-on: ubuntu-20.04 |
| 36 | + # Reduce parallelization due to high build times, and allow individual PRs to fail. |
| 37 | + strategy: |
| 38 | + fail-fast: false |
| 39 | + max-parallel: 5 |
| 40 | + matrix: ${{ fromJson(needs.find_open_pull_requests.outputs.matrix) }} |
| 41 | + env: |
| 42 | + ENABLE_CACHING: false |
| 43 | + CACHE_DIRECTORY: ~/.bazel_cache |
| 44 | + steps: |
| 45 | + - name: Compute PR head owner/repo reference |
| 46 | + env: |
| 47 | + PR_HEAD_REPO: ${{ matrix.prInfo.headRepository.name }} |
| 48 | + PR_HEAD_REPO_OWNER: ${{ matrix.prInfo.headRepositoryOwner.login }} |
| 49 | + run: | |
| 50 | + echo "PR_HEAD=$PR_HEAD_REPO_OWNER/$PR_HEAD_REPO" >> "$GITHUB_ENV" |
| 51 | + - name: Print PR information for this run |
| 52 | + env: |
| 53 | + PR_BASE_REF_NAME: ${{ matrix.prInfo.baseRefName }} |
| 54 | + PR_HEAD_REF_NAME: ${{ matrix.prInfo.headRefName }} |
| 55 | + PR_NUMBER: ${{ matrix.prInfo.number }} |
| 56 | + run: | |
| 57 | + echo "PR $PR_NUMBER is merging into $PR_BASE_REF_NAME from https://github.com/$PR_HEAD branch $PR_HEAD_REF_NAME." |
| 58 | +
|
| 59 | + - name: Set up JDK 9 |
| 60 | + uses: actions/setup-java@v1 |
| 61 | + with: |
| 62 | + java-version: 9 |
| 63 | + |
| 64 | + - name: Set up Bazel |
| 65 | + uses: abhinavsingh/setup-bazel@v3 |
| 66 | + with: |
| 67 | + version: 4.0.0 |
| 68 | + |
| 69 | + # For reference on this & the later cache actions, see: |
| 70 | + # https://github.com/actions/cache/issues/239#issuecomment-606950711 & |
| 71 | + # https://github.com/actions/cache/issues/109#issuecomment-558771281. Note that these work |
| 72 | + # with Bazel since Bazel can share the most recent cache from an unrelated build and still |
| 73 | + # benefit from incremental build performance (assuming that actions/cache aggressively removes |
| 74 | + # older caches due to the 5GB cache limit size & Bazel's large cache size). |
| 75 | + - uses: actions/cache@v2 |
| 76 | + id: cache |
| 77 | + with: |
| 78 | + path: ${{ env.CACHE_DIRECTORY }} |
| 79 | + key: ${{ runner.os }}-${{ env.CACHE_DIRECTORY }}-bazel-binary-${{ github.sha }} |
| 80 | + restore-keys: | |
| 81 | + ${{ runner.os }}-${{ env.CACHE_DIRECTORY }}-bazel-binary- |
| 82 | + ${{ runner.os }}-${{ env.CACHE_DIRECTORY }}-bazel- |
| 83 | +
|
| 84 | + # This check is needed to ensure that Bazel's unbounded cache growth doesn't result in a |
| 85 | + # situation where the cache never updates (e.g. due to exceeding GitHub's cache size limit) |
| 86 | + # thereby only ever using the last successful cache version. This solution will result in a |
| 87 | + # few slower CI actions around the time cache is detected to be too large, but it should |
| 88 | + # incrementally improve thereafter. |
| 89 | + - name: Ensure cache size |
| 90 | + env: |
| 91 | + BAZEL_CACHE_DIR: ${{ env.CACHE_DIRECTORY }} |
| 92 | + run: | |
| 93 | + # See https://stackoverflow.com/a/27485157 for reference. |
| 94 | + EXPANDED_BAZEL_CACHE_PATH="${BAZEL_CACHE_DIR/#\~/$HOME}" |
| 95 | + CACHE_SIZE_MB=$(du -smc $EXPANDED_BAZEL_CACHE_PATH | grep total | cut -f1) |
| 96 | + echo "Total size of Bazel cache (rounded up to MBs): $CACHE_SIZE_MB" |
| 97 | + # Use a 4.5GB threshold since actions/cache compresses the results, and Bazel caches seem |
| 98 | + # to only increase by a few hundred megabytes across changes for unrelated branches. This |
| 99 | + # is also a reasonable upper-bound (local tests as of 2021-03-31 suggest that a full build |
| 100 | + # of the codebase (e.g. //...) from scratch only requires a ~2.1GB uncompressed/~900MB |
| 101 | + # compressed cache). |
| 102 | + if [[ "$CACHE_SIZE_MB" -gt 4500 ]]; then |
| 103 | + echo "Cache exceeds cut-off; resetting it (will result in a slow build)" |
| 104 | + rm -rf $EXPANDED_BAZEL_CACHE_PATH |
| 105 | + fi |
| 106 | +
|
| 107 | + - name: Configure Bazel to use a local cache |
| 108 | + env: |
| 109 | + BAZEL_CACHE_DIR: ${{ env.CACHE_DIRECTORY }} |
| 110 | + run: | |
| 111 | + EXPANDED_BAZEL_CACHE_PATH="${BAZEL_CACHE_DIR/#\~/$HOME}" |
| 112 | + echo "Using $EXPANDED_BAZEL_CACHE_PATH as Bazel's cache path" |
| 113 | + echo "build --disk_cache=$EXPANDED_BAZEL_CACHE_PATH" >> $HOME/.bazelrc |
| 114 | + shell: bash |
| 115 | + |
| 116 | + # This checks out the actual true develop branch separately to ensure that the stats check is |
| 117 | + # run from the latest develop rather than the base branch (which might be different for |
| 118 | + # chained PRs). |
| 119 | + - name: Check out develop repository |
| 120 | + uses: actions/checkout@v4 |
| 121 | + with: |
| 122 | + path: develop |
| 123 | + |
| 124 | + - name: Set up build environment |
| 125 | + uses: ./develop/.github/actions/set-up-android-bazel-build-environment |
| 126 | + |
| 127 | + - name: Check Bazel environment |
| 128 | + run: | |
| 129 | + cd develop |
| 130 | + bazel info |
| 131 | +
|
| 132 | + - name: Check out base repository and branch |
| 133 | + env: |
| 134 | + PR_BASE_REF_NAME: ${{ matrix.prInfo.baseRefName }} |
| 135 | + uses: actions/checkout@v4 |
| 136 | + with: |
| 137 | + fetch-depth: 0 |
| 138 | + ref: ${{ env.PR_BASE_REF_NAME }} |
| 139 | + path: base |
| 140 | + |
| 141 | + - name: Check out head repository and branch |
| 142 | + env: |
| 143 | + PR_HEAD_REF_NAME: ${{ matrix.prInfo.headRefName }} |
| 144 | + uses: actions/checkout@v4 |
| 145 | + with: |
| 146 | + fetch-depth: 0 |
| 147 | + repository: ${{ env.PR_HEAD }} |
| 148 | + ref: ${{ env.PR_HEAD_REF_NAME }} |
| 149 | + path: head |
| 150 | + |
| 151 | + # Note that Bazel is shutdown between builds since multiple Bazel servers will otherwise end |
| 152 | + # up being active (due to multiple repositories being used) and this can quickly overwhelm CI |
| 153 | + # worker resources. |
| 154 | + - name: Build Oppia dev, alpha, beta, and GA (feature branch) |
| 155 | + run: | |
| 156 | + cd head |
| 157 | + git log -n 1 |
| 158 | + bazel build -- //:oppia_dev //:oppia_alpha //:oppia_beta //:oppia_ga |
| 159 | + cp bazel-bin/oppia_dev.aab ../develop/oppia_dev_with_changes.aab |
| 160 | + cp bazel-bin/oppia_alpha.aab ../develop/oppia_alpha_with_changes.aab |
| 161 | + cp bazel-bin/oppia_beta.aab ../develop/oppia_beta_with_changes.aab |
| 162 | + cp bazel-bin/oppia_ga.aab ../develop/oppia_ga_with_changes.aab |
| 163 | + bazel shutdown |
| 164 | +
|
| 165 | + - name: Build Oppia dev, alpha, beta, and GA (base branch) |
| 166 | + run: | |
| 167 | + cd base |
| 168 | + git log -n 1 |
| 169 | + bazel build -- //:oppia_dev //:oppia_alpha //:oppia_beta //:oppia_ga |
| 170 | + cp bazel-bin/oppia_dev.aab ../develop/oppia_dev_without_changes.aab |
| 171 | + cp bazel-bin/oppia_alpha.aab ../develop/oppia_alpha_without_changes.aab |
| 172 | + cp bazel-bin/oppia_beta.aab ../develop/oppia_beta_without_changes.aab |
| 173 | + cp bazel-bin/oppia_ga.aab ../develop/oppia_ga_without_changes.aab |
| 174 | + bazel shutdown |
| 175 | +
|
| 176 | + - name: Run stats analysis tool (develop branch) |
| 177 | + run: | |
| 178 | + cd develop |
| 179 | + git log -n 1 |
| 180 | + bazel run //scripts:compute_aab_differences -- \ |
| 181 | + $(pwd)/brief_build_summary.log $(pwd)/full_build_summary.log \ |
| 182 | + dev $(pwd)/oppia_dev_without_changes.aab $(pwd)/oppia_dev_with_changes.aab \ |
| 183 | + alpha $(pwd)/oppia_alpha_without_changes.aab $(pwd)/oppia_alpha_with_changes.aab \ |
| 184 | + beta $(pwd)/oppia_beta_without_changes.aab $(pwd)/oppia_beta_with_changes.aab \ |
| 185 | + ga $(pwd)/oppia_ga_without_changes.aab $(pwd)/oppia_ga_with_changes.aab |
| 186 | +
|
| 187 | + # Reference: https://github.com/peter-evans/create-or-update-comment#setting-the-comment-body-from-a-file. |
| 188 | + # Also, for multi-line env values, see: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings. |
| 189 | + - name: Extract reports for uploading & commenting |
| 190 | + env: |
| 191 | + PR_NUMBER: ${{ matrix.prInfo.number }} |
| 192 | + id: compute-comment-body |
| 193 | + run: | |
| 194 | + { |
| 195 | + echo 'comment_body<<EOF' |
| 196 | + cat $GITHUB_WORKSPACE/develop/brief_build_summary.log |
| 197 | + echo EOF |
| 198 | + } >> "$GITHUB_OUTPUT" |
| 199 | + FULL_BUILD_SUMMARY_FILE_NAME="full_build_summary_pr_$PR_NUMBER.log" |
| 200 | + FULL_BUILD_SUMMARY_FILE_PATH="$GITHUB_WORKSPACE/develop/$FULL_BUILD_SUMMARY_FILE_NAME" |
| 201 | + echo "FULL_BUILD_SUMMARY_FILE_NAME=$FULL_BUILD_SUMMARY_FILE_NAME" >> "$GITHUB_ENV" |
| 202 | + echo "FULL_BUILD_SUMMARY_FILE_PATH=$FULL_BUILD_SUMMARY_FILE_PATH" >> "$GITHUB_ENV" |
| 203 | + cp "$GITHUB_WORKSPACE/develop/full_build_summary.log" "$FULL_BUILD_SUMMARY_FILE_PATH" |
| 204 | +
|
| 205 | + - name: Add build stats summary comment |
| 206 | + env: |
| 207 | + PR_NUMBER: ${{ matrix.prInfo.number }} |
| 208 | + uses: peter-evans/create-or-update-comment@v1 |
| 209 | + with: |
| 210 | + issue-number: ${{ env.PR_NUMBER }} |
| 211 | + body: ${{ steps.compute-comment-body.outputs.comment_body }} |
| 212 | + |
| 213 | + - uses: actions/upload-artifact@v2 |
| 214 | + with: |
| 215 | + name: ${{ env.FULL_BUILD_SUMMARY_FILE_NAME }} |
| 216 | + path: ${{ env.FULL_BUILD_SUMMARY_FILE_PATH }} |
0 commit comments