If a build completes in one project is there a way to trigger build on another project

We have two code repositories say A and B. Once a workflow completes on Repo A is it possible to auto trigger a build on Repo B? Couldn’t find any YAML configuration or something to do that.

I’ll ask around about this, but the workaround that comes to mind would be setting an environment variable when your build is complete, indicating it’s complete, and running a script which checks that environment variable, and triggers the other repo to build via an empty commit.

Thanks @Cormac ! I think this variable could be set at repository level itself so that if we don’t want to trigger build in other Repo we can delete this variable and add it again when required.

1 Like

When I did the research, I couldn’t find anything built-in to Circle that would do this.

What I would do is add a step after the build step of rep A to trigger a build on B via the API.

This has the advantage of:
1- only triggering the build on B if the build step in A succeeds, and
2- doesn’t need empty commits.

Getting a Circle API Token
To trigger a build via the API, you need to get a circle token. To generate this,
1- Go to https://circleci.com/dashboard,
2- make sure you’re logged in, then on the top right corner click on your profile icon and select User settings,
3- from the left side menu, select Personal API Tokens,
4- on the right, click on Create New Token,
5- pick an appropriate name such as “Repo A Token” and click Add API Token, then
6- copy the string that appears on the screen and save it somewhere.

Once you have the API token, you can use it to trigger builds on your repositories using POST requests.
The API call requires the URL of the repository you want to trigger the build on (Repo B) and the Circle API token.

1- Repository URL
The repository URL is the address of the POST request and has the format:

https://circleci.com/api/v1/project/organization_name/project_name/tree/branch_name

It is best practice to save the organisation, project, and branch names in corresponding environment variables instead of hardcoding the repository URL:

ORGANIZATION=your_organization_name
PROJECT=your_project_name
BRANCH=desired_branch

Note that Circle exposes the branch being built through an environment variable called $CIRCLE_BRANCH, so in repo A, it’s possible to do:

BRANCH=$CIRCLE_BRANCH

if you have the same branch names on both repositories.

2- Circle API Token
The API token is passed to the POST request as a request parameter:

?circle-token=xxxxxxxxxxxxxxxxxxxxxxxxxx

It is best practice to save your Circle Token as a secured environment variable (in Project Settings -> Environment Variables) to avoid exposing it in plain text in your source code.
Assuming you’ve added a secured environment variable called CIRCLE_TOKEN, the request parameter can be passed as:

?circle-token=$CIRCLE_TOKEN

Putting It Altogether
The following curl command shows the exact syntax needed:

curl -X POST -H -d \
 "{}" \
 "https://circleci.com/api/v1/project/$ORGANIZATION/$PROJECT/tree/$BRANCH?circle-token=$CIRCLE_TOKEN"

Note that v1 of the API is used for both CircleCI 1 and 2

Complete Example
Here’s a complete config.yml file that I’ve used in a previous project:

version: 2
general:
  branches:
    only:
      - dev
      - staging
      - prod
jobs:
  build:
    docker:
      - image: spotify/alpine

    steps:
      - run:
          name: Trigger the Build Job
          command: |
            printf "Triggering an $PROJECT build on the $CIRCLE_BRANCH branch\n\n"

            BUILD_INFO=$(curl -X POST -H -d \
                "{}" \
                "https://circleci.com/api/v1/project/$ORGANIZATION/$PROJECT/tree/$CIRCLE_BRANCH?circle-token=$CIRCLE_TOKEN")

            printf "\n\nBuild triggered\n\n"
            printf "Follow the progress of the build on \nhttps://circleci.com/gh/$ORGANIZATION/$PROJECT/tree/$CIRCLE_BRANCH"

Note that I’ve stored all the environment variables in Cirlce’s settings, and I’m using CIRCLE_BRANCH directly in the curl command.

9 Likes

Nice work @muhammed-sayadi :smile_cat:

1 Like

I think thats the correct way (i.e. using CircleCI API) to trigger a build. Thank you.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.