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 -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" if (( queue_size > 0 ));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 -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 > 0 ));then curl -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 -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"