如何清除仓库部署历史并在GitHub环境区仅显示活跃部署?
如何清除代码仓库的部署历史?
- 进入仓库的Settings页面,在左侧菜单的「Code and automation」分类下找到Environments选项
- 点击目标环境名称进入详情页
- 在页面下方的Deployment history区域,可点击单条部署记录右侧的垃圾桶图标删除单条记录;若需批量删除,可通过以下方式:
- 使用GitHub CLI命令:
gh api -X DELETE repos/{用户名}/{仓库名}/environments/{环境名}/deployments/{部署ID},循环处理所有旧部署ID - 或通过curl调用API:
curl -X DELETE -H "Authorization: token YOUR_TOKEN" https://api.github.com/repos/{用户名}/{仓库名}/environments/{环境名}/deployments/{部署ID}
- 使用GitHub CLI命令:
在GitHub中部署项目后,如何在每次提交更新后仅在环境区域显示活跃部署?
- 配置工作流自动清理:
在GitHub Actions部署工作流文件(如.github/workflows/deploy.yml)中,添加清理旧部署的步骤,示例代码如下:- name: 清理旧部署记录 uses: actions/github-script@v6 with: script: | const deployments = await github.rest.repos.listDeployments({ owner: context.repo.owner, repo: context.repo.repo, environment: '你的环境名称' }); // 仅保留最新的1个活跃部署,删除其余旧部署 const oldDeployments = deployments.data.filter(d => d.id !== context.deployment.id); for (const dep of oldDeployments) { await github.rest.repos.deleteDeployment({ owner: context.repo.owner, repo: context.repo.repo, deployment_id: dep.id }); } - 开启环境自动清理功能:
进入目标环境详情页,找到「Deployment protection rules」下方的Automatic cleanup选项,设置保留的活跃部署数量(例如仅保留1个),新部署完成后旧部署会自动被标记为非活跃并清理
内容的提问来源于stack exchange,提问作者EhsaN Karimi
相关产品推荐
相关产品推荐

