-
Notifications
You must be signed in to change notification settings - Fork 0
/
circle_ci_orb
397 lines (382 loc) · 18.6 KB
/
circle_ci_orb
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# This code is licensed from CircleCI to the user under the MIT license.
# See here for details: https://circleci.com/developer/orbs/licensing
commands:
notify:
parameters:
environment:
default: ${CIRCLE_JOB}
description: For deployments. Indicates the name of target environment. Default is the CircleCI Job Name.
type: string
environment_type:
default: development
description: Indicates the category of target environment as defined by Atlassian
enum:
- production
- staging
- testing
- development
- unmapped
type: enum
issue_regexp:
default: '[A-Z]{2,30}-[0-9]+'
description: Override the default project key regexp if your project keys follow a different format.
type: string
job_type:
default: build
description: Indicates if job should be treated as build or deployment in Jira dev panel. Note that Deployments require additional details
enum:
- build
- deployment
type: enum
scan_commit_body:
default: false
description: Whether or not to scan the Commit Body for the JIRA Issue Tag. Default is false.
type: boolean
service_id:
default: ""
description: Specify the JSD service ID for the project this notification targets.
type: string
state_path:
default: ./circleci-orb-jira.status
description: Relative or absolute path to a store build state for orb.
type: string
token_name:
default: CIRCLE_TOKEN
description: The name of environment variable containing CircleCI API Token. Required for all projects.
type: string
steps:
- jq/install:
when: always
- run:
command: |
echo 'JIRA_BUILD_STATUS="failed"' > <<parameters.state_path>>
name: JIRA - Setting Failure Condition
when: on_fail
- run:
command: |
echo 'JIRA_BUILD_STATUS="successful"' > <<parameters.state_path>>
name: JIRA - Setting Success Condition
when: on_success
- run:
command: |
: ${<<parameters.token_name>>:?"Please provide a CircleCI API token for this orb to work!"} >&2
if [[ $(echo $CIRCLE_REPOSITORY_URL | grep github.com) ]]; then
VCS_TYPE=github
else
VCS_TYPE=bitbucket
fi
run () {
verify_api_key
parse_jira_key_array
HAS_JSD_SERVICE_ID="<< parameters.service_id >>"
# If you have either an issue key or a service ID
if [[ -n "${ISSUE_KEYS}" || -n "${HAS_JSD_SERVICE_ID}" ]]; then
check_workflow_status
generate_json_payload_<<parameters.job_type>>
post_to_jira
else
# If no service is or issue key is found.
echo "No Jira issue keys found in commit subjects or branch name, skipping."
echo "No service ID selected. Please add the service_id parameter for JSD deployments."
exit 0
fi
}
verify_api_key () {
URL="https://circleci.com/api/v2/me?circle-token=${<<parameters.token_name>>}"
fetch $URL /tmp/me.json
jq -e '.login' /tmp/me.json
}
fetch () {
URL="$1"
OFILE="$2"
RESP=$(curl -w "%{http_code}" -s <<# parameters.token_name >> --user "${<<parameters.token_name>>}:" <</parameters.token_name>> \
-o "${OFILE}" \
"${URL}")
echo "${RESP}:${URL}"
if [[ "$RESP" != "20"* ]]; then
echo "Curl failed with code ${RESP}. full response below."
cat $OFILE
exit 1
fi
}
parse_jira_key_array () {
# must save as ISSUE_KEYS='["CC-4"]'
fetch https://circleci.com/api/v1.1/project/${VCS_TYPE}/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/${CIRCLE_BUILD_NUM} /tmp/job_info.json
# see https://jqplay.org/s/TNq7c5ctot
ISSUE_KEYS=$(cat /tmp/job_info.json | jq '[.all_commit_details[].subject | scan("(<<parameters.issue_regexp>>)") | .[] ] + [.all_commit_details[].branch | scan("(<<parameters.issue_regexp>>)") | .[] ] + [if .branch then .branch else "" end | scan("(<<parameters.issue_regexp>>)") | . [] ] + [if <<parameters.scan_commit_body>> then .all_commit_details[].body else "" end | scan("(<<parameters.issue_regexp>>)") | .[] ]')
if [ -z "$ISSUE_KEYS" ]; then
# No issue keys found.
echo "No issue keys found. This build does not contain a match for a Jira Issue. Please add your issue ID to the commit message or within the branch name."
exit 0
fi
}
check_workflow_status () {
URL="https://circleci.com/api/v2/workflow/${CIRCLE_WORKFLOW_ID}"
fetch $URL /tmp/workflow.json
export WORKFLOW_STATUS=$(jq -r '.status' /tmp/workflow.json)
export CIRCLE_PIPELINE_NUMBER=$(jq -r '.pipeline_number' /tmp/workflow.json)
echo "This job is passing, however another job in workflow is ${WORKFLOW_STATUS}"
if [ "<<parameters.job_type>>" != "deployment" ]; then
# deployments are special, cause they pass or fail alone.
# but jobs are stuck togehter, and they must respect status of workflow
if [[ "$WORKFLOW_STATUS" == "fail"* ]]; then
export JIRA_BUILD_STATUS="failed"
fi
fi
}
generate_json_payload_build () {
iso_time=$(date '+%Y-%m-%dT%T%z'| sed -e 's/\([0-9][0-9]\)$/:\1/g')
echo {} | jq \
--arg time_str "$(date +%s)" \
--arg lastUpdated "${iso_time}" \
--arg pipelineNumber "${CIRCLE_PIPELINE_NUMBER}" \
--arg projectName "${CIRCLE_PROJECT_REPONAME}" \
--arg state "${JIRA_BUILD_STATUS}" \
--arg jobName "${CIRCLE_JOB}" \
--arg buildNumber "${CIRCLE_BUILD_NUM}" \
--arg url "${CIRCLE_BUILD_URL}" \
--arg workflowUrl "https://circleci.com/workflow-run/${CIRCLE_WORKFLOW_ID}" \
--arg commit "${CIRCLE_SHA1}" \
--arg refUri "${CIRCLE_REPOSITORY_URL}/tree/${CIRCLE_BRANCH}" \
--arg repositoryUri "${CIRCLE_REPOSITORY_URL}" \
--arg branchName "${CIRCLE_BRANCH}" \
--arg workflowId "${CIRCLE_WORKFLOW_ID}" \
--arg repoName "${CIRCLE_PROJECT_REPONAME}" \
--arg display "${CIRCLE_PROJECT_REPONAME}" \
--arg description "${CIRCLE_PROJECT_REPONAME} #${CIRCLE_BUILD_NUM} ${CIRCLE_JOB}" \
--argjson issueKeys "${ISSUE_KEYS}" \
'
($time_str | tonumber) as $time_num |
{
"builds": [
{
"schemaVersion": "1.0",
"pipelineId": $projectName,
"buildNumber": $pipelineNumber,
"updateSequenceNumber": $time_str,
"displayName": $display,
"description": $description,
"url": $workflowUrl,
"state": $state,
"lastUpdated": $lastUpdated,
"issueKeys": $issueKeys
}
]
}
' > /tmp/jira-status.json
}
generate_json_payload_deployment () {
echo "Update Jira with status: ${JIRA_BUILD_STATUS} for ${CIRCLE_PIPELINE_NUMBER}"
iso_time=$(date '+%Y-%m-%dT%T%z'| sed -e 's/\([0-9][0-9]\)$/:\1/g')
echo {} | jq \
--arg time_str "$(date +%s)" \
--arg lastUpdated "${iso_time}" \
--arg state "${JIRA_BUILD_STATUS}" \
--arg buildNumber "${CIRCLE_BUILD_NUM}" \
--arg pipelineNumber "${CIRCLE_PIPELINE_NUMBER}" \
--arg projectName "${CIRCLE_PROJECT_REPONAME}" \
--arg url "${CIRCLE_BUILD_URL}" \
--arg commit "${CIRCLE_SHA1}" \
--arg refUri "${CIRCLE_REPOSITORY_URL}/tree/${CIRCLE_BRANCH}" \
--arg repositoryUri "${CIRCLE_REPOSITORY_URL}" \
--arg branchName "${CIRCLE_BRANCH}" \
--arg workflowId "${CIRCLE_WORKFLOW_ID}" \
--arg workflowUrl "https://circleci.com/workflow-run/${CIRCLE_WORKFLOW_ID}" \
--arg repoName "${CIRCLE_PROJECT_REPONAME}" \
--arg pipelineDisplay "#${CIRCLE_PIPELINE_NUMBER} ${CIRCLE_PROJECT_REPONAME}" \
--arg deployDisplay "#${CIRCLE_PIPELINE_NUMBER} ${CIRCLE_PROJECT_REPONAME} - <<parameters.environment>>" \
--arg description "${CIRCLE_PROJECT_REPONAME} #${CIRCLE_PIPELINE_NUMBER} ${CIRCLE_JOB} <<parameters.environment>>" \
--arg envId "${CIRCLE_WORKFLOW_ID}-<<parameters.environment>>" \
--arg envName "<<parameters.environment>>" \
--arg envType "<<parameters.environment_type>>" \
--argjson issueKeys "${ISSUE_KEYS}" \
'
($time_str | tonumber) as $time_num |
{
"deployments": [
{
"schemaVersion": "1.0",
"pipeline": {
"id": $repoName,
"displayName": $pipelineDisplay,
"url": $workflowUrl
},
"deploymentSequenceNumber": $pipelineNumber,
"updateSequenceNumber": $time_str,
"displayName": $deployDisplay,
"description": $description,
"url": $url,
"state": $state,
"lastUpdated": $lastUpdated,
"associations": [
{
"associationType": "issueKeys",
"values": $issueKeys
},
{
"associationType": "serviceIdOrKeys",
"values": ["<< parameters.service_id >>"]
}
],
"environment":{
"id": $envId,
"displayName": $envName,
"type": $envType
}
}
]
}
' > /tmp/jira-status.json
}
post_to_jira () {
HTTP_STATUS=$(curl \
-u "${<<parameters.token_name>>}:" \
-s -w "%{http_code}" -o /tmp/curl_response.txt \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-X POST "https://circleci.com/api/v1.1/project/${VCS_TYPE}/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/jira/<<parameters.job_type>>" --data @/tmp/jira-status.json)
echo "Results from Jira: "
if [ "${HTTP_STATUS}" != "200" ];then
echo "Error calling Jira, result: ${HTTP_STATUS}" >&2
jq '.' /tmp/curl_response.txt
exit 0
fi
case "<<parameters.job_type>>" in
"build")
if jq -e '.unknownIssueKeys[0]' /tmp/curl_response.txt > /dev/null; then
echo "ERROR: unknown issue key"
jq '.' /tmp/curl_response.txt
exit 0
fi
;;
"deployment")
if jq -e '.unknownAssociations[0]' /tmp/curl_response.txt > /dev/null; then
echo "ERROR: unknown association"
jq '.' /tmp/curl_response.txt
exit 0
fi
if jq -e '.rejectedDeployments[0]' /tmp/curl_response.txt > /dev/null; then
echo "ERROR: Deployment rejected"
jq '.' /tmp/curl_response.txt
exit 0
fi
;;
esac
# If reached this point, the deployment was a success.
echo
jq '.' /tmp/curl_response.txt
echo
echo
echo "Success!"
}
# kick off
source <<parameters.state_path>>
run
rm -f <<parameters.state_path>>
name: Update status in Atlassian Jira
when: always
description: |
This Orb reports the status of builds and deployments in CircleCI Projects to your Jira Cloud instance.
Requires that [CircleCI for Jira](https://marketplace.atlassian.com/apps/1215946) be installed in the Jira *Cloud* instance.
Please see [CircleCI Jira integration docs](https://circleci.com/docs/2.0/jira-plugin/)
display:
home_url: https://circleci.com/docs/2.0/jira-plugin/
source_url: https://github.com/CircleCI-Public/jira-connect-orb
examples:
basic_build:
description: Notify jira on single job
usage:
jobs:
build:
docker:
- image: circleci/node:10
steps:
- run: echo "hello"
orbs:
jira: circleci/jira@x.y.z
version: 2.1
workflows:
build:
jobs:
- build:
post-steps:
- jira/notify
full_workflow:
description: Includes multiple buld jobs and deployment jobs.
usage:
jobs:
component-A:
docker:
- image: circleci/node:10
steps:
- checkout
- run: exit 0
- jira/notify
working_directory: ~/repo
component-B:
docker:
- image: circleci/node:10
steps:
- checkout
- run: sleep 2
- jira/notify
working_directory: ~/repo
deploy:
docker:
- image: circleci/node:10
steps:
- checkout
- run: echo "hi Jira"
working_directory: ~/repo
integration:
docker:
- image: circleci/node:10
steps:
- checkout
- run: echo "hi Jira"
- jira/notify
working_directory: ~/repo
orbs:
jira: circleci/jira@x.y.z
version: 2.1
workflows:
build-deploy:
jobs:
- component-A
- component-B
- integration:
requires:
- component-A
- component-B
- deploy:
name: Dev East
post-steps:
- jira/notify:
environment_type: development
job_type: deployment
requires:
- integration
- deploy:
name: Test East
post-steps:
- jira/notify:
environment_type: testing
job_type: deployment
requires:
- Dev East
- hold:
name: Verify
requires:
- Test East
type: approval
- deploy:
name: Prod East
post-steps:
- jira/notify:
environment_type: production
job_type: deployment
requires:
- Verify
orbs:
jq: circleci/jq@2.2
version: 2.1