Postcommits gate #322
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: Postcommits gate | |
| on: | |
| schedule: | |
| - cron: "*/5 * * * *" # Every 5 minutes | |
| workflow_dispatch: | |
| jobs: | |
| switch_gate: | |
| runs-on: ubuntu-latest | |
| name: SwitchGate | |
| steps: | |
| - name: main | |
| shell: bash | |
| run: | | |
| queue_size=$(curl --retry 5 --retry-all-errors --retry-delay 10 --fail --show-error -Ls -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{github.token}}" -H "X-GitHub-Api-Version: 2022-11-28" \ | |
| https://api.github.com/repos/${{github.repository}}/actions/runs?per_page=1000\&page=1\&status=queued\&event=pull_request_target | \ | |
| jq '[.workflow_runs[] | {name,status,id,updated_at,"waiting_seconds":(now - (.updated_at | fromdate)), html_url}]' | \ | |
| jq '[.[] | select(.waiting_seconds > 60)] | length') | |
| echo "Queue size: $queue_size" | |
| echo "Queue threshold: ${{ vars.POSTCOMMIT_QUEUE_THRESHOLD }}" | |
| if (( queue_size > ${{ vars.POSTCOMMIT_QUEUE_THRESHOLD }} ));then | |
| echo "Close gate for postcommits" | |
| query='.runners[] | select(.labels[].name=="ghrun") | select( any([.labels[].name][]; .=="postcommit")) | .id' | |
| else | |
| echo "Open gate for postcommits" | |
| query='.runners[] | select(.labels[].name=="ghrun") | select( all([.labels[].name][]; .!="postcommit")) | .id' | |
| fi | |
| echo "Requesting a list of runners to update labels..." | |
| j1=$(curl --retry 5 --retry-all-errors --retry-delay 10 --fail --show-error -Ls -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{secrets.GH_PERSONAL_ACCESS_TOKEN}}" \ | |
| -H "X-GitHub-Api-Version: 2022-11-28" -w "%{http_code}\n" \ | |
| https://api.github.com/repos/${{github.repository}}/actions/runners?per_page=100\&page=1) | |
| http_code=$(echo "$j1" | tail -n 1) | |
| echo "Result code: $http_code" | |
| if [ "$http_code" != "200" ];then | |
| echo "HTTP result code is not 200, exiting" | |
| echo "$j1" | |
| exit 1 | |
| fi | |
| # echo "$j1" | |
| echo "$j1" | sed '$d' | jq "$query" | while read x;do | |
| echo "Runner: $x" | |
| if (( queue_size > ${{ vars.POSTCOMMIT_QUEUE_THRESHOLD }} ));then | |
| curl --retry 5 --retry-all-errors --retry-delay 10 --fail --show-error -Ls -X DELETE -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{secrets.GH_PERSONAL_ACCESS_TOKEN}}" \ | |
| -H "X-GitHub-Api-Version: 2022-11-28" \ | |
| https://api.github.com/repos/${{github.repository}}/actions/runners/$x/labels/postcommit | |
| else | |
| curl --retry 5 --retry-all-errors --retry-delay 10 --fail --show-error -Ls -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{secrets.GH_PERSONAL_ACCESS_TOKEN}}" \ | |
| -H "X-GitHub-Api-Version: 2022-11-28" \ | |
| https://api.github.com/repos/${{github.repository}}/actions/runners/$x/labels \ | |
| -d '{"labels":["postcommit"]}' | |
| fi | |
| done | |
| echo "Runner labels synchronized" |