-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathpush-env-key.sh
More file actions
executable file
·162 lines (132 loc) · 4.26 KB
/
push-env-key.sh
File metadata and controls
executable file
·162 lines (132 loc) · 4.26 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
#!/bin/bash
set -euo pipefail
REMOTE_HOST="jack"
REMOTE_FILE="env.json"
LOCAL_ENV="env.json"
usage() {
cat <<EOF
Push an env.json key to the Jenkins env.json on $REMOTE_HOST.
Inserts the key alphabetically within the correct section.
Usage: $(basename "$0") [-m MESSAGE] [-f FOLDER] <KEY> [JSON_VALUE]
KEY Top-level key in env.json (e.g. XGRAM_INIT)
JSON_VALUE JSON value to set (any valid JSON). If omitted, extracted from local env.json.
-m MESSAGE Custom commit message (default: "Set <KEY> in env.json")
-f FOLDER Remote folder under ~/jenkins-files (default: master)
Examples:
$(basename "$0") XGRAM_INIT
$(basename "$0") -f testnet XGRAM_INIT '{"apiKey": "abc123"}'
$(basename "$0") -m "Enable xgram exchange plugin" -f master XGRAM_INIT
EOF
exit 1
}
COMMIT_MSG=""
TARGET_FOLDER=""
while getopts "m:f:h" opt; do
case $opt in
m) COMMIT_MSG="$OPTARG" ;;
h) usage ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
[ $# -lt 1 ] && usage
KEY="$1"
shift
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
if [ $# -gt 0 ]; then
VALUE="$1"
echo "$VALUE" | jq . > /dev/null 2>&1 || { echo "Error: Invalid JSON value" >&2; exit 1; }
else
VALUE=$(jq --arg key "$KEY" '.[$key]' "$REPO_ROOT/$LOCAL_ENV")
if [ "$VALUE" = "null" ]; then
echo "Error: Key '$KEY' not found in local $LOCAL_ENV" >&2
exit 1
fi
fi
TARGET_FOLDER="${TARGET_FOLDER:-master}"
TARGET_FOLDER="${TARGET_FOLDER#/}"
TARGET_FOLDER="${TARGET_FOLDER%/}"
[ -z "$TARGET_FOLDER" ] && TARGET_FOLDER="master"
REMOTE_BASE='$HOME/jenkins-files'
REMOTE_REPO="$REMOTE_BASE/$TARGET_FOLDER"
[ -z "$COMMIT_MSG" ] && COMMIT_MSG="Set $KEY in $REMOTE_FILE"
echo "Key: $KEY"
echo "Value: $VALUE"
echo "Folder: $TARGET_FOLDER"
echo "Message: $COMMIT_MSG"
echo ""
REMOTE_TMP=$(mktemp)
VALUE_TMP=$(mktemp)
RESULT_TMP=$(mktemp)
trap 'rm -f "$REMOTE_TMP" "$VALUE_TMP" "$RESULT_TMP"' EXIT
echo "Pulling latest on $REMOTE_HOST..."
ssh "$REMOTE_HOST" "cd $REMOTE_REPO && git pull --ff-only" >&2
echo "Fetching remote $REMOTE_FILE..."
scp -q "$REMOTE_HOST:$REMOTE_REPO/$REMOTE_FILE" "$REMOTE_TMP"
printf '%s' "$VALUE" > "$VALUE_TMP"
echo "Merging $KEY into correct section..."
python3 - "$KEY" "$VALUE_TMP" "$REMOTE_TMP" "$REPO_ROOT/$LOCAL_ENV" > "$RESULT_TMP" <<'PYEOF'
import json
import sys
SECTION_MARKER = "--------"
key = sys.argv[1]
with open(sys.argv[2]) as f:
value = json.loads(f.read())
with open(sys.argv[3]) as f:
remote = json.loads(f.read())
with open(sys.argv[4]) as f:
local = json.loads(f.read())
# Find which section the key belongs to using local env.json key order
target_section = None
current_section = None
for k in local:
if SECTION_MARKER in k:
current_section = k
elif k == key:
target_section = current_section
break
if target_section is None:
print(f"Warning: '{key}' not in local env.json; appending to last section",
file=sys.stderr)
# Split remote keys into sections: [(header, [key, ...])]
sections = []
cur_header = None
cur_keys = []
for k in remote:
if SECTION_MARKER in k:
sections.append((cur_header, cur_keys))
cur_header = k
cur_keys = []
else:
cur_keys.append(k)
sections.append((cur_header, cur_keys))
# Remove key from wherever it currently sits
for i, (h, keys) in enumerate(sections):
sections[i] = (h, [k for k in keys if k != key])
# Insert key into the target section
added = False
for i, (h, keys) in enumerate(sections):
if h == target_section:
keys.append(key)
added = True
break
if not added:
sections[-1][1].append(key)
# Sort keys alphabetically within each section
for i, (h, keys) in enumerate(sections):
sections[i] = (h, sorted(keys))
# Reconstruct ordered dict, pulling values from remote (or using new value)
result = {}
for h, keys in sections:
if h is not None:
result[h] = remote[h]
for k in keys:
result[k] = value if k == key else remote[k]
print(json.dumps(result, indent=2))
PYEOF
echo "Pushing to $REMOTE_HOST..."
scp -q "$RESULT_TMP" "$REMOTE_HOST:$REMOTE_REPO/$REMOTE_FILE"
ESCAPED_MSG=$(printf '%q' "$COMMIT_MSG")
ssh "$REMOTE_HOST" "cd $REMOTE_REPO && git add $REMOTE_FILE && git diff --cached --stat && git commit -m $ESCAPED_MSG && git push"
echo "Done."