Google Cloud Build Monitor #45981
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright 2026 Google LLC | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # https://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| name: Google Cloud Build Monitor | |
| on: | |
| check_suite: | |
| types: [completed] | |
| permissions: | |
| contents: read | |
| issues: write | |
| jobs: | |
| create-issue-on-failure: | |
| runs-on: ubuntu-24.04 | |
| # Only alert for a failed GCB build suite on the main branch. | |
| if: > | |
| github.repository == 'googleapis/google-cloud-rust' && github.event.check_suite.app.name == 'Google Cloud Build' && github.event.check_suite.head_branch == 'main' && (github.event.check_suite.conclusion == 'failure' || | |
| github.event.check_suite.conclusion == 'timed_out' || | |
| github.event.check_suite.conclusion == 'cancelled') | |
| steps: | |
| - name: Create CI failure issue | |
| shell: bash | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| GH_REPO: ${{ github.repository }} | |
| CHECK_SUITE_ID: ${{ github.event.check_suite.id }} | |
| COMMIT_SHA: ${{ github.event.check_suite.head_sha }} | |
| run: | | |
| # Enumerate the failed builds in the check suite. | |
| FAILED_BUILDS=$(gh api repos/$GH_REPO/check-suites/$CHECK_SUITE_ID/check-runs --paginate | jq -c '.check_runs[] | select(.conclusion == "failure" or .conclusion == "timed_out" or .conclusion == "cancelled") | {name: .name, url: .details_url}') | |
| if [[ -z "$FAILED_BUILDS" ]]; then | |
| echo "No failed check runs found in check suite $CHECK_SUITE_ID." | |
| exit 0 | |
| fi | |
| BUILD_TEXT="" | |
| while IFS= read -r build; do | |
| if [[ -n "$build" ]]; then | |
| name=$(echo "$build" | jq -r '.name') | |
| url=$(echo "$build" | jq -r '.url') | |
| BUILD_TEXT="$BUILD_TEXT"$'\n'"- [\`$name\`]($url)" | |
| fi | |
| done < <(echo "$FAILED_BUILDS") | |
| # Query GitHub for any PRs associated with this commit | |
| PRS=$(gh api repos/$GH_REPO/commits/$COMMIT_SHA/pulls --jq '.[].number') | |
| PR_TEXT="" | |
| if [[ -n "$PRS" ]]; then | |
| for pr in $PRS; do | |
| PR_TEXT="$PR_TEXT"$'\n'"- #$pr" | |
| done | |
| fi | |
| # Construct the title and body of the issue | |
| TITLE="Post-merge failure" | |
| BODY="**Failure in Google Cloud Build on \`main\`:** | |
| **Failed Builds:** | |
| $BUILD_TEXT | |
| **Commit:** $COMMIT_SHA | |
| **Associated Pull Requests:** | |
| $PR_TEXT" | |
| # Check for an existing open issue with the same title. | |
| EXISTING_ISSUE=$(gh issue list --repo $GH_REPO --search "\"$TITLE\" in:title" --state open --json number --jq '.[0].number') | |
| if [[ -n "$EXISTING_ISSUE" && "$EXISTING_ISSUE" != "null" ]]; then | |
| echo "Found existing open issue #$EXISTING_ISSUE. Adding comment." | |
| gh issue comment "$EXISTING_ISSUE" --repo $GH_REPO --body "$BODY" | |
| else | |
| echo "Creating new issue." | |
| LINK=$(gh issue create --title "$TITLE" --body "$BODY" --label ":rotating_light: critical" --assignee ${GITHUB_ACTOR} -R $GH_REPO) | |
| NUM=${LINK##*/} | |
| gh issue comment $NUM --repo $GH_REPO --body "@googleapis/cloud-sdk-rust-team A critical issue has been created, please respond immediately." | |
| fi |