forked from Homebrew/homebrew-core
-
Notifications
You must be signed in to change notification settings - Fork 1
110 lines (97 loc) · 3.38 KB
/
recreate-linux-runners.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
name: Recreate Linux self-hosted runners
on:
workflow_dispatch:
schedule:
# Once each 24 hours, at 1 during the night
- cron: "0 1 * * *"
workflow_run:
workflows:
- CI
types:
- completed
concurrency:
group: recreate-linux-runners-${{ github.event.workflow_run.id || github.ref }}
cancel-in-progress: true
permissions:
contents: read
env:
GH_REPO: ${{github.repository}}
GH_NO_UPDATE_NOTIFIER: 1
GH_PROMPT_DISABLED: 1
jobs:
check:
if: >
github.repository_owner == 'Homebrew' &&
(github.event_name != 'workflow_run' || github.event.workflow_run.event == 'pull_request')
runs-on: ubuntu-latest
outputs:
recreate: ${{ steps.check.outputs.recreate }}
permissions:
actions: read # for `gh run download`
pull-requests: read # for `gh api`
steps:
- uses: Homebrew/actions/find-related-workflow-run-id@master
if: github.event_name == 'workflow_run'
with:
run-id: ${{ github.event.workflow_run.id }}
workflow-name: Triage tasks
- name: Download `event_payload` artifact
if: github.event_name == 'workflow_run'
uses: Homebrew/actions/gh-try-download@master
with:
artifact-name: event_payload
workflow-id: ${{ env.workflow_run_id }}
- name: Check if runner needs to be recreated
id: check
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
recreate=true
if [[ "$GITHUB_EVENT_NAME" = "workflow_run" ]]
then
PR="$(jq --raw-output .number event.json)"
recreate="$(
gh api \
--header 'Accept: application/vnd.github+json' \
--header 'X-GitHub-Api-Version: 2022-11-28' \
"repos/{owner}/{repo}/pulls/$PR" \
--jq 'any(.labels[].name; .== "CI-linux-self-hosted")'
)"
fi
echo "recreate=$recreate" >> "$GITHUB_OUTPUT"
recreate:
needs: check
if: >
github.repository_owner == 'Homebrew' &&
(github.event_name != 'workflow_run' || github.event.workflow_run.event == 'pull_request') &&
fromJson(needs.check.outputs.recreate)
runs-on: ubuntu-latest
env:
# TODO agree on one label for all runners
RUNNER_LABEL: TODO
strategy:
matrix:
runner_name:
- linux-self-hosted-1
steps:
- name: Set up Cloud SDK
uses: google-github-actions/[email protected]
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}
service_account_key: ${{ secrets.GCP_SA_KEY }}
export_default_credentials: true
- name: Wait for idle runner
id: killable
uses: Homebrew/actions/wait-for-idle-runner@master
with:
runner_name: ${{ matrix.runner_name }}
github_token: ${{ secrets.HOMEBREW_GITHUB_PUBLIC_REPO_TOKEN }}
- name: Kill runner and create a new one
if: ${{ steps.killable.outputs.runner-found == 'true' && steps.killable.outputs.runner-idle == 'true' }}
uses: Homebrew/actions/create-gcloud-instance@master
with:
runner_name: ${{ matrix.runner_name }}
gcp_project_id: ${{ secrets.GCP_PROJECT_ID }}
gcp_service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
gcp_sa_key: ${{ secrets.GCP_SA_KEY }}
github_token: ${{ secrets.HOMEBREW_GITHUB_PUBLIC_REPO_TOKEN }}