Skip to content

.github/workflows/cleanup-branch.yml #4

.github/workflows/cleanup-branch.yml

.github/workflows/cleanup-branch.yml #4

on:
schedule:
- cron: "0 0 * * 5" # 매주 금요일 00:00 UTC
workflow_dispatch:
permissions:
contents: write
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
const days = 7;
const cutoff = new Date(Date.now() - days * 24 * 60 * 60 * 1000);
const branches = await github.paginate(
'GET /repos/{owner}/{repo}/branches',
{ owner, repo }
);
for (const branch of branches) {
if (branch.name === 'main' || branch.name === 'dev') continue;
const commits = await github.request(
'GET /repos/{owner}/{repo}/commits',
{
owner,
repo,
sha: branch.name,
per_page: 1
}
);
const lastCommitDate = new Date(
commits.data[0].commit.committer.date
);
if (lastCommitDate < cutoff) {
console.log(`Deleting ${branch.name}`);
await github.request(
'DELETE /repos/{owner}/{repo}/git/refs/heads/{branch}',
{
owner,
repo,
branch: branch.name
}
);
}
}