Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 430
alkaia Avatar asked Jun 21 '17 09:06

alkaia


People also ask

How do you trigger a pull request Build?

You can set up pull request triggers for both Azure Repos or GitHub repositories. From within your project, Select Pipelines > Releases, and then select your release pipeline. Under the Pull request trigger section, select the toggle button to enable it.

How do I trigger a pull request GitHub?

Creating the pull request. On GitHub.com, navigate to the main page of the repository. In the "Branch" menu, choose the branch that contains your commits. Above the list of files, click Pull request.


Video Answer


1 Answers

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 }}
        CIRCLE_TOKEN: ${{ secrets.CIRCLE_TOKEN }}
      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.

like image 150
Nick Palmer Avatar answered Oct 17 '22 09:10

Nick Palmer