1
1
description : |
2
- This command waits for another job in this workflow. Since right now Circle CI doesnt let
2
+ This command waits for a job(s) in this workflow. Since right now Circle CI doesn't let
3
3
you queue up jobs irrespective of whether they fail or not. This is a faux queue where
4
4
the command stalls till the other job succeeds.
5
5
This requires a Circle CI token be set as $CIRCLE_TOKEN
6
6
7
7
parameters :
8
8
job-name :
9
- description : The job on which to wait. If job not found continue immediately
9
+ description : The job or jobs on which to wait. If job not found continue immediately. For multiple jobs the list must be comma separated.
10
10
type : string
11
11
max-wait-time :
12
12
description : |
@@ -27,6 +27,12 @@ parameters:
27
27
'*' the wait will run only on the specified branch
28
28
type : string
29
29
default : " *"
30
+ circleci-api-key :
31
+ default : CIRCLE_TOKEN
32
+ description : >-
33
+ In case you use a different Environment Variable Name than
34
+ CIRCLE_TOKEN, supply it here.
35
+ type : env_var_name
30
36
31
37
steps :
32
38
- run :
37
43
exit 1
38
44
fi
39
45
hash jq 2>/dev/null || { echo >&2 "jq is not installed. Aborting."; exit 1; }
40
- if [[ "$CIRCLE_TOKEN " == "" ]]; then
41
- echo "CIRCLE_TOKEN not set. Set a token to access the circle API in the env var CIRCLE_TOKEN ";
46
+ if [[ "$<< parameters.circleci-api-key >> " == "" ]]; then
47
+ echo "<< parameters.circleci-api-key >> not set. Set a token to access the circle API in the env var << parameters.circleci-api-key >> ";
42
48
exit 1;
43
49
fi
44
50
@@ -47,42 +53,51 @@ steps:
47
53
exit 0;
48
54
fi
49
55
50
- api_endpoint="api/v2/workflow/${CIRCLE_WORKFLOW_ID}/job"
51
-
52
56
mkdir -p /tmp/swissknife
53
57
54
- # This is a global variable used to get return value for get_job_status
55
- job_status=""
56
- job_number=""
57
58
58
59
get_job_status() {
59
- wf_url="https://circleci.com/$api_endpoint?circle-token=${CIRCLE_TOKEN}"
60
- curl -f -s $wf_url > /tmp/swissknife/wf_$CIRCLE_WORKFLOW_ID.json
61
- job_status=$(jq -r '.items[] | select(.name=="<< parameters.job-name >>") | .status' /tmp/swissknife/wf_$CIRCLE_WORKFLOW_ID.json)
62
- job_number=$(jq -r '.items[] | select(.name=="<< parameters.job-name >>") | .job_number' /tmp/swissknife/wf_$CIRCLE_WORKFLOW_ID.json)
60
+ local NAME_OF_JOB="$1"
61
+ local STATUS=""
62
+ local NUMBER=""
63
+
64
+ curl --header "Circle-Token: $<< parameters.circleci-api-key >>" -f -s https://circleci.com/api/v2/workflow/${CIRCLE_WORKFLOW_ID}/job > /tmp/swissknife/wf_$CIRCLE_WORKFLOW_ID.json
65
+ STATUS=$(jq -r ".items[] | select(.name==\"${NAME_OF_JOB}\") | .status" /tmp/swissknife/wf_$CIRCLE_WORKFLOW_ID.json)
66
+ NUMBER=$(jq -r ".items[] | select(.name==\"${NAME_OF_JOB}\") | .job_number" /tmp/swissknife/wf_$CIRCLE_WORKFLOW_ID.json)
67
+ echo "${STATUS} ${NUMBER}"
63
68
}
64
69
65
- current_wait_time=0
66
70
67
- while true; do
68
- get_job_status
69
- if [[ "$job_status" == "success" || "$job_status" == "failed" || "$job_status" == "canceled" || "$job_status" == "" ]]; then
70
- echo "Its finally my turn. exiting"
71
- exit 0
72
- else
73
- echo "Looks like the other guy ($job_number) is still not done ($job_status)."
74
- echo "Going to sleep for << parameters.sleep-time-between-checks >>"
75
- sleep << parameters.sleep-time-between-checks >>
76
- current_wait_time=$(( current_wait_time + << parameters.sleep-time-between-checks >> ))
77
- fi
71
+ job_list="<< parameters.job-name >>"
72
+
73
+ for job_name in ${job_list//,/ }
74
+ do
75
+ # Reset job status, job number and wait time
76
+ job_status=""
77
+ job_number=""
78
+ current_wait_time=0
78
79
79
- if (( $current_wait_time > << parameters.max-wait-time >> )); then
80
- if [[ "<< parameters.kill-gracefully >>" == "true" ]]; then
81
- echo "Proceeding with future steps";
82
- exit 0;
80
+ echo "Starting to check status of ${job_name}"
81
+ while true; do
82
+ read job_status job_number < <(get_job_status ${job_name})
83
+ if [[ "$job_status" == "success" || "$job_status" == "failed" || "$job_status" == "canceled" || "$job_status" == "" ]]; then
84
+ echo "Job, ${job_name}, has completed!"
85
+ break
83
86
else
84
- echo "Failing job by exiting forcefully";
85
- exit 1;
87
+ echo "Looks like the other guy ($job_number) is still not done ($job_status)."
88
+ echo "Going to sleep for << parameters.sleep-time-between-checks >>"
89
+ sleep << parameters.sleep-time-between-checks >>
90
+ current_wait_time=$(( current_wait_time + << parameters.sleep-time-between-checks >> ))
91
+ fi
92
+
93
+ if (( $current_wait_time > << parameters.max-wait-time >> )); then
94
+ if [[ "<< parameters.kill-gracefully >>" == "true" ]]; then
95
+ echo "Proceeding with future steps";
96
+ break
97
+ else
98
+ echo "Failing job by exiting forcefully";
99
+ exit 1;
100
+ fi
86
101
fi
87
- fi
102
+ done
88
103
done
0 commit comments