gate_postcommits.yml 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. name: Postcommits gate
  2. on:
  3. schedule:
  4. - cron: "*/5 * * * *" # Every 5 minutes
  5. workflow_dispatch:
  6. jobs:
  7. switch_gate:
  8. runs-on: ubuntu-latest
  9. name: SwitchGate
  10. steps:
  11. - name: main
  12. shell: bash
  13. run: |
  14. queue_size=$(curl -Ls -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{github.token}}" -H "X-GitHub-Api-Version: 2022-11-28" \
  15. https://api.github.com/repos/${{github.repository}}/actions/runs?per_page=1000\&page=1\&status=queued\&event=pull_request_target | \
  16. jq '[.workflow_runs[] | {name,status,id,updated_at,"waiting_seconds":(now - (.updated_at | fromdate)), html_url}]' | \
  17. jq '[.[] | select(.waiting_seconds > 600)] | length')
  18. echo "Queue size: $queue_size"
  19. # temporary solution for https://www.githubstatus.com/incidents/m70hk23gx3nx (some workflows are uncancellable stuck)
  20. if (( queue_size > 0 ));then
  21. echo "Close gate for postcommits"
  22. query='.runners[] | select(.labels[].name=="ghrun") | select( any([.labels[].name][]; .=="postcommit")) | .id'
  23. else
  24. echo "Open gate for postcommits"
  25. query='.runners[] | select(.labels[].name=="ghrun") | select( all([.labels[].name][]; .!="postcommit")) | .id'
  26. fi
  27. echo "Requesting a list of runners to update labels..."
  28. j1=$(curl -Ls -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{secrets.GH_PERSONAL_ACCESS_TOKEN}}" \
  29. -H "X-GitHub-Api-Version: 2022-11-28" -w "%{http_code}\n" \
  30. https://api.github.com/repos/${{github.repository}}/actions/runners?per_page=100\&page=1)
  31. http_code=$(echo "$j1" | tail -n 1)
  32. echo "Result code: $http_code"
  33. if [ "$http_code" != "200" ];then
  34. echo "HTTP result code is not 200, exiting"
  35. echo "$j1"
  36. exit 1
  37. fi
  38. # echo "$j1"
  39. echo "$j1" | sed '$d' | jq "$query" | while read x;do
  40. echo "Runner: $x"
  41. # temporary solution for https://www.githubstatus.com/incidents/m70hk23gx3nx (some workflows are uncancellable stuck)
  42. if (( queue_size > 0 ));then
  43. curl -Ls -X DELETE -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{secrets.GH_PERSONAL_ACCESS_TOKEN}}" \
  44. -H "X-GitHub-Api-Version: 2022-11-28" \
  45. https://api.github.com/repos/${{github.repository}}/actions/runners/$x/labels/postcommit
  46. else
  47. curl -Ls -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{secrets.GH_PERSONAL_ACCESS_TOKEN}}" \
  48. -H "X-GitHub-Api-Version: 2022-11-28" \
  49. https://api.github.com/repos/${{github.repository}}/actions/runners/$x/labels \
  50. -d '{"labels":["postcommit"]}'
  51. fi
  52. done
  53. echo "Runner labels synchronized"