Skip to content

Instantly share code, notes, and snippets.

@d2lam
Created March 5, 2019 00:12
Show Gist options
  • Save d2lam/d1251e3431cd50cf7f1de9a5dea74368 to your computer and use it in GitHub Desktop.
Save d2lam/d1251e3431cd50cf7f1de9a5dea74368 to your computer and use it in GitHub Desktop.
#!/bin/bash
usage() { echo "Usage: sd-cmd exec sd/getJobId@<VERSION> [-p] [-j]"; }
while getopts 'p:j:u:' option; do
case "${option}" in
p) PIPELINE_ID=${OPTARG};;
j) JOB_NAME=${OPTARG};;
u) URL=${OPTARG};;
*) usage;;
esac
done
if [[ -z $PIPELINE_ID ]] || [[ -z $JOB_NAME ]]
then
echo "Need to pass pipelineID and jobName"
exit 1
fi
export API_URL="https://api.screwdriver.cd"
if [[ ! -z $URL ]]
then
export API_URL=$URL
fi
export JOB_URL="$API_URL/v4/pipelines/$PIPELINE_ID/jobs?jobName=$JOB_NAME"
JOB=$(curl -sb -1 -H "Authorization: Bearer $SD_TOKEN" -H "Accept: application/json" -H "Content-Type: application/json" $JOB_URL)
RESPONSE=$(echo "$JOB" | sed -e "s/.*{\"id\"://")
if [[ $RESPONSE =~ ^[0-9] ]];
then
JOB_ID=$(echo "$RESPONSE" | sed -e "s/,.*//")
echo $JOB_ID
else
echo "Failed to fetch job ID: $RESPONSE"
exit 1
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment