-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: refactor project configuration and workflows
- Loading branch information
1 parent
cb431e0
commit afa393d
Showing
3 changed files
with
83 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,103 @@ | ||
# .gitHub/workflows/cleanup.yml | ||
name: Cleanup Deployments | ||
# .github/workflows/cleanup.yml | ||
name: Repository Cleanup | ||
|
||
on: | ||
workflow_dispatch: # 允許手動觸發 | ||
workflow_dispatch: | ||
inputs: | ||
action_type: | ||
description: '選擇要執行的操作' | ||
required: true | ||
type: choice | ||
options: | ||
- 'Cleanup Workflow' | ||
- 'Cleanup Deployments' | ||
workflow_status: | ||
description: '要清理的工作流程狀態 (僅在選擇 Cleanup Workflow 時需要)' | ||
required: false | ||
type: choice | ||
options: | ||
- 'disabled' # 已停用的工作流程 | ||
- 'active' # 活躍的工作流程 | ||
- 'all' # 所有工作流程 | ||
environment: | ||
description: '要清理的部署環境 (僅在選擇 Cleanup Deployments 時需要)' | ||
required: false | ||
type: choice | ||
options: | ||
- 'all' | ||
- 'github-pages' | ||
- 'pypi' | ||
|
||
jobs: | ||
cleanup: | ||
cleanup-workflows: | ||
if: ${{ github.event.inputs.action_type == 'Cleanup Workflow' }} | ||
runs-on: ubuntu-latest | ||
permissions: | ||
actions: write | ||
steps: | ||
- name: Cleanup workflows | ||
uses: actions/github-script@v7 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
const status = '${{ github.event.inputs.workflow_status }}'; | ||
console.log(`Cleaning up workflows with status: ${status}`); | ||
// 獲取所有工作流程 | ||
const workflows = await github.rest.actions.listRepoWorkflows({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo | ||
}); | ||
for (const workflow of workflows.data.workflows) { | ||
// 根據選擇的狀態過濾工作流程 | ||
if (status === 'all' || | ||
(status === 'disabled' && !workflow.state === 'active') || | ||
(status === 'active' && workflow.state === 'active')) { | ||
console.log(`Processing workflow: ${workflow.name} (${workflow.state})`); | ||
// 獲取此工作流程的所有運行 | ||
const runs = await github.rest.actions.listWorkflowRuns({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
workflow_id: workflow.id, | ||
}); | ||
// 刪除運行 | ||
console.log(`Found ${runs.data.total_count} runs to delete`); | ||
for (const run of runs.data.workflow_runs) { | ||
console.log(`Deleting run #${run.run_number} of ${workflow.name}`); | ||
await github.rest.actions.deleteWorkflowRun({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
run_id: run.id | ||
}); | ||
} | ||
} | ||
} | ||
console.log('Cleanup completed'); | ||
cleanup-deployments: | ||
if: ${{ github.event.inputs.action_type == 'Cleanup Deployments' }} | ||
runs-on: ubuntu-latest | ||
permissions: | ||
deployments: write | ||
actions: write | ||
contents: write | ||
|
||
steps: | ||
- name: Delete github-pages deployments | ||
if: ${{ github.event.inputs.environment == 'github-pages' || github.event.inputs.environment == 'all' }} | ||
uses: strumwolf/delete-deployment-environment@v2 | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
environment: github-pages | ||
onlyRemoveDeployments: true | ||
|
||
- name: Delete pypi deployments | ||
if: ${{ github.event.inputs.environment == 'pypi' || github.event.inputs.environment == 'all' }} | ||
uses: strumwolf/delete-deployment-environment@v2 | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
environment: pypi | ||
onlyRemoveDeployments: true | ||
onlyRemoveDeployments: true |
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
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