forked from ipdxco/unified-github-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
156 lines (153 loc) · 5.57 KB
/
dispatch.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
name: Dispatch
on:
workflow_call:
inputs:
workflow:
required: true
type: string
inputs:
required: false
default: '{}'
type: string
filter:
required: false
default: 'true'
type: string
dry-run:
required: false
default: false
type: boolean
jobs:
batch:
name: Batch targets
runs-on: ubuntu-latest
outputs:
batches: ${{ steps.matrix.outputs.result }}
steps:
- id: matrix
uses: actions/github-script@v7
env:
FILTER: ${{ inputs.filter }}
with:
github-token: ${{ secrets.UCI_GITHUB_TOKEN }}
retries: 0
script: |
const request = async function(req, opts) {
try {
return await req(opts)
} catch(err) {
opts._attempt = (opts._attempt || 0) + 1
if (err.status === 403) {
if (err.response.headers['x-ratelimit-remaining'] === '0') {
const retryAfter = err.response.headers['x-ratelimit-reset'] - Math.floor(Date.now() / 1000) || 1
core.info(`Rate limit exceeded, retrying in ${retryAfter} seconds`)
await new Promise(resolve => setTimeout(resolve, retryAfter * 1000))
return request(req, opts)
}
if (err.message.toLowerCase().includes('secondary rate limit')) {
const retryAfter = Math.pow(2, opts._attempt)
core.info(`Secondary rate limit exceeded, retrying in ${retryAfter} seconds`)
await new Promise(resolve => setTimeout(resolve, retryAfter * 1000))
return request(req, opts)
}
}
throw err
}
}
github.hook.wrap('request', request)
core.info(`Looking for repositories the user has direct access to`)
const items = await github.paginate(github.rest.repos.listForAuthenticatedUser, {
affiliation: 'collaborator'
})
const maxReposPerWorkflow = 100
const batches = []
let batch = []
for (const item of items) {
if (item.archived) {
core.info(`Skipping archived repository ${item.full_name}`)
continue
}
try {
await exec.exec('jq', ['-e', '-n', `${JSON.stringify(item)} | ${process.env.FILTER}`])
} catch(e) {
core.info(`Skipping repository ${item.full_name} due to filter`)
continue
}
batch.push(item.full_name)
if (batch.length === maxReposPerWorkflow) {
batches.push({
key: batches.length,
value: batch
})
batch = []
}
}
if (batch.length > 0) {
batches.push({
key: batches.length,
value: batch
})
}
return batches
dispatch:
needs: [ batch ]
name: Dispatch workflow(batch ${{ matrix.cfg.key }})
runs-on: ubuntu-latest
permissions:
actions: write
strategy:
fail-fast: false
matrix:
cfg: ${{ fromJSON(needs.batch.outputs.batches) }}
max-parallel: 1
env:
GITHUB_TOKEN: ${{ github.token }}
WORKFLOW: ${{ inputs.workflow }}
REPO: ${{ github.repository }}
steps:
- id: dispatch
name: Dispatch workflow
env:
TARGETS: ${{ toJSON(matrix.cfg.value) }}
INPUTS: ${{ inputs.inputs }}
DRY_RUN: ${{ inputs.dry-run }}
run: |
start_date="$(date +%s)"
args=()
for key in $(jq -r 'keys[]' <<< "$INPUTS"); do
args+=("--field" "$key=$(jq -rc '.["'"$key"'"]' <<< "$INPUTS")")
done
if [[ "$DRY_RUN" == "true" ]]; then
echo "DRY RUN: gh workflow run $WORKFLOW --ref $GITHUB_REF --repo $REPO ${args[@]} --field targets=$TARGETS"
else
gh workflow run "$WORKFLOW" --ref "$GITHUB_REF" --repo "$REPO" ${args[@]} --field "targets=$TARGETS"
fi
echo "start_date=$start_date" | tee -a $GITHUB_OUTPUT
- id: run
name: Wait for workflow run to start
if: inputs.dry-run == false
env:
START_DATE: ${{ steps.dispatch.outputs.start_date }}
run: |
# checks every 3 seconds until the most recent workflow run's created_at is later than this job's start_date
while sleep 3; do
run="$(gh api "/repos/$REPO/actions/workflows/$WORKFLOW/runs?per_page=1" --jq '.workflow_runs[0]')"
# nothing to check if no workflow run was returned
if [[ ! -z "$run" ]]; then
run_start_date="$(date --date="$(jq -r '.created_at' <<< "$run")" +%s)"
if [[ "$run_start_date" > "$START_DATE" ]]; then
echo "id=$(jq -r '.id' <<< "$run")" | tee -a $GITHUB_OUTPUT
break
fi
fi
done
- name: Wait for workflow run to complete
if: inputs.dry-run == false
env:
RUN_ID: ${{ steps.run.outputs.id }}
run: |
# delays checking workflow's run status to save on GH API requests
sleep 60
# checks every 3 seconds until the workflow run's status is completed
# redirects the stdout to /dev/null because it is very chatty
gh run watch "$RUN_ID" --repo "$REPO" > /dev/null