Example bash script to identify who triggered a workflow

Hello Everyone!

Recently the Support team created a script for determining who triggered a workflow by using our v2 API. Feel free to mix up the script how ever you best see fit!

Note: We’re making us of jq which makes using JSON in scripts possible
Also, if you have an environment variable set with your CIRCLE_API_TOKEN, feel free to delete the variable declaration line from the script!

#!/bin/bash -e

# This script will return the user who started a workflow
# Can be called with "sh name_of_file.sh" -- you will be prompted for a workflow ID to get data on

### Variables ###
​
# Set to personal API token
CIRCLE_API_TOKEN=
​
### Commands ###

# Check for Input
read -p 'Workflow ID: ' WORKFLOW
​
if [ -z "$WORKFLOW" ]
  then
    echo "No argument supplied, please enter a workflow ID."
    exit 1
fi

USER_ID=$(curl https://circleci.com/api/v2/workflow/$WORKFLOW?circle-token=$CIRCLE_API_TOKEN | jq -r '.started_by')

curl https://circleci.com/api/v2/user/$USER_ID?circle-token=$CIRCLE_API_TOKEN
​
#### END ####