Circle ci variable for manual approver of a build

is there are any circleCI variable available for the approver who approves a build in a workflow. need to add the variable in custom slack integrations to know who kicks the deployment etc

Hi @junip-dewan,

There is no variable available by default for finding who approves a job in a workflow, but you can utilize our API to get this information. For this example, I will be using V2 of our API, for which the documentation can be found on the following page:

https://circleci.com/docs/api/v2/

From this API, we will be using the following two endpoints:

  1. Get a workflow’s jobs
  2. User information

For the first endpoint, we can get basic information about every job in a specific workflow.
Within that information is what user triggered each job, and what type of job it is (such as a regular build job, an approval job, etc.)

A request to this endpoint would look something like the following. I am passing in my user token as $CIRCLECI_TOKEN and the workflow id as $CIRCLE_WORKFLOW_ID, as well as using jq to parse out the id of users who approved jobs:

curl -s --request GET 'https://circleci.com/api/v2/workflow/$CIRCLE_WORKFLOW_ID/job' \
--header "circle-token: $CIRCLECI_TOKEN" \
--header 'content-type: application/json' | jq '.items | .[] | select(.type == "approval") | {approved_by}'

Assuming there was one approval job in the workflow, the response would look something like this:

{
  "approved_by": "2b583l96-d62z-4a12-9f0n-fd4c8f66ac8x"
}

Using this value, we can then use the second endpoint to get information about that user. I am passing in the user id from the previous result with $USER_ID:

curl -s --request GET 'https://circleci.com/api/v2/user/$USER_ID' \
--header "circle-token: $CIRCLECI_TOKEN" \
--header 'content-type: application/json'

The response will look something like the following:

{
  "name" : "CircleCI Test",
  "login" : "githubtestaccount",
  "id" : "2b583l96-d62z-4a12-9f0n-fd4c8f66ac8x"
}

You could take this further, parse out the login name with jq, and then store that inside of an environment variable if you were running the above code inside of a build.

Let me know if this helps, or if you would like me to clarify anything!

1 Like

@aaronclark Thanks . I have checked it worked. I was able to get the approver name in the build which is approved. But I was trying to get the variable to another job , Is there any way to get the variable from one job to another