How to recognize different events in CircleCI

Hello all,

I have a simple question but unfortunately, I couldn’t find it searching! And I am a newbie in CircleCI and coding so feel free to explain plain things. :face_with_raised_eyebrow:

I am using config.yml version 2 for our automated deployment using CircleCI integrated with Bitbucket. I want to know how can I recognize which events from Bitbucket triggered the pipeline? for example was it Push, PR update, or Merge? I have already selected my desired webhooks in the Bitbucket setting.
So in simple words, I need to do something like this in my config.yml:

- run:
      name: Which-event-triggered
      command: |
        if [ $event == "PR_updated" ];
        then
          ./an_script.sh
        else
          ./another_script.sh
        fi

Any bits of help or ideas would be really appreciated. :blush:
Thanks,
Hamed!

Hi @Hamed!

Welcome to Discuss and great question – this is a bit of a tricky one!

To clarify some details, we don’t have a specific event type variable in CircleCI, we receive the webhook and it contains commit details. However, there could be some ways to check for this information.

  1. We will automatically set a variable called CIRCLE_PULL_REQUEST if the commit that triggered the build is associated with an open Pull Request.
  2. We have an API endpoint that allows you to get all commit details associated with a build:
    https://circleci.com/docs/api/v1/#single-job

With the above in mind, you could at the very least tell if the commit is associated with a PR with something like:

- run: 
    name: If associated with PR
    command: |
      if [ -n "$CIRCLE_PULL_REQUEST" ]; then
        echo "This is part of a PR!"
      else
        echo "This isn't part of a PR!"
      fi

Alternatively, if the above doesn’t work, you could modify your processes on your end for merging in PR’s and merging to open PR’s to include specific information or key identifier in the commit subject.

You could then use the API endpoint I mentioned to parse out (likely with jq) the commit details and look for specific matchings in order to proceed with a specific part of the script.

Hopefully the above helps and please let me know if you have any further questions!

2 Likes

Hi @nbialostosky,
Thanks for your reply and help.
For my exact situation, it works fine now. :slightly_smiling_face: I wanted it to:

1- Run deploy to the DEV server whenever one of our team members approves a pull request from Bitbucket (unfortunately CircleCI doesn’t take action on approving PR event from Bitbucket, even though I removed the created PR from webhooks and Approve is the first event in its kind) so we have to make an Update on PR and CircleCI does take action on it. (So not bad for a start)

2- Run deploy to the Staging server whenever someone merges this PR to the master branch.

Using the > CIRCLE_PULL_REQUEST in the if then else block which you mentioned helped me executing different scripts for deployment to different environments.

For other scenarios, I will definitely need to go for the API endpoints as a beginner :sweat_smile:
Can you please suggest any simple tutorial to start with APIs?
Thanks a million.

You’re welcome and happy to hear it!

As for the API side of things, we have some examples of utilizing the API in bash scripts here:

And here:

And here:

We also have a thing called CircleCI Academy that has a “General Developer” course that includes information on utilizing the API, you can signup and access here:

Hope that helps!

2 Likes

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