-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv0.plugin.yaml
More file actions
455 lines (397 loc) · 17.4 KB
/
env0.plugin.yaml
File metadata and controls
455 lines (397 loc) · 17.4 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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
name: Overmind
icon: https://avatars.githubusercontent.com/u/93521445
inputs:
action:
description: "The action to perform. Must be one of: submit-plan, start-change, end-change, wait-for-simulation"
required: true
api_key:
description: "Overmind API key for authentication"
required: true
tags:
description: "A comma-separated list of key=value tags to attach to the change (only used with submit-plan action)"
required: false
app:
description: "The Overmind instance to connect to (defaults to https://app.overmind.tech)"
required: false
post_comment:
description: "Whether wait-for-simulation should post the Overmind markdown to the PR/MR. Defaults to true when ENV0_PR_NUMBER is set, otherwise false. When true, comment_provider must be set."
required: false
comment_provider:
description: "Where to post PR/MR comments when wait-for-simulation runs with post_comment=true. Must be one of: github, gitlab."
required: false
run:
exec: |
#!/bin/sh
set -e
TMP_DIR=""
GH_TMP_DIR=""
MARKDOWN_FILE=""
JSON_PAYLOAD_FILE=""
ORIGINAL_DIR=$(pwd)
cleanup() {
if [ -n "${TMP_DIR}" ] && [ -d "${TMP_DIR}" ]; then
rm -rf "${TMP_DIR}"
fi
if [ -n "${GH_TMP_DIR}" ] && [ -d "${GH_TMP_DIR}" ]; then
rm -rf "${GH_TMP_DIR}"
fi
if [ -n "${MARKDOWN_FILE}" ] && [ -f "${MARKDOWN_FILE}" ]; then
rm -f "${MARKDOWN_FILE}"
fi
if [ -n "${JSON_PAYLOAD_FILE}" ] && [ -f "${JSON_PAYLOAD_FILE}" ]; then
rm -f "${JSON_PAYLOAD_FILE}"
fi
}
trap cleanup EXIT
# Export API key as environment variable
export OVM_API_KEY="${inputs.api_key}"
# Install Overmind CLI
REPO="overmindtech/cli"
OS=$(uname -s)
ARCH=$(uname -m)
case "${ARCH}" in
x86_64) ARCH="x86_64" ;;
aarch64|arm64) ARCH="arm64" ;;
i386|i686) ARCH="i386" ;;
*) echo "Unsupported architecture: ${ARCH}"; exit 1 ;;
esac
case "${OS}" in
Linux|Darwin) ARCHIVE_EXT="tar.gz" ;;
MINGW*|MSYS*|CYGWIN*) OS="Windows"; ARCHIVE_EXT="zip" ;;
*) echo "Unsupported OS: ${OS}"; exit 1 ;;
esac
# Function to find a writable directory in PATH
find_install_dir() {
IFS=':'
for dir in $PATH; do
[ -z "$dir" ] && continue
# Create directory if it doesn't exist (if parent is writable)
if [ ! -d "$dir" ]; then
mkdir -p "$dir" 2>/dev/null || continue
fi
# Check if directory is writable
if [ -w "$dir" ]; then
echo "$dir"
unset IFS
return 0
fi
done
unset IFS
return 1
}
INSTALL_DIR=$(find_install_dir)
if [ -z "${INSTALL_DIR}" ]; then
echo "Error: No writable directory found in PATH"
echo "Current PATH: ${PATH}"
exit 1
fi
echo "Installing to: ${INSTALL_DIR}"
echo "Downloading latest Overmind release..."
FILENAME="overmind_cli_${OS}_${ARCH}.${ARCHIVE_EXT}"
DOWNLOAD_URL="https://github.com/${REPO}/releases/latest/download/${FILENAME}"
echo "Downloading ${FILENAME}..."
TMP_DIR=$(mktemp -d)
cd "${TMP_DIR}"
curl -fsSL "${DOWNLOAD_URL}" -o archive
if [ "${ARCHIVE_EXT}" = "tar.gz" ]; then
tar -xzf archive
else
unzip -q archive
fi
if [ "${OS}" = "Windows" ]; then
EXPECTED_BINARY="overmind.exe"
else
EXPECTED_BINARY="overmind"
fi
if [ -f "${EXPECTED_BINARY}" ]; then
BINARY="./${EXPECTED_BINARY}"
else
BINARY=$(find . -maxdepth 2 -type f -executable -name "overmind*" ! -name "*.txt" ! -name "*.md" ! -name "LICENSE" | head -n 1)
fi
if [ -z "${BINARY}" ]; then
echo "Error: Could not find binary in archive"
ls -la
exit 1
fi
cp "${BINARY}" "${INSTALL_DIR}/overmind"
chmod +x "${INSTALL_DIR}/overmind"
echo "✓ Overmind CLI installed to ${INSTALL_DIR}/overmind"
# Verify installation
"${INSTALL_DIR}/overmind" version 2>/dev/null || echo "Installation complete"
cd "${ORIGINAL_DIR}"
# Install GitHub CLI
GH_REPO="cli/cli"
echo "Installing GitHub CLI..."
case "${ARCH}" in
x86_64) GH_ARCH="amd64" ;;
arm64) GH_ARCH="arm64" ;;
i386) GH_ARCH="386" ;;
*) echo "Unsupported architecture for GitHub CLI: ${ARCH}"; exit 1 ;;
esac
case "${OS}" in
Linux) GH_OS="linux" ;;
Darwin) GH_OS="macOS" ;;
Windows) GH_OS="windows" ;;
*) echo "Unsupported OS for GitHub CLI: ${OS}"; exit 1 ;;
esac
if [ "${OS}" = "Windows" ]; then
GH_ARCHIVE_EXT="zip"
GH_BINARY_NAME="gh.exe"
GH_INSTALL_PATH="${INSTALL_DIR}/gh.exe"
else
GH_ARCHIVE_EXT="tar.gz"
GH_BINARY_NAME="gh"
GH_INSTALL_PATH="${INSTALL_DIR}/gh"
fi
GH_LATEST_VERSION=$(curl -fsSL "https://api.github.com/repos/${GH_REPO}/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [ -z "${GH_LATEST_VERSION}" ]; then
echo "Failed to fetch latest GitHub CLI version"
exit 1
fi
GH_VERSION_NUMBER=$(echo "${GH_LATEST_VERSION}" | sed 's/^v//')
GH_FILENAME="gh_${GH_VERSION_NUMBER}_${GH_OS}_${GH_ARCH}.${GH_ARCHIVE_EXT}"
GH_DOWNLOAD_URL="https://github.com/${GH_REPO}/releases/download/${GH_LATEST_VERSION}/${GH_FILENAME}"
echo "Downloading ${GH_FILENAME}..."
GH_TMP_DIR=$(mktemp -d)
cd "${GH_TMP_DIR}"
curl -fsSL "${GH_DOWNLOAD_URL}" -o gh-archive
if [ "${GH_ARCHIVE_EXT}" = "tar.gz" ]; then
tar -xzf gh-archive
else
unzip -q gh-archive
fi
GH_EXTRACTED_DIR=$(find . -maxdepth 1 -type d -name "gh_*" | head -n 1)
if [ -z "${GH_EXTRACTED_DIR}" ]; then
echo "Error: Could not find extracted GitHub CLI directory"
exit 1
fi
GH_BINARY_PATH="${GH_EXTRACTED_DIR}/bin/${GH_BINARY_NAME}"
if [ ! -f "${GH_BINARY_PATH}" ]; then
echo "Error: GitHub CLI binary not found in archive"
exit 1
fi
cp "${GH_BINARY_PATH}" "${GH_INSTALL_PATH}"
chmod +x "${GH_INSTALL_PATH}"
echo "✓ GitHub CLI ${GH_LATEST_VERSION} installed to ${GH_INSTALL_PATH}"
GH_BINARY="${GH_INSTALL_PATH}"
cd "${ORIGINAL_DIR}"
# Construct the env0 deployment URL for the ticket-link
ticket_link="https://app.env0.com/p/${ENV0_PROJECT_ID}/environments/${ENV0_ENVIRONMENT_ID}/deployments/${ENV0_DEPLOYMENT_LOG_ID}?organizationId=${ENV0_ORGANIZATION_ID}"
# Build description from env0 deployment info
description="Env0 Deployment: ${ENV0_PROJECT_NAME} - ${ENV0_ENVIRONMENT_NAME} (${ENV0_DEPLOYMENT_TYPE})"
if [ -n "${ENV0_DEPLOYER_NAME}" ]; then
description="${description} by ${ENV0_DEPLOYER_NAME}"
fi
# Build title from env0 deployment info
title="${ENV0_PROJECT_NAME} - ${ENV0_ENVIRONMENT_NAME}"
if [ -n "${ENV0_TEMPLATE_NAME}" ]; then
title="${ENV0_TEMPLATE_NAME} (${ENV0_ENVIRONMENT_NAME})"
fi
# Execute the appropriate action based on input
case "${inputs.action}" in
submit-plan)
if [ -z "${ENV0_TF_PLAN_JSON}" ]; then
echo "Error: ENV0_TF_PLAN_JSON environment variable is not set"
exit 1
fi
echo "Submitting plan to Overmind..."
# Generate code changes diff if in a git repository
DIFF_FILE=""
if [ -d "${ENV0_TEMPLATE_DIR}/.git" ] && [ -n "${ENV0_TEMPLATE_REVISION}" ]; then
cd "${ENV0_TEMPLATE_DIR}"
if git rev-parse HEAD~1 >/dev/null 2>&1; then
DIFF_FILE=$(mktemp)
git diff HEAD~1 HEAD > "${DIFF_FILE}" 2>/dev/null || true
if [ ! -s "${DIFF_FILE}" ]; then
rm -f "${DIFF_FILE}"
DIFF_FILE=""
fi
fi
fi
# Find terraform plan output file if it exists
PLAN_OUTPUT=""
if [ -f "${ENV0_TEMPLATE_DIR}/.tf-plan-output" ]; then
PLAN_OUTPUT="${ENV0_TEMPLATE_DIR}/.tf-plan-output"
elif [ -f "${ENV0_ROOT_DIR}/.tf-plan-output" ]; then
PLAN_OUTPUT="${ENV0_ROOT_DIR}/.tf-plan-output"
fi
# Determine owner (prefer name over email)
OWNER=""
if [ -n "${ENV0_DEPLOYER_NAME}" ]; then
OWNER="${ENV0_DEPLOYER_NAME}"
elif [ -n "${ENV0_DEPLOYER_EMAIL}" ]; then
OWNER="${ENV0_DEPLOYER_EMAIL}"
fi
# Build submit-plan command with all available information
set -- "${INSTALL_DIR}/overmind" changes submit-plan \
"${ENV0_TF_PLAN_JSON}" \
--title "${title}" \
--description "${description}" \
--ticket-link "${ticket_link}"
if [ -n "${inputs.app}" ]; then
set -- "$@" --app "${inputs.app}"
fi
if [ -n "${ENV0_TEMPLATE_REPOSITORY}" ]; then
set -- "$@" --repo "${ENV0_TEMPLATE_REPOSITORY}"
fi
if [ -n "${OWNER}" ]; then
set -- "$@" --owner "${OWNER}"
fi
if [ -n "${inputs.tags}" ]; then
set -- "$@" --tags "${inputs.tags}"
fi
if [ -n "${PLAN_OUTPUT}" ]; then
set -- "$@" --terraform-plan-output "${PLAN_OUTPUT}"
fi
if [ -n "${DIFF_FILE}" ]; then
set -- "$@" --code-changes-diff "${DIFF_FILE}"
fi
"$@"
# Clean up diff file if created
if [ -n "${DIFF_FILE}" ] && [ -f "${DIFF_FILE}" ]; then
rm -f "${DIFF_FILE}"
fi
;;
start-change)
echo "Starting change in Overmind..."
"${INSTALL_DIR}/overmind" changes start-change \
--ticket-link "${ticket_link}"
;;
end-change)
echo "Ending change in Overmind..."
"${INSTALL_DIR}/overmind" changes end-change \
--ticket-link "${ticket_link}"
;;
wait-for-simulation)
echo "Waiting for Overmind simulation and commenting on PR/MR..."
should_post_comment="${inputs.post_comment}"
if [ -n "${should_post_comment}" ]; then
should_post_comment=$(echo "${should_post_comment}" | tr '[:upper:]' '[:lower:]')
case "${should_post_comment}" in
true|false) ;;
*)
echo "Error: post_comment must be 'true' or 'false'"
exit 1
;;
esac
else
if [ -n "${ENV0_PR_NUMBER}" ]; then
should_post_comment="true"
else
should_post_comment="false"
fi
fi
MARKDOWN_FILE=$(mktemp)
set -- "${INSTALL_DIR}/overmind" changes get-change \
--ticket-link "${ticket_link}" \
--format markdown
if [ -n "${inputs.app}" ]; then
set -- "$@" --app "${inputs.app}"
fi
if ! "$@" > "${MARKDOWN_FILE}"; then
echo "Error: Failed to retrieve change markdown from Overmind"
exit 1
fi
if [ ! -s "${MARKDOWN_FILE}" ]; then
echo "Error: Overmind did not return any markdown content"
exit 1
fi
if [ "${should_post_comment}" = "true" ]; then
if [ -z "${ENV0_PR_NUMBER}" ]; then
echo "Error: ENV0_PR_NUMBER environment variable is not set but post_comment=true"
exit 1
fi
if [ -z "${ENV0_PR_SOURCE_REPOSITORY}" ]; then
echo "Error: ENV0_PR_SOURCE_REPOSITORY environment variable is not set but post_comment=true"
exit 1
fi
comment_provider=$(echo "${inputs.comment_provider}" | tr '[:upper:]' '[:lower:]')
if [ -z "${comment_provider}" ]; then
echo "Error: comment_provider input must be set to 'github' or 'gitlab' when post_comment=true"
exit 1
fi
case "${comment_provider}" in
gitlab)
# GitLab merge request
if [ -z "${GITLAB_TOKEN}" ]; then
echo "Error: GITLAB_TOKEN environment variable is not set but comment_provider=gitlab"
exit 1
fi
# Extract GitLab base URL and project path from repository string
# ENV0_PR_SOURCE_REPOSITORY format: "group/project" or "https://gitlab.com/group/project" or "gitlab.com/group/project"
REPO_STR="${ENV0_PR_SOURCE_REPOSITORY}"
# Remove protocol if present
REPO_STR=$(echo "${REPO_STR}" | sed 's|^https\?://||')
# Extract host (default to gitlab.com if not specified)
if echo "${REPO_STR}" | grep -q "/"; then
GITLAB_HOST=$(echo "${REPO_STR}" | cut -d'/' -f1)
PROJECT_PATH=$(echo "${REPO_STR}" | cut -d'/' -f2-)
else
# If no host, assume gitlab.com
GITLAB_HOST="gitlab.com"
PROJECT_PATH="${REPO_STR}"
fi
# Ensure we have https:// prefix for API calls
if [ "${GITLAB_HOST}" != "gitlab.com" ] && ! echo "${GITLAB_HOST}" | grep -q "^https\?://"; then
GITLAB_BASE_URL="https://${GITLAB_HOST}"
elif [ "${GITLAB_HOST}" = "gitlab.com" ]; then
GITLAB_BASE_URL="https://gitlab.com"
else
GITLAB_BASE_URL="${GITLAB_HOST}"
fi
# URL encode the project path (handle spaces and special chars)
PROJECT_PATH_ENCODED=$(echo "${PROJECT_PATH}" | sed 's|/|%2F|g' | sed 's| |%20|g')
MR_IID="${ENV0_PR_NUMBER}"
API_URL="${GITLAB_BASE_URL}/api/v4/projects/${PROJECT_PATH_ENCODED}/merge_requests/${MR_IID}/notes"
echo "Posting simulation results to GitLab MR ${PROJECT_PATH}!${MR_IID}..."
# Create JSON payload file using jq
JSON_PAYLOAD_FILE=$(mktemp)
jq -n --rawfile body "${MARKDOWN_FILE}" '{body: $body}' > "${JSON_PAYLOAD_FILE}"
# Post comment using curl
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X POST \
-H "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
-H "Content-Type: application/json" \
--data-binary "@${JSON_PAYLOAD_FILE}" \
"${API_URL}")
# Clean up JSON payload file
rm -f "${JSON_PAYLOAD_FILE}"
if [ "${HTTP_CODE}" -ge 200 ] && [ "${HTTP_CODE}" -lt 300 ]; then
echo "✓ Simulation results posted to GitLab MR"
else
echo "Error: Failed to post comment to GitLab MR (HTTP ${HTTP_CODE})"
exit 1
fi
;;
github)
# GitHub pull request
if [ -z "${GH_BINARY}" ] || [ ! -x "${GH_BINARY}" ]; then
echo "Error: GitHub CLI was not installed correctly"
exit 1
fi
if ! "${GH_BINARY}" auth status >/dev/null 2>&1; then
echo "Warning: GitHub CLI authentication not detected. Ensure GH_TOKEN is configured."
fi
echo "Posting simulation results to ${ENV0_PR_SOURCE_REPOSITORY}#${ENV0_PR_NUMBER}..."
"${GH_BINARY}" pr comment "${ENV0_PR_NUMBER}" \
--repo "${ENV0_PR_SOURCE_REPOSITORY}" \
--body-file "${MARKDOWN_FILE}"
echo "✓ Simulation results posted to GitHub PR"
;;
*)
echo "Error: comment_provider input must be 'github' or 'gitlab' when post_comment=true"
exit 1
;;
esac
else
if [ -n "${ENV0_PR_NUMBER}" ]; then
echo "Skipping comment because post_comment=false."
else
echo "Skipping comment because ENV0_PR_NUMBER is not set."
fi
fi
;;
*)
echo "Error: Invalid action '${inputs.action}'. Must be one of: submit-plan, start-change, end-change, wait-for-simulation"
exit 1
;;
esac