如何配置 Travis-CI 以构建拉取请求和合并以掌握 w/o 冗余

How to configure Travis-CI to build pull requests & merges to master w/o redundancy

换句话说"BDD":

Background:
Given I'm contributing to a GH repo

When I create a pull request
Then Travis should build the latest commit

When I push to an existing pull request
Then Travis should build the latest commit

When I merge a pull request to master
Then Travis should build master

我对 Travis-CI 的 "build pushes" 和 "build PRs" 设置感到困惑,因为:

这在 Travis-CI docs and GH issue #3241 中有更多解释。

有人知道满足上述条件的配置吗?

我最终发现了另一个 GH 问题 (#2111),它给了我尝试同时启用 PR 和推送的想法,但使用白名单来限制对特定分支的推送。这似乎满足我的工作流程的标准。这是我所做的:

  1. 在存储库的 Travis 设置中启用 PR 和分支推送:

  1. .travis.yml 更改为 white-list master branch(即仅构建推送到 master):
branches:
  only: 
    - master
  1. 通过创建 PR with the .travis.yml change, and another PR with some empty commits to verify it works for forks too.

  2. 来测试它
  3. 验证successful merge commit build from master.

接受的答案中描述的白名单方法有一些明显的局限性。特别是,它不支持在不打开 PR 的情况下非冗余地构建任意分支。

我打开了an issue asking for a better solution

如果您不仅要测试 master 分支,还要测试其他一些分支,您可以使用下一个工作流程:

  • 同时保持 "Build pushes" 和 "Build pull requests" 开启
  • branches:except 指令添加到您的 .travis.yml:

    branches:
      except:
        - /^pr\..*/
    

在此配置中:

  • 对分支 feature-A 的任何提交都将触发构建
  • 对分支 pr.feature-A 的任何提交都不会触发构建
  • 如果在打开的拉取请求中使用分支 pr.feature-A 则构建将被触发

工作流程示例

  • 多个开发人员共享的临时 WIP 分支:wip.feature-A,对该分支的任何提交都将触发构建
  • 当分支准备好合并到 master 时,您可以将其从 wip.feature-A 重命名为 pr.feature-A 并打开 pull request
  • 如果在审查拉取请求时您想要应用新修复,只需推送到 pr.feature-A

在上述所有步骤中,只会触发一个构建。

刚刚在 travis docs

中找到

添加到 .travis.yml

if: type = push

或者:

if: type = pull_request

假设你想构建 all 个 PR,像下面这样的东西就可以了。在设置页面上启用分支和 PR 构建,并将此行作为第一行 travis.yml:

if: (type = push AND branch IN (master, dev)) OR (type = pull_request AND NOT branch =~ /no-ci/)

这将尝试对所有推送进行推送构建,并尝试对所有推送进行 PR 构建以打开 PR,但会过滤掉任何不符合条件的内容。您可能需要稍微修改一下 - 关于不在其名称某处构建带有 no-ci 的分支的条款显然是可选的,并且您可能没有两个总是希望 运行 构建的分支。

您可以在 Travis 的网站 conditions and conditional builds 上阅读更多内容。

对于我正在使用的其中一个存储库,这是我想要的:

有一个 origin 存储库,它是执行所有版本的主要存储库。

我希望所有到达 master 分支的拉取请求都应该只用 Travis 构建 一次 而不管它来自 forked 回购或 origin 本身的任何 其他分支

对于这种情况,这很有效

if: (type == push) OR (type == pull_request AND fork == true)