# 创建自定义部署保护规则

使用 GitHub Apps 通过第三方系统自动保护部署。

## 先决条件

> \[!NOTE]
> 自定义部署保护规则目前为 公开预览，可能随时更改。

有关部署保护规则的一般信息，请参阅 [使用 GitHub Actions 进行部署](/zh/actions/how-tos/deploy/configure-and-manage-deployments/control-deployments#using-custom-deployment-protection-rules)。

## 使用GitHub Apps创建自定义部署保护规则

1. 创建一个 GitHub App。 有关详细信息，请参阅“[注册GitHub应用](/zh/apps/creating-github-apps/registering-a-github-app/registering-a-github-app)”。 按如下所示配置 GitHub App。
   1. （可选）在“标识和授权用户”下的“回调 URL”文本字段中，输入回调 URL。 有关详细信息，请参阅“[关于用户授权回调 URL](/zh/apps/creating-github-apps/registering-a-github-app/about-the-user-authorization-callback-url)”。
   2. 在“权限”下，选择“存储库权限”。
   3. 在“操作”右侧，单击下拉菜单，然后选择“访问: 只读”。
      新GitHub应用“存储库权限”部分的![截图。 “操作”权限显示“只读”，并以橙色框出。](/assets/images/help/actions/actions-repo-permissions-read-only.png)
   4. 在“部署”右侧，单击下拉菜单，然后选择“访问: 读取和写入”。
      新GitHub应用“存储库权限”部分的![截图。 “部署”权限显示“读取和写入”，并以橙色框出。](/assets/images/help/actions/actions-deployments-repo-permissions-read-and-write.png)
   5. 在“订阅事件”下，选择“部署保护规则”。

![新GitHub应用中“订阅事件”部分的截图。 以橙色框出了“部署保护规则”的复选框。](/assets/images/help/actions/actions-subscribe-to-events-deployment-protection-rules.png)

1. 在存储库中安装自定义部署保护规则并启用它以供使用。 有关详细信息，请参阅“[配置自定义部署保护规则](/zh/actions/how-tos/deploy/configure-and-manage-deployments/configure-custom-protection-rules)”。

## 批准或拒绝部署

工作流到达引用了已启用自定义部署保护规则的环境的作业后，GitHub 会将 `POST` 请求发送到配置的包含 `deployment_protection_rule` 有效负载的 URL。 可以编写部署保护规则，以自动发送基于 `deployment_protection_rule` 有效负载批准或拒绝部署的 REST API 请求。 按如下所示配置 REST API 请求。

当工作流作业的环境设置为 `deployment: false` 时，自定义部署保护规则不兼容。 有关详细信息，请参阅“[使用 GitHub Actions 进行部署](/zh/actions/how-tos/deploy/configure-and-manage-deployments/control-deployments#interaction-with-protection-rules)”。

1. 验证传入的 `POST` 请求。 有关详细信息，请参阅“[验证 Webhook 交付](/zh/webhooks/using-webhooks/validating-webhook-deliveries#validating-webhook-deliveries)”。

2. 使用 JSON Web 令牌以 GitHub App 身份进行身份验证。 有关详细信息，请参阅“[以GitHub应用身份进行身份验证](/zh/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app#about-authentication-as-a-github-app)”。

3. 使用 `deployment_protection_rule` Webhook 有效负载中的安装 ID 生成安装令牌。 有关详细信息，请参阅“[关于使用 GitHub 应用进行身份验证](/zh/apps/creating-github-apps/authenticating-with-a-github-app/about-authentication-with-a-github-app#authentication-as-a-github-app)”。

   ```shell
   curl --request POST \
   --url "https://api.github.com/app/installations/INSTALLATION_ID/ACCESS_TOKENS" \
   --header "Accept: application/vnd.github+json" \
   --header "Authorization: Bearer {jwt}" \
   --header "Content-Type: application/json" \
   --data \
   '{ \
      "repository_ids": [321], \
      "permissions": { \
         "deployments": "write" \
      } \
   }'
   ```

4. 可选地，若要在不对GitHub执行任何其他操作的情况下添加状态报告，请向`POST`发送`/repos/OWNER/REPO/actions/runs/RUN_ID/deployment_protection_rule`请求。 在请求正文中，省略 `state`。 有关详细信息，请参阅“[工作流运行的 REST API 终结点](/zh/rest/actions/workflow-runs#review-custom-deployment-protection-rules-for-a-workflow-run)”。 最多可以针对同一部署发布状态报告 10 次。 状态报告支持 Markdown 格式，长度最多为 1024 个字符。

5. 若要批准或拒绝请求，请向 `POST` 发送 `/repos/OWNER/REPO/actions/runs/RUN_ID/deployment_protection_rule` 请求。 在请求正文中，将 `state` 属性设置为 `approved` 或 `rejected`。 有关详细信息，请参阅“[工作流运行的 REST API 终结点](/zh/rest/actions/workflow-runs#review-custom-deployment-protection-rules-for-a-workflow-run)”。

6. （可选）通过向 `GET` 发送 `/repos/OWNER/REPOSITORY_ID/actions/runs/RUN_ID/approvals` 请求来请求运行工作流的批准状态。 有关详细信息，请参阅“[工作流运行的 REST API 终结点](/zh/rest/actions/workflow-runs#get-the-review-history-for-a-workflow-run)”。

7. （可选）在GitHub上查看部署。 有关详细信息，请参阅“[审查部署](/zh/actions/how-tos/deploy/configure-and-manage-deployments/review-deployments)”。

## 在 GitHub Marketplace 中发布自定义部署保护规则

你可以将其GitHub App发布到GitHub Marketplace该存储库，以便开发人员能够发现合适的保护规则，并将其安装在其GitHub存储库中。 或者，可以浏览现有的自定义部署保护规则以满足需求。 有关详细信息，请参阅 [关于 GitHub 应用市场](/zh/apps/github-marketplace/github-marketplace-overview/about-github-marketplace-for-apps) 和 [在GitHub Marketplace发布应用](/zh/apps/github-marketplace/listing-an-app-on-github-marketplace)。