cleanup #368
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2024 The Authors (see AUTHORS file) | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
name: 'cleanup' | |
on: | |
schedule: | |
- cron: '0 12 * * *' | |
workflow_dispatch: | |
jobs: | |
remove-old-workflows: | |
runs-on: 'ubuntu-latest' | |
permissions: | |
contents: 'read' | |
id-token: 'write' | |
steps: | |
- id: 'minty-auth' | |
uses: 'google-github-actions/auth@6fc4af4b145ae7821d527454aa9bd537d1f2dc5f' # ratchet:google-github-actions/auth@v2 | |
with: | |
create_credentials_file: false | |
export_environment_variables: false | |
workload_identity_provider: '${{ vars.TOKEN_MINTER_WIF_PROVIDER }}' | |
service_account: '${{ vars.TOKEN_MINTER_WIF_SERVICE_ACCOUNT }}' | |
token_format: 'id_token' | |
id_token_audience: '${{ vars.TOKEN_MINTER_SERVICE_AUDIENCE }}' | |
id_token_include_email: true | |
- id: 'mint-token' | |
uses: 'abcxyz/github-token-minter/.github/actions/minty@main' # ratchet:exclude | |
with: | |
id_token: '${{ steps.minty-auth.outputs.id_token }}' | |
service_url: '${{ vars.TOKEN_MINTER_SERVICE_URL }}' | |
requested_permissions: |- | |
{ | |
"scope": "pkg-cleanup", | |
"repositories": [], | |
"permissions": { | |
"actions": "write", | |
"workflows": "write" | |
} | |
} | |
- name: 'Remove old workflows' | |
uses: 'actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea' # ratchet:actions/github-script@v7 | |
with: | |
github-token: '${{ steps.mint-token.outputs.token }}' | |
script: |- | |
const today = new Date(); | |
const cutoff = new Date(new Date().setDate(today.getDate() - 30)); | |
const cutoffString = cutoff.toISOString().split('T')[0]; | |
const repos = await github.paginate(github.rest.repos.listForOrg, { | |
org: context.repo.owner, | |
per_page: 100, | |
}); | |
let workflowRuns = []; | |
for(const repo of repos) { | |
const runs = await github.paginate(github.rest.actions.listWorkflowRunsForRepo, { | |
owner: context.repo.owner, | |
repo: repo.name, | |
created: `< ${cutoffString}`, | |
exclude_pull_requests: true, | |
per_page: 100, | |
}); | |
for (const run of runs) { | |
if(['action_required', 'in_progress', 'queued', 'requested', 'waiting', 'pending'].includes(run.status)) { | |
continue; | |
} | |
workflowRuns.push({ | |
repo_name: repo.name, | |
run_id: run.id, | |
}); | |
} | |
} | |
core.info(`Found ${workflowRuns.length} runs to delete...`) | |
for(const run of workflowRuns) { | |
core.info(`Deleting ${run.repo_name}/${run.run_id}`); | |
await github.rest.actions.deleteWorkflowRun({ | |
owner: context.repo.owner, | |
repo: run.repo_name, | |
run_id: run.run_id, | |
}) | |
} |