有没有一种方法可以在Pull Request上触发CircleCI构建? [英] Is there a way to trigger a CircleCI build on a Pull Request?

查看:81
本文介绍了有没有一种方法可以在Pull Request上触发CircleCI构建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的问题是,CircleCI现在不会触发PR上的构建,并且我检查到我只想在PR上运行.有此选项:

So, the problem I have is that right now CircleCI doesn't trigger builds on PRs, and I have checks that I'd like to run only on PRs. There is this option:

仅构建拉取请求

默认情况下,我们将构建该项目的所有提交.一次启用后,我们将仅构建具有关联拉动的分支请求打开.注意:对于您的默认分支,我们将始终构建所有提交.

By default, we will build all the commits for this project. Once turned on, we will only build branches that have associated pull requests open. Note: For your default branch, we will always build all commits.

但是,这不是我所需要的,因为我仍然需要一些检查才能在所有分支的所有提交上运行,而不仅仅是默认的.

However, this is not what I need, because I still need some checks to run on every commit, on all branches, not just the default one.

这是我的circle.yml文件中的内容:

This is what I have in my circle.yml file:

test:
  override:
    - if [[ ! -z $CI_PULL_REQUEST ]] ; then yarn test:selenium ; fi

只有在打开PR后对分支 进行另一次推送时,才会触发此操作,因为只有这样才能触发构建.

This only gets triggered if there's another push made to the branch after opening a PR, because only then is the build triggered.

我只是无法找到解决方法,有人有任何想法吗?

I just haven't been able to find a workaround for this, does anyone have any ideas?

推荐答案

如果您在github上,现在有一种方法,并且您愿意运行一个快速的github动作来给CircleCI挠痒痒.该动作非常快,因此不会花费很多github动作时间.

There is NOW a way if you are on github, and you are willing to run a quick github action to tickle CircleCI. The action is pretty quick so doesn't consume much github actions time.

通过将以下内容保存在.github/workflows/main.yml

Setup an action by saving the following in .github/workflows/main.yml

name: CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  pull_request:
    types: [opened,reopened]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    - name: Tickle CircleCI
      env:
        CIRCLE_BRANCH: ${{ github.head_ref }}
      run: |
        curl -X POST \
        -H 'Circle-Token: ${CIRCLE_TOKEN}' \
        -H 'Content-Type: application/json' \
        -H 'Accept: application/json' \
        -d "{\"branch\":\"${CIRCLE_BRANCH}\"}" \
        https://circleci.com/api/v2/project/<project_slug>/pipeline

替换< project_slug>对于您的项目,该子段为gh/< repo_owner>/< repo_name>.

Replace <project_slug> with the slug for your project which is gh/<repo_owner>/<repo_name>.

现在,在CircleCI中设置一个令牌,并将其作为CIRCLE_TOKEN添加到github操作的机密中.

Now go setup a token in CircleCI and add it to secrets in github actions as CIRCLE_TOKEN.

接下来,在第一步中要有条件运行的任何作业中,向.circleci/config.yml添加一个步骤.我们总是在master上运行测试因此将其考虑在内.随时调整以适应您的需求.

Next add a step to your .circleci/config.yml in any job you want to be conditionally run as the very first step. We always run tests on master so this takes that into account. Feel free to adjust to your needs.

      - run:
          name: Check for PR or master
          command: |
            if [ -z "$CIRCLE_PULL_REQUEST" ]; then
              if [ "$CIRCLE_BRANCH" != "master" ]; then
                echo "Not a PR or master, halting"
                circleci step halt
              fi
            fi

如果PR未打开,则作业将在CircleCI上运行,然后立即停止.就CircleCI时间而言,这也不是太昂贵,因此总的来说,这使我们可以以合理的成本运行所有提交到master的提交以及开放式PR中的任何内容.

If a PR is not open a job will run on CircleCI and then immediately stop. This isn't super expensive in terms of CircleCI time either, so overall this lets us run all commits to master and anything in an open PR for reasonable cost.

打开PR时,将运行github操作并触发CircleCI重新运行,但是由于PR现在已打开,因此它将完全运行.

When you open a PR the github action will run and trigger CircleCI to rerun but since a PR is now open it will run completely.

这种方法的缺点是,您将在PR打开之前对提交的运行进行绿色检查,表明测试已通过,但没有通过,但是,一旦github操作运行了最后一次提交,您就在乎,当CircleCI重新运行作业时,将变回黄色.

The one downside to this approach is that you WILL get a green check on commits run before the PR is open indicating tests passed but they did not, however as soon as the github action runs the last commit, the one you care about, will change back to yellow as CircleCI reruns the job.

将来的提交仅由CircleCI运行,并且一旦PR打开,github动作根本不会运行,而在github动作中却什么也不花钱.

Future commits are run only by CircleCI and the github action does not run at all costing you nothing in github actions once the PR is open.

这篇关于有没有一种方法可以在Pull Request上触发CircleCI构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆