Circle CI api url to trigger a pipeline on a webhook

I am trying to add circle ci in a phraseApp webhook, so on every translation change, circle ci pipeline would trigger.

Since I need to give only a callback url in a webhook, I have given like below

https://circleci.com/api/v2/project//pipeline?circle-token=

I am currently have couple of issues

  1. How can I pass parameters, branch name in this? I have tried to pass branch as a query param, that didn’t work.

  2. I am getting 400 error response from circle ci when the trigged via webhook , not sure what exacly is invalid in the request (I have tested webhook via send test notification, which worked fine and pipeline got triggered)

Please help to resolve this issues.

Thanks

I had a similar problem - I want to construct a hyperlink that, when a user clicks on, it, runs a Circle pipeline with my pipeline parameters in the get query, as you want here.

Like your webhook request, a hyperlink is a GET request, not a POST request. However the documentation says the pipeline triggering API is a POST request.

AND I can’t just construct a URL that points to somewhere on circleci.com, although that would be awesome. It would actually fix having to worry about the CircleCI token because you have a cookie when you use the web UI. (That would have worked in my case, anyway).

So you need a translator. I wrote the following Google Cloud Function, although 2 lines of change would make it work under AWS Lambda or whatever. You can allow selected identities to run the function in IAM controls, or depend on security by obscurity.

Anyway, here’s 40 lines of sloppy Node.js code (half generated by Postman) using axios to make the request

const functions = require('@google-cloud/functions-framework');
var axios = require('axios');


functions.http('ciTrigger', (req, res) => {

    var branch = req.query.branch || "main"
    var orgRepo = req.query.orgRepo || "mygithuborb/my-default-repo";

    if (req.query.branch) {
        delete req.query.branch
    }

    if (req.query.orgRepo) {
        delete req.query.orgRepo
    }

    var data = JSON.stringify({
        "branch": branch,
        "parameters": req.query
    });

    var config = {
        method: 'post',
        url: `https://circleci.com/api/v2/project/gh/${orgRepo}/pipeline`,
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Circle-Token': process.env.CIRCLE_TOKEN
        },

        data: data
    };

    axios(config)
        .then(function(response) {
           var pipelineNum = response.data.number
           res.send(`<html><body><a href="https://app.circleci.com/pipelines/github/${orgRepo}/${pipelineNum}">Open in Circle</a></body></html>`);
        })
        .catch(function(error) {
            res.send(error);
        });
});

Then the following link will work

https://BAZ-BAR-WHERE-THIS-FUNCTION-LIVES.a.run.app/?name=ryan

and calls your pipeline with a variable name whose value = “ryan”.

https://BAZ-BAR-WHERE-THIS-FUNCTION-LIVES.a.run.app/?name=ryan&orgRepo=boo%2Frepo triggers the build in boo org’s repo repo, running the pipeline with that name parameter.

It’ll even give you a link to the Circle UI, with my awesome HTML skillz.

@DineshkumarT We just launched something that may help you automate triggering a pipeline on a translation change without needing to use our API:

You would set up an inbound webhook in CircleCI and then go to wherever the translation changes are happening and set up an “outbound” webhook to hit the URL from Circle’s webhook whenever a translation event happens. You can even have a separate configuration file just for these pipelines that are different than your “typical” CI pipeline.