forked from atlassian-labs/Compass-Orb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorb.yml
More file actions
173 lines (165 loc) · 7.04 KB
/
orb.yml
File metadata and controls
173 lines (165 loc) · 7.04 KB
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
version: 2.1
description: |
This is a fork of the mainline https://go.atlassian.com/compass-circleci-integration. This Orb reports the status of deployments in CircleCI Projects to your Compass instance. Requires that CircleCI for Compass be installed in the Compass instance.
orbs:
jq: circleci/jq@2.2
commands:
notify_deployment:
description: |
Report builds or deployments to Compass based on job type.
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
state_path:
default: ./circleci-orb-compass.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: env_var_name
steps:
- jq/install:
when: always
- run:
command: |
echo 'COMPASS_BUILD_STATUS="FAILED"' > $STATE_PATH
environment:
STATE_PATH: <<parameters.state_path>>
name: COMPASS - Setting Failure Condition
when: on_fail
- run:
command: |
echo 'COMPASS_BUILD_STATUS="SUCCESSFUL"' > $STATE_PATH
environment:
STATE_PATH: <<parameters.state_path>>
name: COMPASS - Setting Success Condition
when: on_success
- run:
command: |4-
echo "export ENVIRONMENT_NAME=${ENVIRONMENT_NAME}" >> $BASH_ENV
source $BASH_ENV
echo "Using displayName: $ENVIRONMENT_NAME"
environment:
ENVIRONMENT_NAME: <<parameters.environment>>
name: COMPASS - Set Environment Name
when: always
- run:
command: |-
run () {
verify_env_variables
generate_json_payload_deployment
post_to_compass
}
verify_env_variables () {
if [[ ! ${COMPASS_SHARED_SECRET} || ! ${COMPASS_WEBTRIGGER} ]]; then
echo "The environment variables required for the Compass orb aren’t configured. Please check if you’ve configured the integration correctly in the Compass UI."
exit 0
fi
}
generate_json_payload_deployment () {
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 category "${ENVIRONMENT_TYPE^^}" \
--arg environmentName "${ENVIRONMENT_NAME}" \
--arg environmentType "${ENVIRONMENT_TYPE}" \
--arg workflowId "${CIRCLE_WORKFLOW_ID}" \
--arg jobId "${CIRCLE_BUILD_NUM}" \
--arg status "${COMPASS_BUILD_STATUS}" \
'
($time_str | tonumber) as $time_num |
{
"workflowId": $workflowId,
"jobId": $jobId,
"lastUpdated": $lastUpdated,
"status": $status,
"deployments": [
{
"environment": {
"category": $category,
"displayName": $environmentName,
"environmentId": $environmentType
},
"lastUpdated": $lastUpdated
}
]
}
' > /tmp/compass-status.json
}
post_to_compass () {
cat /tmp/compass-status.json
eval TOKEN=\$$TOKEN_NAME #most portable way to use dynamic variable name
HTTP_STATUS=$(curl \
-u "${TOKEN}:" \
-s -w "%{http_code}" -o /tmp/curl_response.txt \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "x-forge-secret: ${COMPASS_SHARED_SECRET}" \
-X POST "${COMPASS_WEBTRIGGER}" --data @/tmp/compass-status.json)
echo "Results from Compass: "
if [ "${HTTP_STATUS}" != "200" ];then
echo "Error calling Compass, result: ${HTTP_STATUS}" >&2
jq '.' /tmp/curl_response.txt
exit 0
fi
# If reached this point, the deployment was a success.
echo
jq '.' /tmp/curl_response.txt
echo
echo
echo "Success!"
}
# kick off
source ${STATE_PATH}
run
rm -f ${STATE_PATH}
environment:
ENVIRONMENT_TYPE: <<parameters.environment_type>>
STATE_PATH: <<parameters.state_path>>
TOKEN_NAME: <<parameters.token_name>>
name: Update status in Atlassian Compass
when: always
examples:
basic_deployment:
description: |
Report deployments to Compass
usage:
version: "2.1"
orbs:
compass: moneytree/compass@x.y.z
jobs:
deploy:
docker:
- image: cimg/base:2021.04
steps:
- run: echo "hello"
workflows:
deploy:
jobs:
- deploy:
context:
- compass-integration-<compass-site-name>
post-steps:
- compass/notify_deployment