Skip to content

Conversation

@mdroll
Copy link

@mdroll mdroll commented Nov 19, 2025

See RM #55707

How to test the github change action before merging:

Create an example repository e.g. xyz on github and create a github actions file like this. Either via the github editor or while creating a new file and put it under the .github/workflows/ directory:

/gihub_repo/.github/workflows/main_task.yml - First job - Failing simulation

name: "Main task"

permissions:
  issues: write
  contents: read

on:
  push:
    branches: ['*']
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  main_task:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
      - name: Will fail on purpose
        run: |
          echo "Führe wichtige Tests aus..."
          # Diese Zeile simuliert einen Fehler. Bei Erfolg entfernen Sie 'exit 1'.
          exit 1

  notify_failure:
    uses: ./.github/workflows/notify_failure.yml
    needs: [main_task]
    if: failure()
    with:
      needs_job: main_task

/gihub_repo/.github/workflows/notify_failure.yml - Second job - Issue handling itself

name: Notify Failure

on:
  workflow_call:
    inputs:
      needs_job:
        required: true
        type: string

permissions:
  issues: write
  contents: read

jobs:
  notify_failure:
    runs-on: ubuntu-latest

    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    steps:
      - name: Manage issue
        id: manage_issue
        run: |
          ISSUE_TITLE="[Workflow Log] Central error logging"
          ISSUE_DATA=$(gh issue list -R "$GITHUB_REPOSITORY" --search "is:open in:title \"${ISSUE_TITLE}\"" --json number --limit 1)
          ISSUE_NUMBER=$(echo "$ISSUE_DATA" | jq -r '.[0].number')
          
          if [ -z "$ISSUE_NUMBER" ] || [ "$ISSUE_NUMBER" = "null" ]; then
            NEW_ISSUE_NUMBER=$(gh issue create -R "$GITHUB_REPOSITORY" --title "$ISSUE_TITLE" \
                                               --body "This issue serves as a central log for all workflow errors." \
                                                | sed -n 's/.*#\([0-9]\+\).*/\1/p')
            echo "issue_number=$NEW_ISSUE_NUMBER" >> "$GITHUB_OUTPUT"
          else              
            echo "issue_number=$ISSUE_NUMBER" >> "$GITHUB_OUTPUT"
          fi                   

      - name: Create comment
        run: |
          gh issue comment ${{ steps.manage_issue.outputs.issue_number }} \
             -R "$GITHUB_REPOSITORY" \
             --body "***Workflow failure*** ([View workflow run for details](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}))"

On every commit or push, the GitHub Actions workflow runs:

The first job simulates a failure so that the comment and issue creation job is triggered. It is not part of the actual pull request itself; it only exists to demonstrate how to test the workflow.

The second job performs two important steps:

2.1 The first step checks whether a general issue for workflow runs already exists. If it does not, a new issue is created. The ID of this newly created issue is then passed to the next step.

2.2 In the second step, a comment is posted—using the GitHub CLI, the API, and a token—on the previously found or newly created issue, based on the failed workflow run.

This allows anyone interested to subscribe to the issue and stay informed about workflow failures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants