forked from kandji-inc/kst
-
Notifications
You must be signed in to change notification settings - Fork 0
292 lines (268 loc) · 9.49 KB
/
kst-push.yml
File metadata and controls
292 lines (268 loc) · 9.49 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
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
name: Kandji Sync Toolkit Push
on:
workflow_call:
inputs:
debug:
description: "Enable debug log to console"
required: false
default: false
type: boolean
force:
description: "Force push to overwrite changes in Kandji"
required: false
default: true
type: boolean
clean:
description: "Delete profiles and scripts if missing from the GitHub repository"
required: false
default: true
type: boolean
secrets:
kst_tenant:
description: "Your Kandji tenant's API URL"
required: true
kst_token:
description: "Your Kandji tenant's API token"
required: true
slack_webhook_url:
description: "Slack webhook URL for sending notifications"
required: false
jobs:
push-to-kandji:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
env:
KST_TENANT: ${{ secrets.kst_tenant }}
KST_TOKEN: ${{ secrets.kst_token }}
KST_REPORT: "/home/runner/.local/state/kst/log/kst_report.json"
steps:
- name: Clone Repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.13"
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
version: "0.7.2"
enable-cache: true
- name: Install kst
run: uv tool install --python 3.13 kst
- name: Display kst Version
run: kst --version
- name: Configure kst options
run: |
if [[ "${{ inputs.debug }}" == "true" ]]; then
echo "Debug logging enabled"
echo debug_opts="--debug --log -" >> $GITHUB_ENV
fi
declare -a includes=(
"--include" "conflict"
"--include" "new_local"
"--include" "updated_local"
)
declare -a options=()
if [[ "${{ inputs.force }}" == "true" ]]; then
echo "Force pull enabled, local updates will be overwritten"
options+=("--force")
includes+=("--include" "updated_remote")
fi
if [[ "${{ inputs.clean }}" == "true" ]]; then
echo "Clean option enabled, local profiles and scripts will be deleted if missing from Kandji"
options+=("--clean")
includes+=("--include" "new_remote")
fi
echo "includes=${includes[*]}" >> $GITHUB_ENV
echo "options=${options[*]}" >> $GITHUB_ENV
- name: Configure github-actions[bot] user
run : |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Ensure .kst marker file
run: |
if [ ! -f ".kst" ]; then
echo "Creating .kst marker file"
touch .kst
git add .kst
git commit -m "Add .kst marker file"
fi
- name: Show Pending Profile Changes
run: kst ${debug_opts} profile list ${includes[*]}
- name: Push Profiles to Kandji
run: kst ${debug_opts} profile push --all ${options[*]}
- name: Check Profiles Report
id: profiles-report
run: |
echo "summary=$(jq '.[0].summary' $KST_REPORT)" >> $GITHUB_OUTPUT
# Set the message and color based on the status
case "$(jq -r '.[0].status' $KST_REPORT)" in
"success")
message="Custom profiles were updated in Kandji."
color="#36a64f"
;;
"warning")
message="Warnings occurred while updating profiles in Kandji."
color="#f2c717"
;;
"failed")
message="Errors occurred while updating profiles in Kandji."
color="#ed1f1f"
;;
*)
message="An unknown status occrued while updating profiles in Kandji: $status"
color="#f2c717"
;;
esac
echo "message=${message}" >> $GITHUB_OUTPUT
echo "color=${color}" >> $GITHUB_OUTPUT
- name: Send Profiles Result Slack Notification
if: ${{ steps.profiles-report.outputs.summary != '""' }}
uses: slackapi/slack-github-action@v2.0.0
with:
webhook: ${{ secrets.slack_webhook_url }}
webhook-type: incoming-webhook
payload: |
{
"attachments": [
{
"color": "${{ steps.profiles-report.outputs.color }}",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "${{ steps.profiles-report.outputs.message }}"
}
},
{
"type": "section",
"text": {
"type": "plain_text",
"text": ${{ steps.profiles-report.outputs.summary }}
}
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|GitHub Action Log>"
}
]
}
]
}
]
}
- name: Show Pending Script Changes
run: kst ${debug_opts} script list ${includes[*]}
- name: Push Scripts to Kandji
run: kst ${debug_opts} script push --all ${options[*]}
- name: Check Scripts Report
id: scripts-report
run: |
echo "summary=$(jq '.[0].summary' $KST_REPORT)" >> $GITHUB_OUTPUT
# Set the message and color based on the status
case "$(jq -r '.[0].status' $KST_REPORT)" in
"success")
message="Custom scripts were updated in Kandji."
color="#36a64f"
;;
"warning")
message="Warnings occurred while updating scripts in Kandji."
color="#f2c717"
;;
"failed")
message="Errors occurred while updating scripts in Kandji."
color="#ed1f1f"
;;
*)
message="An unknown status occrued while updating scripts in Kandji: $status"
color="#f2c717"
;;
esac
echo "message=${message}" >> $GITHUB_OUTPUT
echo "color=${color}" >> $GITHUB_OUTPUT
- name: Send Scripts Result Slack Notification
if: ${{ steps.scripts-report.outputs.summary != '""' }}
uses: slackapi/slack-github-action@v2.0.0
with:
webhook: ${{ secrets.slack_webhook_url }}
webhook-type: incoming-webhook
payload: |
{
"attachments": [
{
"color": "${{ steps.scripts-report.outputs.color }}",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "${{ steps.scripts-report.outputs.message }}"
}
},
{
"type": "section",
"text": {
"type": "plain_text",
"text": ${{ steps.scripts-report.outputs.summary }}
}
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|GitHub Action Log>"
}
]
}
]
}
]
}
- name: Write Changes to the GitHub Repository
run: git push
- name: Send Slack Notification on Job Failure
if: ${{ failure() }}
uses: slackapi/slack-github-action@v2.0.0
with:
webhook: ${{ secrets.slack_webhook_url }}
webhook-type: incoming-webhook
payload: |
{
"attachments": [
{
"color": "#ed1f1f",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Kandji Sync Toolkit Push Workflow Error"
}
},
{
"type": "section",
"text": {
"type": "plain_text",
"text": "Steps failed while executing the workflow. Please check the link for more details."
}
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|GitHub Action Log>"
}
]
}
]
}
]
}