简体   繁体   English

Github 操作:合并拉取请求后将主分支部署到受保护环境

[英]Github Actions: Deploy main branch to protected environment after pull request is merged

In our github repository, we have set up a protected environment named Sandbox , for which the main branch is the only allowed deployment branch.在我们的 github 存储库中,我们设置了一个名为Sandbox的受保护环境, main分支是唯一允许的部署分支。 Now we want to deploy automatically to that environment if a pullrequest is merged into main (and the if the pullrequest in addition bears the label "Sandbox").现在,如果 pullrequest 合并到 main 中(并且 pullrequest 还带有 label“沙箱”),我们希望自动部署到该环境。

Our workflow is roughly as follows:我们的工作流程大致如下:

name: Pull Request Merged

concurrency:
  group: ${{ github.ref }}

on:
  pull_request:
    types: [closed]

jobs:
  deploy_to_sandbox:
    if: |
      github.event.pull_request.merged == true && 
      contains(github.event.pull_request.labels.*.name, 'Sandbox')
    name: Deploy to Sandbox
    uses: ./.github/workflows/deploy.yml
    with:
      environment: Sandbox
    secrets: inherit

The workflow is triggered as expected upon merging a PR, but somehow it tries to deploy from the feature branch instead of deploying from main.工作流在合并 PR 时按预期触发,但不知何故它试图从功能分支部署而不是从 main 部署。 Since the environment is protected, the deployment fails accordingly.由于环境受到保护,因此部署失败。 How can we achieve that the deployment uses the target branch (ie , main ) that was merged into, instead of the source branch?我们如何实现部署使用合并到的目标分支(即main ),而不是源分支?

There's no way to specify that a workflow should be triggered when a pull request is merged.无法指定在合并拉取请求时应触发工作流。 the reason why it's the feature branch that gets deployed is because it's the one that triggers the workflow.部署功能分支的原因是因为它是触发工作流的分支。 However, because a merged pull request always results in a push, you can use the push event to accomplish your goal.但是,由于合并的拉取请求总是导致推送,您可以使用推送事件来实现您的目标。

For example, let's say that you want to run a workflow whenever a pull request is merged to your main branch.例如,假设您希望在拉取请求合并到主分支时运行工作流。 You can do something like this:你可以这样做:

on:
  push:
    branches:
      - main

also if you want to prevent push directly to main it's part of github pro plan.另外,如果你想防止直接推送到 main 它是 github 专业计划的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM