Last active
August 20, 2024 04:34
-
-
Save MinhOmega/9c72f3db2f332725145ca590e7201447 to your computer and use it in GitHub Desktop.
A GitHub Action that checks for running workflows every 5 minutes and triggers the some workflow if none are in progress.
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
| name: Check and Trigger Batch Creation | |
| on: | |
| workflow_dispatch: | |
| schedule: | |
| - cron: '*/20 * * * *' # Runs every 20 minutes | |
| env: | |
| WORKFLOW_FILE_NAME: 'main.yml' # Workflow file to trigger | |
| jobs: | |
| check-and-trigger: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check for running workflows | |
| id: check_workflows | |
| run: | | |
| # Get the list of running workflows | |
| response=$(curl -s -H "Authorization: token ${{ secrets.PAT }}" \ | |
| "https://api.github.com/repos/${{ github.repository }}/actions/runs?status=in_progress") | |
| # Parse the total count of running workflows | |
| running_count=$(echo $response | jq '.total_count') | |
| echo "Total running workflows: $running_count" | |
| echo "::set-output name=running_count::$running_count" | |
| - name: Trigger Workflow | |
| if: steps.check_workflows.outputs.running_count == '0' | |
| run: | | |
| curl -X POST \ | |
| -H "Accept: application/vnd.github.v3+json" \ | |
| -H "Authorization: token ${{ secrets.PAT }}" \ | |
| https://api.github.com/repos/${{ github.repository }}/actions/workflows/${{ env.WORKFLOW_FILE_NAME }}/dispatches \ | |
| -d '{"ref":"main"}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment