Advice on triggering build in separate GitHub repo from config.yml

Hi, new poster here. I’ve searched for this requirement but haven’t found anything exactly like it or the posts were very old. We have the following scenario.

  • The .circleci/config.yml resides in it’s own GitHub repo that is read only for the developers. Call it RepoA.
  • In another GitHub repo is where the developers would check in their code. Call it RepoB.
  • We want the pipeline in RepoA to run when a push is done in RepoB, but the pipeline should run in the context of RepoB. Meaning, checking out the code in RepoB and running steps on the contents of RepoB.

I’ve seen various ways that might accomplish this including multiple pipelines in each repo, GiHub webhooks, CircleCI webhooks, API calls, etc., but I’m having a hard time getting my head around it.

Any advice would be much appreciated. Thanks!

Hello,
To achieve the setup you’re describing, where the pipeline in RepoA triggers and runs in the context of RepoB, you can use a combination of GitHub Actions (or Webhooks) and CircleCI’s API. Here’s a general approach to accomplish this:

Step 1: Set Up a GitHub Action or Webhook in RepoB
GitHub Actions: You can create a GitHub Action in RepoB that triggers on push events. This action would make an API call to CircleCI to trigger the pipeline in RepoA.

Webhook: Alternatively, set up a GitHub Webhook in RepoB that sends a POST request to CircleCI when a push occurs.

Step 2: Configure CircleCI to Run in the Context of RepoB
In RepoA’s .circleci/config.yml, you’ll configure the job to:

Check Out RepoB: Use the checkout step to clone RepoB. Since CircleCI is running from RepoA, you’ll need to explicitly specify the repository URL for RepoB.

- checkout:
    path: /path/to/repoB

Set Up the Environment: Configure the environment to ensure it reflects the context of RepoB (e.g., installing dependencies, setting environment variables, etc.).

Run the Pipeline Steps: Execute the necessary steps on the contents of RepoB.

Example GitHub Action in RepoB
Here’s an example of a GitHub Action that could be placed in RepoB to trigger the CircleCI pipeline in RepoA:

name: Trigger CircleCI Pipeline

on:
  push:
    branches:
      - main

jobs:
  trigger-circleci:
    runs-on: ubuntu-latest
    steps:
      - name: Trigger CircleCI Pipeline
        run: |
          curl -u $CIRCLECI_TOKEN: \
          -d '{
            "branch": "main",
            "parameters": {
              "run": true
            }
          }' \
          https://circleci.com/api/v2/project/github/<RepoA-Owner>/RepoA/pipeline
        env:
          CIRCLECI_TOKEN: ${{ secrets.CIRCLECI_TOKEN }}

Replace with the owner of RepoA.
Store the CIRCLECI_TOKEN as a secret in RepoB’s GitHub repository settings.
Final Notes
CircleCI Environment Variables: Ensure any required environment variables or secrets are configured in CircleCI to allow RepoA’s pipeline to access RepoB.
Branch Matching: You may want to ensure that the branches match between RepoA and RepoB when triggering the pipeline.
This approach lets you keep the pipeline configuration centralized in RepoA, while still reacting to changes in RepoB and running the pipeline in its context.

Hope this will help you.
Best regards,
florence023

Hey @nek, we are currently working on enabling this type of functionality for users that auth with our GitHub App integration. In practice, you would create a pipeline that uses a configuration file stored in repo A, checks out code from repo B, and listens to commits pushed from repo B. You’d repeat this for any other repos that you want to listen to events from.

I can update this post when that functionality is ready, feel free to send any additional thoughts/feedback to sebastian@circleci.com

Thanks Sebastian. That would be great.

@nek Sorry for the delay here, we have some documentation on how to achieve this here. Want to let us know if this works for your scenario?

cc @Benny

1 Like