【问题标题】:Running a github action after travis CI succeedtravis CI 成功后运行 github 操作
【发布时间】:2023-04-03 09:32:01
【问题描述】:

如果 travis CI 成功,我想以某种方式仅在 PR 上运行 Github 操作。我想这可以外推到任何其他类型的 CI。

目前系统是这样运行的

1) submit PR
2) Travis CI & Action run at the same time

但我想要这个流程:

1) submit PR
2) Travis CI runs
  success: run Action
  failure: don't run Action

我已经尝试实现run-travis-yml,它基本上只是让我在操作的下一步之前运行 Travis CI 的构建脚本;但是,我猜实际的 CI 是异步的,所以它总是会完全基于它是否构建成功。

如何在单独检查成功或失败后运行给定操作?

顺便说一句,this code is public

【问题讨论】:

标签: yaml travis-ci github-actions


【解决方案1】:

这对我有用:

https://api.github.com/repos/ethereum/EIPs/commits/<ref>/statuses 出于某种原因只能看到我的第 3 方 CI 检查的状态。因此,即使其他测试失败,它也只会考虑 CI 测试。

所以我使用这个属性每 30 秒检查一次,如果出现错误则中止;这是代码:

// HACK (alita): check the CI status and only continue if the CI is successful
const checkCIStatus = async () => {
  const Github = getOctokit(GITHUB_TOKEN);
  const pr = await requirePr()
  const status = await Github.repos.getCombinedStatusForRef({
    owner: context.repo.owner,
    repo: context.repo.repo,
    ref: pr.head.ref
  }).then(res => res.data.state)

  console.log(`status is '${status}'...`)
  if (status === "failure") {
    setFailed("CI checks failed; bot can only merge if CI checks pass")
    throw "CI checks failed; bot can only merge if CI checks pass"
  }

  return status === "success";
}

export const pauseInterval = (checker, next, timeout) => async () => {
  const status = await checker();
  if (!status) {
    setTimeout(pauseInterval(checker, next, timeout), timeout);
  } else {
    return next()
  }
}

// only runs the bot if the CI statuses pass; checks every 30 seconds
export const main = pauseInterval(checkCIStatus, _main, CHECK_STATUS_INTERVAL)

成功的输出

status is 'pending'...
status is 'pending'...
status is 'pending'...
status is 'pending'...
status is 'pending'...
status is 'pending'...
status is 'pending'...
status is 'pending'...
status is 'pending'...
status is 'pending'...
status is 'pending'...
status is 'success'...
<normal action outputs>

检查失败的输出

status is 'pending'...
status is 'pending'...
(node:1603) UnhandledPromiseRejectionWarning: CI checks failed; bot can only merge if CI checks pass
status is 'failure'...
(Use `node --trace-warnings ...` to show where the warning was created)
(node:1603) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1603) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

这显然只有在自定义操作中才真正可行,但您可能可以创建一个简单的操作/js 文件,该文件可以执行相同的行为并在给定操作之前被视为一个操作。如果你有这个,并且在成功或错误时失败/成功,那么你可以有条件地在 Github yaml 脚本中调用你的操作。

【讨论】:

    猜你喜欢
    • 2018-03-14
    • 2021-11-21
    • 2021-03-18
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 2015-03-28
    相关资源
    最近更新 更多