Skip to content

Commit 9ff1dca

Browse files
authored
ci(.github): extract issue creation on failure into a reusable action (#4880)
This change modifies Librarian to extract the inline shell script logic for creating GitHub issues on workflow failures into a reusable composite action. Fixes #4631
1 parent 63a5ecf commit 9ff1dca

4 files changed

Lines changed: 75 additions & 35 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: 'Create Issue on Failure'
2+
description: 'Creates or updates a GitHub issue when a workflow fails'
3+
inputs:
4+
title:
5+
description: "The title of the issue to create or search for."
6+
required: true
7+
body:
8+
description: "The body content for a new issue."
9+
required: true
10+
labels:
11+
description: "Comma-separated list of labels to apply to a new issue."
12+
required: false
13+
default: ":rotating_light: critical"
14+
assignee:
15+
description: "The GitHub username to assign the issue to."
16+
required: false
17+
default: ${{ github.actor }}
18+
mention:
19+
description: "A user or team to mention in a follow-up comment if a new issue is created."
20+
required: false
21+
default: "@googleapis/cloud-sdk-librarian-team"
22+
runs:
23+
using: "composite"
24+
steps:
25+
- name: Create or comment on issue
26+
shell: bash
27+
run: "${{ github.action_path }}/create-issue-on-failure.sh"
28+
env:
29+
GITHUB_TOKEN: ${{ github.token }}
30+
GITHUB_REPO: ${{ github.repository }}
31+
GITHUB_SERVER_URL: ${{ github.server_url }}
32+
GITHUB_RUN_ID: ${{ github.run_id }}
33+
ISSUE_TITLE: ${{ inputs.title }}
34+
ISSUE_BODY: ${{ inputs.body }}
35+
ISSUE_LABELS: ${{ inputs.labels }}
36+
ISSUE_ASSIGNEE: ${{ inputs.assignee }}
37+
ISSUE_MENTION: ${{ inputs.mention }}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
# Search for an existing open issue with the same title
6+
EXISTING_ISSUE=$(gh issue list --repo "$GITHUB_REPO" --search "$ISSUE_TITLE in:title" --state open --json number --jq '.[0].number')
7+
8+
if [[ -n "$EXISTING_ISSUE" && "$EXISTING_ISSUE" != "null" ]]; then
9+
echo "Found existing open issue #$EXISTING_ISSUE. Adding comment."
10+
gh issue comment "$EXISTING_ISSUE" --repo "$GITHUB_REPO" --body "The build failed again. Workflow Run: $GITHUB_SERVER_URL/$GITHUB_REPO/actions/runs/$GITHUB_RUN_ID"
11+
else
12+
echo "Creating new issue."
13+
ISSUE_LINK=$(gh issue create \
14+
--title "$ISSUE_TITLE" \
15+
--body "$ISSUE_BODY" \
16+
--label "$ISSUE_LABELS" \
17+
--assignee "$ISSUE_ASSIGNEE" \
18+
--repo "$GITHUB_REPO")
19+
20+
if [[ -n "$ISSUE_MENTION" ]]; then
21+
ISSUE_NUM=${ISSUE_LINK##*/}
22+
gh issue comment "$ISSUE_NUM" --repo "$GITHUB_REPO" --body "$ISSUE_MENTION The workflow failed. Workflow Run: $GITHUB_SERVER_URL/$GITHUB_REPO/actions/runs/$GITHUB_RUN_ID"
23+
fi
24+
fi

.github/workflows/librarian.yaml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,8 @@ jobs:
7575
needs: [go-generate, legacylibrarian, test]
7676
if: ${{ always() && contains(needs.*.result, 'failure') && github.event_name == 'push' && github.ref == 'refs/heads/main' }}
7777
steps:
78-
- name: Create an issue for push event to main
79-
run: |
80-
ISSUE_TITLE="all: tests failed at HEAD"
81-
BODY="Tests failed at HEAD of the \`main\` branch. Please review Workflow Run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} for detail."
82-
ISSUE_LINK=$(gh issue create --title "$ISSUE_TITLE" --body "$BODY" --label ":rotating_light: critical" --assignee ${{ github.actor }} -R $GH_REPO)
83-
ISSUE_NUM=${ISSUE_LINK##*/}
84-
gh issue comment $ISSUE_NUM --body "@googleapis/cloud-sdk-librarian-team A critical issue has been created, please respond immediately."
85-
env:
86-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
87-
GH_REPO: ${{ github.repository }}
78+
- uses: actions/checkout@v6
79+
- uses: ./.github/actions/create-issue-on-failure
80+
with:
81+
title: "all: tests failed at HEAD"
82+
body: "Tests failed at HEAD of the `main` branch. Please review Workflow Run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} for detail."

.github/workflows/rust.yaml

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,31 +78,15 @@ jobs:
7878
needs: [test, integration]
7979
if: ${{ always() && contains(needs.*.result, 'failure') && github.event_name == 'push' && github.ref == 'refs/heads/main' }}
8080
steps:
81-
- name: Create an issue for push event to main
82-
run: |
83-
ISSUE_TITLE="google-cloud-rust: librarian generate fails when run at HEAD"
84-
BODY="The Rust CI failed on the \`main\` branch.
85-
86-
Running \`librarian generate --all\` on the [google-cloud-rust](https://github.com/googleapis/google-cloud-rust) repository using \`librarian\` at HEAD failed.
87-
88-
Note: This integration test only runs as a postsubmit test due to its long execution time.
81+
- uses: actions/checkout@v6
82+
- uses: ./.github/actions/create-issue-on-failure
83+
with:
84+
title: "google-cloud-rust: librarian generate fails when run at HEAD"
85+
body: |
86+
The Rust CI failed on the `main` branch.
8987
90-
Workflow Run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
88+
Running `librarian generate --all` on the [google-cloud-rust](https://github.com/googleapis/google-cloud-rust) repository using `librarian` at HEAD failed.
9189
92-
# Check for an existing open issue
93-
EXISTING_ISSUE=$(gh issue list --repo $GH_REPO --search "$ISSUE_TITLE in:title" --state open --json number --jq '.[0].number')
90+
Note: This integration test only runs as a postsubmit test due to its long execution time.
9491
95-
if [[ -n "$EXISTING_ISSUE" && "$EXISTING_ISSUE" != "null" ]]; then
96-
echo "Found existing open issue #$EXISTING_ISSUE. Adding comment."
97-
gh issue comment "$EXISTING_ISSUE" --repo $GH_REPO --body "The build failed again. Workflow Run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
98-
else
99-
echo "Creating new issue."
100-
ISSUE_LINK=$(gh issue create --title "$ISSUE_TITLE" --body "$BODY" --label ":rotating_light: critical" --assignee ${{ github.actor }} -R $GH_REPO)
101-
ISSUE_NUM=${ISSUE_LINK##*/}
102-
gh issue comment $ISSUE_NUM --body "@googleapis/cloud-sdk-librarian-team A critical issue has been created, please respond immediately."
103-
fi
104-
env:
105-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
106-
GH_REPO: ${{ github.repository }}
107-
TEST_RESULT: ${{ needs.test.result }}
108-
INTEGRATION_RESULT: ${{ needs.integration.result }}
92+
Workflow Run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}

0 commit comments

Comments
 (0)