-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·116 lines (92 loc) · 4.43 KB
/
entrypoint.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
113
114
115
116
#!/bin/bash
set -e
set -o pipefail
readonly SUPERBLOCKS_CLI_VERSION='^1.4.0'
COMMIT_SHA="${COMMIT_SHA:-HEAD}"
SUPERBLOCKS_DOMAIN="${SUPERBLOCKS_DOMAIN:-app.superblocks.com}"
SUPERBLOCKS_COMMIT_MESSAGE_IDENTIFIER="${SUPERBLOCKS_COMMIT_MESSAGE_IDENTIFIER:-[superblocks ci]}"
SUPERBLOCKS_PATH="${SUPERBLOCKS_PATH:-.}"
SUPERBLOCKS_CONFIG_RELATIVE_PATH=".superblocks/superblocks.json"
# GitHub Actions default environment variables: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
if [ "$GITHUB_ACTIONS" == "true" ]; then
SUPERBLOCKS_AUTHOR_NAME="${SUPERBLOCKS_AUTHOR_NAME:-superblocks-app[bot]}"
SUPERBLOCKS_AUTHOR_EMAIL="${SUPERBLOCKS_AUTHOR_EMAIL:-142439023+superblocks-app[bot]@users.noreply.github.com}"
# GitLab CI predefined variables: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
elif [ "$GITLAB_CI" == "true" ]; then
SUPERBLOCKS_AUTHOR_NAME="${SUPERBLOCKS_AUTHOR_NAME:-superblocks-bot}"
SUPERBLOCKS_AUTHOR_EMAIL="${SUPERBLOCKS_AUTHOR_EMAIL:[email protected]}"
else
printf "\nThis script is only supported on GitLab CI and GitHub Actions. Exiting...\n"
exit 1
fi
# Ensure that a Superblocks token is provided
if [ -z "$SUPERBLOCKS_TOKEN" ]; then
printf "\nThe 'SUPERBLOCKS_TOKEN' environment variable is unset or empty. Exiting...\n"
exit 1
fi
if [ -z "$REPO_DIR" ]; then
REPO_DIR="$(pwd)"
else
cd "$REPO_DIR"
fi
git config --global --add safe.directory "$REPO_DIR"
# Get the actor name and commit message the last commit
actor_name=$(git show -s --format='%an' "$COMMIT_SHA")
commit_message=$(git show -s --format='%B' "$COMMIT_SHA")
# Skip pull if the commit was not made by Superblocks. To support multiple Git providers, we also
# check for the commit message identifier used to identify Superblocks commits.
if [ "$actor_name" != "$SUPERBLOCKS_AUTHOR_NAME" ] && ! echo "$commit_message" | grep -qF "$SUPERBLOCKS_COMMIT_MESSAGE_IDENTIFIER" ; then
printf "\nCommit was not made by Superblocks. Skipping components pull...\n"
exit 0
fi
# Get the list of changed files in the last commit
changed_files=$(git diff "${COMMIT_SHA}"^ --name-only -- "$SUPERBLOCKS_PATH")
# Change the working directory to the Superblocks path
pushd "$SUPERBLOCKS_PATH"
if [ -n "$changed_files" ]; then
# Install Superblocks CLI
npm install -g @superblocksteam/cli@"${SUPERBLOCKS_CLI_VERSION}"
superblocks --version
# Login to Superblocks
printf "\nLogging in to Superblocks...\n"
superblocks config set domain "$SUPERBLOCKS_DOMAIN"
superblocks login -t "$SUPERBLOCKS_TOKEN"
else
printf "\nNo files changed since the last commit. Skipping pull...\n"
exit 0
fi
# Function to pull custom Components for any changed Superblocks application
pull_and_commit() {
local location="$1"
# Escape any special characters in the resource subdir
escaped_location="${location%%/*}/$(printf '%s\n' "${location#*/}" | sed 's/[][\\^$.|?*+(){}/]/\\&/g')"
if echo "$changed_files" | grep -q "${escaped_location}/"; then
printf "\nChange detected. Pulling components for latest commit...\n"
superblocks pull "$location" -m "most-recent-commit"
# Check if any changes were made to the components subdir based on the pull
app_components_dir="${location}/components"
if [ -n "$(git diff --name-only -- "$app_components_dir")" ]; then
printf "\nComponents diff detected between local and remote components. Committing changes...\n"
git config user.name "$SUPERBLOCKS_AUTHOR_NAME"
git config user.email "$SUPERBLOCKS_AUTHOR_EMAIL"
git add "$app_components_dir"
git commit -m "Pull components source code for '$location'" \
-m "[superblocks ci] This commit was automatically generated by Superblocks."
if [ "$GITLAB_CI" == "true" ]; then
git push -o ci.skip origin HEAD
else
git push origin HEAD
fi
else
printf "\nNo components diff detected. Skipping commit...\n"
fi
else
printf "\nNo change detected. Skipping pull...\n"
fi
}
# Check if any Superblocks applications have changed
jq -r '.resources[] | select(.resourceType == "APPLICATION") | .location' "$SUPERBLOCKS_CONFIG_RELATIVE_PATH" | while read -r location; do
printf "\nChecking %s for changes...\n" "$location"
pull_and_commit "$location"
done
printf "\nChecking complete. Exiting...\n"