amazon-web-services - 如何为合并的 GitHub 拉取请求触发 CodePipeline?

标签 amazon-web-services github terraform aws-codepipeline aws-codebuild

如何配置 CodePipeline 以在打开、编辑或合并拉取请求时触发?

这是一个 Terraform 配置:

resource "aws_codepipeline_webhook" "gh_to_codepipeline_integration" {
  name            = "gh_to_codepipeline_integration"
  authentication  = "GITHUB_HMAC"
  target_action   = "Source"
  target_pipeline = aws_codepipeline.mycodepipeline.name

  authentication_configuration {
    secret_token = var.github_webhook_secret
  }

  // accept pull requests
  // Is there a way to filter on the PR being closed and merged?  This isn't it...
  filter {
    json_path    = "$.action"
    match_equals = "closed"
  }

}

CodePipeline 设置为接受具有过滤器中指定的所有条件的 webhook 事件,对应于 Pull Request Events .

请注意,GitHub 文档说明 action领域PullRequestEvent (我用粗体强调):

The action that was performed. Can be one of assigned, unassigned, review_requested, review_request_removed, labeled, unlabeled, opened, edited, closed, ready_for_review, locked, unlocked, or reopened. If the action is closed and the merged key is false, the pull request was closed with unmerged commits. If the action is closed and the merged key is true, the pull request was merged. While webhooks are also triggered when a pull request is synchronized, Events API timelines don't include pull request events with the synchronize action.



似乎我需要同时过滤 $.action==closed && $.pull_request_merged=true ,但看起来我不能两者兼得。如果我只是过滤 $.action==closed那么如果 PR 关闭而不合并,我的管道将重建。这是我的疏忽,还是 CodePipelines 的触发器不如 CodeBuild 项目灵活?

最佳答案

对于打开/更新的拉取请求,因为 CodePipeline 的 Git 集成需要一个分支名称,这不是 native 支持的,因为分支名称是可变的,除非您在长时间运行的分支上打开 PR,例如 dev , qa等(例如,如果您使用的是基于 Gitflow 的工作流程)。

我们支持基于动态分支的 PR 的方式是将 CodeBuild 用于我们工作流的构建/单元测试阶段,然后打包存储库并构建工件到 S3。从那里,我们使用 S3 人工制品作为源为集成和接受环境触发部署管道。使用 CodePipeline 进行部署非常强大,因为它会自动确保一次只能执行一个阶段,这意味着在任何时候给定的应用程序只有一个更改通过给定的环境。

然而,这种方法非常复杂,需要相当多的 Lambda 魔法与 SQS FIFO 队列混合来处理并发 PR(这是为了克服 CodePipeline 的替代行为),但它是一种非常强大的模式。我们还使用 GitHub 审查来执行诸如触发接受阶段和自动批准 CodePipeline 中的手动批准步骤等操作。

一旦您准备好合并 PR,我们只需使用由 master 触发的普通 CodePipeline 部署到生产环境 - 您还需要做的一件事是确保您使用在 PR 上构建和测试的工件。

关于amazon-web-services - 如何为合并的 GitHub 拉取请求触发 CodePipeline?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58839820/

相关文章:

git - 从 Git 历史记录中删除二进制文件后,为什么我的存储库仍然很大?

amazon-web-services - 为什么我无法使用私有(private) DNS 地址访问 AWS 实例

amazon-web-services - 如何将 AWS 开放数据集下载到我的计算机上?

node.js - Nginx 502 错误网关 Node JS

amazon-web-services - AWS Serverless,CloudFormation : Error, 尝试将非字符串值填充到变量的字符串中

git - 如何在 GitHub 中更改提交的作者?

git - 如何使用 Github Pages 分支在存储库中正确提交

kubernetes - Terraform:错误:Kubernetes 集群无法访问:配置无效

azure - 使用多级 map 在 terraform 中循环

terraform - 如何形成 key=value terraform 输出