This repository was archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest-v2-api.sh
executable file
·68 lines (50 loc) · 1.62 KB
/
test-v2-api.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash -e
# Usage:
#
# 1. set $CIRCLE_API_TOKEN
# 2. run `./test-v2-api <project_slug> [<branch>]`
#
# To trigger a pipeline on https://github.com/CircleCI-Public/circleci-cli:
#
# ./test-v2-api gh/CircleCI-Public/circleci-cli
#
# The above will trigger on the master branch by default.
_project_slug=$1 # e.g. gh/myorg/myproject
_branch=${2:-"master"} #optional, defaults to master.
_circle_token=${CIRCLECI_API_TOKEN}
if [ -z "$CIRCLECI_API_TOKEN" ]; then
echo "ERROR: MISSING CIRCLECI TOKEN"
echo "You need to set CIRCLECI_API_TOKEN in the environment to use this script."
exit 1
fi
if [ $# -eq 0 ]
then
echo "ERROR: MISSING ARGUMENT"
echo "You need to provide a project slug as an argument in the form gh/myorg/myproject to run this script."
exit 1
fi
api_root_endpoint="https://circleci.com/api/v2/"
function call() {
local url
local verb
verb=$1
url="${api_root_endpoint}${2}?circle-token=${_circle_token}"
echo -e "Calling URL: ${verb} ${url}"
echo -e "And now, onto calling with curl"
local response=$(curl -s -v \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data "${3}" \
-request "${verb} ${url}")
echo "$response"
}
trigger_pipeline_url="project/${_project_slug}/pipeline"
echo $trigger_pipeline_url
post_data="{ \"branch\": \"${_branch}\"}"
echo "Triggering pipeline of $_project_slug on branch ${_branch} by hitting ${trigger_pipeline_url}"
trigger_response=`call POST $trigger_pipeline_url $post_data`
echo "Trigger response:"
echo $trigger_response
echo "-------"
pipeline_id=`echo $trigger_response | jq '.id'`
echo $pipeline_id