CircleCI-Archived / api-preview-docs

In-progress docs about the pre-release preview of CircleCI API v2
72 stars 39 forks source link

Unable to trigger pipeline to build forked Pull Request via V2 API, error message: *Branch not found* #85

Open babygoat opened 4 years ago

babygoat commented 4 years ago

Post on discuss forum as well.

I'm encountering problem when triggering a pipeline by v2 API endpoint, which returns error message of "Branch not found" with 400 status code. My goal is to setup a mono-repo with services in individual directories and triggering the workflow only when the services are modified. Thanks to the discussion forums , I found some helpful discussions and explanation which give me great heads up.

Here is a simpler version of the config snippet

version: 2.1
parameters:
    trigger:
      type: boolean
      default: true

    service1:
      type: boolean
      default: false

    service2:
      type: boolean
      default: false

jobs:
   trigger_pipelines:
     ...
     steps:
        - checkout
        - run:
            name: trigger service workflow if modified
            command: |
                // Filter the modified service
                ...
                // trigger modified service workflow 
                curl -u "${CIRCLE_TOKEN}:" \
                       -H "Content-Type: application/json"
                       -d "{\"branch\": \"$CIRCLE_BRANCH\", \"parameters\": {...} }" \
                       https://circleci.com/api/v2/project/gh/org/repo/pipeline

workflows:
    version: 2

    analysis:
        when: << pipeline.parameters.trigger >>
        jobs:
           - trigger_pipelines

        service1:
            when: << pipeline.parameters.service1 >>
            ...
        service2:
            when: << pipeline.parameters.service2 >>
            ...

The default workflow(analysis) was triggered from a forked Pull Request (I switch on the setting Build forked pull requests) which created a pull/1 branch on the CircleCI server. However, when the changes of service1 workflow was detected and triggered by the POST /pipeline endpoint, I got the error message "Branch not found" in the build step terminal. I also tried to try to POST the endpoint through postman to ensure the branch value equals to pull/1 but the same error remained. I was wondering if I missed something in the document. Let me know if you need more informations for investigation. Thanks for your patience to read the post!