gate_postcommits.yml 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 > 60)] | length')
  18. echo "Queue size: $queue_size"
  19. if (( queue_size > 0 ));then
  20. echo "Close gate for postcommits"
  21. query='.runners[] | select(.labels[].name=="ghrun") | select( any([.labels[].name][]; .=="postcommit")) | .id'
  22. else
  23. echo "Open gate for postcommits"
  24. query='.runners[] | select(.labels[].name=="ghrun") | select( all([.labels[].name][]; .!="postcommit")) | .id'
  25. fi
  26. echo "Requesting a list of runners to update labels..."
  27. j1=$(curl -Ls -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{secrets.GH_PERSONAL_ACCESS_TOKEN}}" \
  28. -H "X-GitHub-Api-Version: 2022-11-28" -w "%{http_code}\n" \
  29. https://api.github.com/repos/${{github.repository}}/actions/runners?per_page=100\&page=1)
  30. http_code=$(echo "$j1" | tail -n 1)
  31. echo "Result code: $http_code"
  32. if [ "$http_code" != "200" ];then
  33. echo "HTTP result code is not 200, exiting"
  34. echo "$j1"
  35. exit 1
  36. fi
  37. # echo "$j1"
  38. echo "$j1" | sed '$d' | jq "$query" | while read x;do
  39. echo "Runner: $x"
  40. if (( queue_size > 0 ));then
  41. curl -Ls -X DELETE -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{secrets.GH_PERSONAL_ACCESS_TOKEN}}" \
  42. -H "X-GitHub-Api-Version: 2022-11-28" \
  43. https://api.github.com/repos/${{github.repository}}/actions/runners/$x/labels/postcommit
  44. else
  45. curl -Ls -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{secrets.GH_PERSONAL_ACCESS_TOKEN}}" \
  46. -H "X-GitHub-Api-Version: 2022-11-28" \
  47. https://api.github.com/repos/${{github.repository}}/actions/runners/$x/labels \
  48. -d '{"labels":["postcommit"]}'
  49. fi
  50. done
  51. echo "Runner labels synchronized"