Skip to content

rerun failed jobs #13010

rerun failed jobs

rerun failed jobs #13010

Workflow file for this run

name: rerun failed jobs
on:
workflow_dispatch:
inputs:
run_id:
description: "run id"
required: true
type: string
max_attempts:
description: "max attempts, default is 3"
required: false
default: "3"
type: string
clean_up:
description: "if true, clean up resources"
required: false
default: 'false'
type: choice
options:
- 'true'
- 'false'
schedule:
- cron: "0 11 * * *" # UTC+8 19:00 before any auto pipeline run, to clear all the resources
- cron: "0 1 * * *" # UTC+8 09:00 after the uitest done, to clear all the resources
permissions:
actions: read
jobs:
rerun:
permissions:
actions: write
if: ${{ github.event_name == 'workflow_dispatch' }}
runs-on: ubuntu-latest
env:
DEVTUNNEL_CLIENT_ID: ${{ secrets.TEST_CLEAN_CLIENT_ID }}
DEVTUNNEL_CLIENT_SECRET: ${{ secrets.TEST_CLEAN_CLIENT_SECRET }}
DEVTUNNEL_TENANT_ID: ${{ secrets.TEST_CLEAN_TENANT_ID }}
steps:
- name: wait for 60s
run: |
sleep 60
echo "wait for 60s"
- name: clean devtunnel
run: |
wget https://tunnelsassetsprod.blob.core.windows.net/cli/1.0.1249+67b1cd300c/linux-x64-devtunnel -O ./devtunnel
chmod 777 ./devtunnel
./devtunnel user login --sp-tenant-id ${{env.DEVTUNNEL_TENANT_ID}} --sp-client-id ${{env.DEVTUNNEL_CLIENT_ID}} --sp-secret ${{env.DEVTUNNEL_CLIENT_SECRET}}
./devtunnel delete-all -f
- name: re-run failed jobs
run: |
run=`curl \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.event.inputs.run_id }}`
status=`jq -r '.conclusion' <<< "$run"`
run_attempt=`jq -r '.run_attempt' <<< "$run"`
echo "status: $status"
echo "run_attempt: $run_attempt"
if [[ "$status" == "failure" && "$run_attempt" -lt ${{ github.event.inputs.max_attempts }} ]]; then
echo "[start] rerun failed jobs ${{ github.event.inputs.run_id }} $status $run_attempt..."
curl \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.event.inputs.run_id }}/rerun-failed-jobs
echo "[finish] rerun failed jobs ${{ github.event.inputs.run_id }}"
fi
sleep 60
run=`curl \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.event.inputs.run_id }}`
runningStatus=`jq -r '.status' <<< "$run"`
echo "runningStatus: $runningStatus"
if [[ "$runningStatus" != "queued" && "$runningStatus" != "in_progress" && "$run_attempt" -lt ${{ github.event.inputs.max_attempts }} ]]; then
echo "[start] rerun check..."
curl \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.event.inputs.run_id }}/rerun-failed-jobs
echo "[finish] rerun again..."
fi
build-clean-matrix:
name: Build clean matrix
runs-on: ubuntu-latest
if: ${{ github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && github.event.inputs.clean_up == 'true') }}
outputs:
matrix: ${{ steps.build-matrix.outputs.matrix }}
steps:
- name: Build matrix from M365_USERNAMES variable
id: build-matrix
env:
M365_USERNAMES: ${{ vars.M365_USERNAMES }}
run: |
# Build matrix JSON from comma-separated M365_USERNAMES
matrix="[]"
# Add regular M365 usernames
index=1
IFS=',' read -ra users <<< "$M365_USERNAMES"
for user in "${users[@]}"; do
user=$(echo "$user" | xargs)
if [ -n "$user" ]; then
matrix=$(echo "$matrix" | jq -c --arg name "M365_USERNAME_$index" --arg value "$user" '. + [{name: $name, value: $value}]')
index=$((index+1))
fi
done
clean:
name: Clean resource
needs: build-clean-matrix
runs-on: ubuntu-latest
if: ${{ github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && github.event.inputs.clean_up == 'true') }}
defaults:
run:
working-directory: packages/tests
env:
CLEAN_CLIENT_ID: ${{ vars.TEST_TENANT_CLEAN_CLIENT_ID }}
CLEAN_CLIENT_SECRET: ${{ secrets.TEST_TENANT_CLEAN_CLIENT_SECRET }}
CLEAN_TENANT_ID: ${{ secrets.TEST_TENANT_CLEAN_TENANT_ID }}
AZURE_TENANT_ID: ${{ secrets.TEST_TENANT_TENANT_ID }}
AZURE_SUBSCRIPTION_ID: ${{ secrets.TEST_TENANT_SUBSCRIPTION_ID }}
AZURE_ACCOUNT_NAME: ${{ secrets.TEST_TENANT_AZURE_ACCOUNT_NAME }}
AZURE_ACCOUNT_PASSWORD: ${{ secrets.TEST_TENANT_AZURE_ACCOUNT_PASSWORD }}
M365_ACCOUNT_PASSWORD: ${{ secrets.TEST_TENANT_M365_ACCOUNT_ADMIN_PASSWORD }}
M365_DISPLAY_NAME: "test"
MAIL_API_KEY: ${{ secrets.MAIL_API_KEY }}
TEST_TENANT_M365_ACCOUNT_NAME: ${{ secrets.TEST_TENANT_M365_ACCOUNT_ADMIN_ACCOUNT }}
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.build-clean-matrix.outputs.matrix) }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.ref_name }}
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 22
- uses: pnpm/action-setup@v4
- name: Setup project
working-directory: ./
run: |
npm run setup:e2e
- name: Set M365_ACCOUNT_NAME
run: |
echo "M365_ACCOUNT_NAME=${{ matrix.m365_user.value }}" >> $GITHUB_ENV
- name: Clean resource with ${{ matrix.m365_user.name }}
run: |
npx ts-node src/scripts/clean.ts