如何删除Github环境

mat*_*ode 21 github heroku heroku-pipelines

我的问题是关于清理Github存储库中的"Environments"选项卡.

我之前通过Heroku部署,使用两个独立的Github分支(一个用于分段,一个用于生产)进行自动部署.

这在存储库中创建了一个"环境"选项卡,其中显示了两个Heroku环境 - 完全符合预期.

一旦我开始深入研究Heroku管道,我现在已经将应用程序配置为从暂存升级到生产,因此生产环境不再从分支自动部署.

我的Github仓库上的Environments选项卡无法删除我不再使用的环境.我似乎无法在Github或Heroku上找到任何地方让Github"忘记"这个部署环境.

我希望我的问题足够清楚; 如果我能详细说明,请告诉我.

spe*_*ico 43

我也制作了一个小网页/脚本,以自动化该过程(我没有安装 Python,而且我没有看到其他人已经制作了一个脚本),这是在线的,将您的信息放入该过程中自动地。

Stackblitz - Github 部署删除器

2020 年 7 月 18 日编辑:我也将脚本从 Stackblitz 复制到此处的本地片段代码中,以防 Stackblitz 消失:

// RECOMMENDED: Disconnect HEROKU from Github before doing this (though not strictly necessary, I think).
//See https://stackoverflow.com/a/61272173/6569950 for more info.

// PARAMETERS
const TOKEN = ""; // MUST BE `repo_deployments` authorized
const REPO = "your-repo"; // e.g. "monorepo"
const USER_OR_ORG = "your-name"; // e.g. "your-name"

// GLOBAL VARS
const URL = `https://api.github.com/repos/${USER_OR_ORG}/${REPO}/deployments`;
const AUTH_HEADER = `token ${TOKEN}`;

// UTILITY FUNCTIONS
const getAllDeployments = () =>
  fetch(`${URL}`, {
    headers: {
      authorization: AUTH_HEADER
    }
  }).then(val => val.json());

const makeDeploymentInactive = id =>
  fetch(`${URL}/${id}/statuses`, {
    method: "POST",
    body: JSON.stringify({
      state: "inactive"
    }),
    headers: {
      "Content-Type": "application/json",
      Accept: "application/vnd.github.ant-man-preview+json",
      authorization: AUTH_HEADER
    }
  }).then(() => id);

const deleteDeployment = id =>
  fetch(`${URL}/${id}`, {
    method: "DELETE",
    headers: {
      authorization: AUTH_HEADER
    }
  }).then(() => id);

// MAIN
getAllDeployments()
  .catch(console.error)
  .then(res => {
    console.log(`${res.length} deployments found`);
    return res;
  })
  .then(val => val.map(({
    id
  }) => id))
  .then(ids => Promise.all(ids.map(id => makeDeploymentInactive(id))))
  .then(res => {
    console.log(`${res.length} deployments marked as "inactive"`);
    return res;
  })
  .then(ids => Promise.all(ids.map(id => deleteDeployment(id))))
  .then(res => {
    console.log(`${res.length} deployments deleted`);
    return res;
  })
  .then(finalResult => {
    const appDiv = document.getElementById("app");
    appDiv.innerHTML = `
<h1>CLEANUP RESULT</h1>
<br>
Removed Deployments: ${finalResult.length}
<br>
<br>Ids:<br>
${JSON.stringify(finalResult)}
<br><br><br><br><br><br>
  <p>(Open up the console)</p>
`;
  });
Run Code Online (Sandbox Code Playgroud)
h1,
h2 {
  font-family: Lato;
}
Run Code Online (Sandbox Code Playgroud)
<div id="app">
  <h1>Github Deployment's Cleaner</h1>
  <p> You need to put the parameters in!</p>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 您可以从 https://github.com/settings/tokens 生成一个。记得添加“repo_deployments”权限 (4认同)
  • 如何获得代币? (3认同)
  • 此外,该脚本仅获取 30 个最近的部署。为了访问旧的,您必须在获取部署时将 `?per_page=100` 参数添加到 URL。如果您的部署超过最近 100 个,则必须分页:https://docs.github.com/en/rest/guides/traversing-with-pagination。 (3认同)
  • 请注意,该脚本会自动启动并删除所有部署。作为问题作者,我只想删除特定环境,即该环境的所有部署。因此,我必须调整脚本以按“.environment”属性过滤部署。 (2认同)
  • 为了简单起见,我创建了一个基于 @spersico js 片段的网页,我还添加了一个选项来保留最后一次部署(以防万一):https://githubdeploycleaner.herokuapp.com 代码在这里: https://githubdeploycleaner.herokuapp.com/res/cleaner.js (2认同)

Cad*_*nce 37

它似乎没有 UI,但您可以使用 GitHub API 来完成。

在执行此操作之前,您可能应该断开 GitHub 和 Heroku 的连接。

首先,转到您的 GitHub 帐户设置,然后是开发人员设置,然后是个人访问令牌。创建一个允许 repo_deployments 的新令牌。生成后,保存十六进制令牌,您将需要它用于即将到来的 API 请求。

对于这些示例,我将假设您的用户名是$aaaa,您的存储库名称是$bbbb,您的访问令牌是$tttt。将这些替换为您的实际用户名和存储库名称以及访问令牌。或者只是使用 shell 变量来存储实际值,这样您就可以直接粘贴代码块。

首先,列出存储库中的所有部署:

curl https://api.github.com/repos/$aaaa/$bbbb/deployments
Run Code Online (Sandbox Code Playgroud)

每个部署都有一个id整数。记下它,并$iiii在接下来的代码块中替换为该 ID。或者为它创建另一个 shell 变量。

现在您必须为该部署创建一个“非活动”状态:

curl https://api.github.com/repos/$aaaa/$bbbb/deployments/$iiii/statuses -X POST -d '{"state":"inactive"}' -H 'accept: application/vnd.github.ant-man-preview+json' -H "authorization: token $tttt"
Run Code Online (Sandbox Code Playgroud)

现在您可以永久删除部署:

curl https://api.github.com/repos/$aaaa/$bbbb/deployments/$iiii -X DELETE -H "authorization: token $tttt"
Run Code Online (Sandbox Code Playgroud)

如果您有多个部署,请发送第一个请求以查看剩余的所有部署,然后您也可以根据需要删除它们。

删除所有部署后,GitHub 存储库上的环境按钮将消失。

信息来源于GitHub 部署文档GitHub oauth 文档。这对我有用。


Vad*_*est 21

这不会回答OP问题,我一开始以为它会回答,但它的行为并不像我预期的那样。因此,我将此答案添加为社区维基。

GitHub 似乎有两种“环境”概念,OP 的意思是“公共环境”,但 GitHub 似乎也有某种“私有环境”。

我将我的经验添加为下面的答案,因为它确实令人困惑。


您可以通过“设置 > 环境”访问“私有环境”。(例如https://github.com/UnlyEd/next-right-now/settings/environments:)

在此输入图像描述

然后您可以删除每个环境。它将提示确认对话框。确认后,环境将被破坏。

在此输入图像描述

我删除了“暂存”和“生产”环境。

在此输入图像描述

但公共环境及其所有部署仍然继续存在。(这不是OP想要的)

公共环境仍然包含“登台”和“生产”。

  • 这个已经不存在了吗? (5认同)
  • 不幸的是,环境选项卡是企业独有的功能。 (2认同)

Dee*_*hSH 8

我希望您已经得到了问题的答案这个答案适用于那些不想从 GitHub 存储库中删除“环境”选项卡的人

\n

我认为

\n
  • 如果您想删除屏幕右侧显示的环境选项卡
\n
  • 如果您想删除特定环境
\n

删除环境选项卡

\n删除整个选项卡\n
  1. 在您的存储库页面上单击“设置”按钮
  2. \n取消选中环境标记
\n

它将删除右侧的“环境”选项卡

\n

删除特定环境

\n用于删除特定环境\n
  1. 点击设置
  2. 单击环境
  3. 在这里您可以删除特定环境
\n
\n

GitHub 对从私有仓库中删除环境的回​​复

\n
Hi Deekshith,\n\nThanks for writing in!\n\nAs it turns out, the Environments feature is an enterprise-only feature for private repositories, however, they still get created as a side effect of Deployments in private repositories.\n\nhttps://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment\n\nThis means that though you\'d be able to create Deployments in a private repository, you won\'t be able to manage (e.g delete, add rules e.t.c) the deployment Environments. I\'m afraid this is currently the expected behaviour. Our engineers are still discussing how best to help users who\'d want to delete environments in non-enterprise private repositories, such as in your case. We\xe2\x80\x99ll be sure to update you as soon as there\xe2\x80\x99s any news to share.\n\nSorry that we could not be of more help with this -- please let us know if you have any questions at all!\n\nRegards,\nPeter\nGitHub Support\n\n
Run Code Online (Sandbox Code Playgroud)\n


Vis*_*esh 7

我制作了一个交互式 python 脚本,它可以按名称(这是我的问题)或所有部署删除特定环境。检查一下,让我知道它是否适合你:https : //github.com/VishalRamesh50/Github-Environment-Cleaner

这实际上也将删除您的所有部署,即使您有超过 30 个与此处的其他脚本不同的部署,因为它通过来自 Github 的 API 的分页响应数据并且不只使用第一页。


Fab*_*oni 6

根据@Cadence 的回答,我构建了以下 bash 脚本。只需设置适当的参数并让它运行即可。

令牌需要repo_deploymentOAuth 范围。

env=asd
token=asd
repo=asd
user=asd

for id in $(curl -u $user:$token https://api.github.com/repos/$user/$repo/deployments\?environment\=$env | jq ".[].id"); do
    curl -X POST -u $user:$token -d '{"state":"inactive"}' -H 'accept: application/vnd.github.ant-man-preview+json' https://api.github.com/repos/$user/$repo/deployments/$id/statuses
    curl -X DELETE -u $user:$token https://api.github.com/repos/$user/$repo/deployments/$id
done
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。这需要安装 [jq](https://stedolan.github.io/jq/): `sudo apt install jq` (2认同)

Jaa*_*aap 5

我构建了一个在线工具来帮助删除部署

我遇到了同样的问题,发现 @spersico 的代码非常方便,但需要更多的工具/反馈。我迭代了 @spersico 代码以添加一些前端。与 @spersico 的版本相同,所有调用都是在客户端进行的(并且在控制台/网络日志中可见)。

项目在 Github 上开源,并在 Netlify 上有一个可以立即使用的托管版本: https: //github-deployment-cleaner.netlify.app/