forked from CircleCI-Public/orb-tools-orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.sh
112 lines (97 loc) · 3.68 KB
/
migrate.sh
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
#!/bin/bash
# Use this tool to assist in migrating your Orb Development Kit pipeline.
verify_run() {
CHECK_FAILED=false
# Ensure .circleci/config.yml exists
if [ ! -f .circleci/config.yml ]; then
echo "No .circleci/config.yml found"
echo "This does not appear to be the root of a CircleCI project"
CHECK_FAILED=true
fi
# Ensure jq is installed
if ! command -v jq >/dev/null 2>&1; then
echo "Looks like you don't have \"jq\" installed"
echo "Please install it and run the script again: https://stedolan.github.io/jq/download/."
CHECK_FAILED=true
fi
# Ensure yq is installed
if ! command -v yq >/dev/null 2>&1; then
echo "Looks like you don't have \"yq\" installed"
echo "Please install it and run the script again: https://github.com/mikefarah/yq#install."
CHECK_FAILED=true
fi
if [ "$CHECK_FAILED" == true ]; then
exit 1
fi
}
backup_contents() {
# Backup the existing files in .circleci/
for file in .circleci/*; do
if [ -f "$file" ]; then
mv "$file" "$file.bak"
fi
done
}
download_template() {
ORB_TEMPLATE_TAG_LIST=$(curl --request GET \
--url https://api.github.com/repos/CircleCI-Public/Orb-Template/tags \
--header 'Accept: application/vnd.github.v3+json' | jq -r '.[].name' | grep -v '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -Vr)
ORB_TEMPLATE_VERSION=$(echo "$ORB_TEMPLATE_TAG_LIST" | head -n 1)
ORB_TEMPLATE_DOWNLOAD_URL="https://github.com/CircleCI-Public/Orb-Template/archive/refs/tags/${ORB_TEMPLATE_VERSION}.tar.gz"
ORB_TEMP_DIR=$(mktemp -d)
curl -Ls "$ORB_TEMPLATE_DOWNLOAD_URL" -o "$ORB_TEMP_DIR/orb-template.tar.gz"
tar -xzf "$ORB_TEMP_DIR/orb-template.tar.gz" -C "$ORB_TEMP_DIR" --strip-components 1
cp -r "${ORB_TEMP_DIR}/.circleci/." .circleci/
if [[ ! -e .yamllint ]]; then
cp "${ORB_TEMP_DIR}/.yamllint" .yamllint
fi
}
copy_custom_components() {
ORIGINAL_EXECUTORS=$(yq '.executors' .circleci/config.yml.bak)
ORIGINAL_JOBS=$(yq '.jobs' .circleci/config.yml.bak)
ORIGINAL_COMMANDS=$(yq '.commands' .circleci/config.yml.bak)
export ORIGINAL_EXECUTORS
export ORIGINAL_JOBS
export ORIGINAL_COMMANDS
if [[ -n "$ORIGINAL_EXECUTORS" && ! "$ORIGINAL_EXECUTORS" == "null" ]]; then
yq -i '. += {"executors": env(ORIGINAL_EXECUTORS)}' .circleci/test-deploy.yml
fi
if [[ -n "$ORIGINAL_JOBS" && ! "$ORIGINAL_JOBS" == "null" ]]; then
yq -i '. += {"jobs": env(ORIGINAL_JOBS)}' .circleci/test-deploy.yml
fi
if [[ -n "$ORIGINAL_COMMANDS" && ! "$ORIGINAL_COMMANDS" == "null" ]]; then
yq -i '. += {"commands": env(ORIGINAL_COMMANDS)}' .circleci/test-deploy.yml
fi
}
user_input() {
read -rp 'Namespace: ' ORB_NAMESPACE
read -rp 'Orb name: ' ORB_NAME
read -rp 'Context name: ' ORB_CONTEXT_NAME
}
replace_values() {
sed -i '' "s/<namespace>/$ORB_NAMESPACE/g" .circleci/config.yml
sed -i '' "s/<orb-name>/$ORB_NAME/g" .circleci/config.yml
sed -i '' "s/<publishing-context>/$ORB_CONTEXT_NAME/g" .circleci/config.yml
sed -i '' "s/<namespace>/$ORB_NAMESPACE/g" .circleci/test-deploy.yml
sed -i '' "s/<orb-name>/$ORB_NAME/g" .circleci/test-deploy.yml
sed -i '' "s/<publishing-context>/$ORB_CONTEXT_NAME/g" .circleci/test-deploy.yml
}
msg_success() {
echo "Successfully upgraded config files."
echo "You must now open \"test-deploy.yml\" and add your integrations tests."
echo "Docs: https://circleci.com/docs/2.0/testing-orbs/#integration-testing"
echo
echo "When complete, delete the '.bak' files in the .circleci directory."
echo 'Commit your changes and the next version of your orb will be published when a tag is created.'
}
destroy_script() {
rm -f "$0"
}
verify_run
user_input
backup_contents
download_template
replace_values
copy_custom_components
msg_success
destroy_script