Is there a way to trigger a CircleCI build on a Pull Request?

一曲冷凌霜 提交于 2021-02-08 15:32:24

问题


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:

Only build pull requests

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.

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

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

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?


回答1:


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.

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

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

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

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

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.

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.

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.

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.



来源:https://stackoverflow.com/questions/44672893/is-there-a-way-to-trigger-a-circleci-build-on-a-pull-request

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!