GitHub/rerun

From Omnia
Jump to navigation Jump to search

auto-rerun

something along the lines of:

    steps:
      - name: Rerun failed jobs in the current workflow
        env:
          GH_TOKEN: ${{ github.token }}
        run: gh run rerun ${{ github.run_id }} --failed

test.yml

test.yml:

on: push
jobs:
  fail:
    runs-on: ubuntu-latest
    steps:
      - run: false
  re-run:
    needs: fail
    if: failure() && fromJSON(github.run_attempt) < 3
    runs-on: ubuntu-latest
    steps:
      - env:
          GH_REPO: ${{ github.repository }}
          GH_TOKEN: ${{ github.token }}
          GH_DEBUG: api
        run: gh workflow run rerun.yml -F run_id=${{ github.run_id }}

rerun.yml

on:
  workflow_dispatch:
    inputs:
      run_id:
        required: true
jobs:
  rerun:
    runs-on: ubuntu-latest
    steps:
      - name: rerun ${{ inputs.run_id }}
        env:
          GH_REPO: ${{ github.repository }}
          GH_TOKEN: ${{ github.token }}
          GH_DEBUG: api
        run: |
          gh run watch ${{ inputs.run_id }} > /dev/null 2>&1
          gh run rerun ${{ inputs.run_id }} --failed

mikamika

rerun-failed-jobs:
    runs-on: ubuntu-latest
    needs: [ job1, job2]
    if: failure()
    steps:
      - name: Rerun failed jobs in the current workflow
        env:
          GH_TOKEN: ${{ github.token }}
        run: gh workflow run retry-workflow.yml -F run_id=${{ github.run_id }}

--

name: Retry workflow

on:
    workflow_dispatch:
        inputs:
            run_id:
                required: true
jobs:
    rerun:
        runs-on: ubuntu-latest
        steps:
            - name: rerun ${{ inputs.run_id }}
              env:
                  GH_REPO: ${{ github.repository }}
                  GH_TOKEN: ${{ github.token }}
              run: |
                  gh run watch ${{ inputs.run_id }} > /dev/null 2>&1
                  gh run rerun ${{ inputs.run_id }} --failed


medium.com example

name: Feature Branch - Push

on:
    push:
        branches:
            - "feat-*"

jobs:
    some-other-step:
        runs-on: ubuntu-latest
        steps:
            - name: This step always fails
              run: exit 1

    retry-on-failure:
        # only execute the step on failed workflows
        # do not trigger more than 3 times
        if: failure() && fromJSON(github.run_attempt) < 3

        # run thes step last
        needs: [some-other-step]
        runs-on: ubuntu-latest
        steps:
              run: echo "runs only on failure (for maximum of 3 times)"

Example 2:

# Save this as .github/workflows/retry-workflow.yml

name: Retry workflow

on:
    workflow_dispatch:
        inputs:
            run_id:
                required: true
jobs:
    rerun:
        runs-on: ubuntu-latest
        steps:
            - name: rerun ${{ inputs.run_id }}
              env:
                  GH_REPO: ${{ github.repository }}
                  GH_TOKEN: ${{ github.token }}
              run: |
                  gh run watch ${{ inputs.run_id }} > /dev/null 2>&1
                  gh run rerun ${{ inputs.run_id }} --failed
jobs:
    retry-on-failure:
        if: failure() && fromJSON(github.run_attempt) < 3
        needs: [some-other-step]
        runs-on: ubuntu-latest
        steps:
            - env:
                  GH_REPO: ${{ github.repository }}
                  GH_TOKEN: ${{ github.token }}
              run: gh workflow run retry-workflow.yml -F run_id=${{ github.run_id }}

keywords