-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevelopment.sh
executable file
·176 lines (132 loc) · 4.19 KB
/
development.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
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
#!/usr/bin/env bash
set -euo pipefail
# allow running from any working directory
WD=$(dirname "$0")
cd "${WD}"
# By default, the tip of the main branch of the jore4-docker-compose-bundle
# repository is used as the commit reference, which determines the version of
# the Docker Compose bundle to download. For debugging purposes, this default
# can be overridden by some other commit reference (e.g., commit SHA or its
# initial substring), which you can pass via the `BUNDLE_REF` environment
# variable.
DOCKER_COMPOSE_BUNDLE_REF=${BUNDLE_REF:-main}
DOCKER_COMPOSE_CMD="docker compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.custom.yml"
# Download Docker Compose bundle from the "jore4-docker-compose-bundle"
# repository. GitHub CLI is required to be installed.
#
# A commit reference is read from global `DOCKER_COMPOSE_BUNDLE_REF` variable,
# which should be set based on the script execution arguments.
download_docker_compose_bundle() {
local commit_ref="$DOCKER_COMPOSE_BUNDLE_REF"
local repo_name="jore4-docker-compose-bundle"
local repo_owner="HSLdevcom"
# Check GitHub CLI availability.
if ! command -v gh &> /dev/null; then
echo "Please install the GitHub CLI (gh) on your machine."
exit 1
fi
# Make sure the user is authenticated to GitHub.
gh auth status || gh auth login
echo "Using the commit reference '${commit_ref}' to fetch a Docker Compose bundle..."
# First, try to find a commit on GitHub that matches the given reference.
# This function exits with an error code if no matching commit is found.
local commit_sha
commit_sha=$(
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"repos/${repo_owner}/${repo_name}/commits/${commit_ref}" \
--jq '.sha'
)
echo "Commit with the following SHA digest was found: ${commit_sha}"
local zip_file="/tmp/${repo_name}.zip"
local unzip_target_dir_prefix="/tmp/${repo_owner}-${repo_name}"
# Remove old temporary directories if any remain.
rm -fr "$unzip_target_dir_prefix"-*
echo "Downloading the JORE4 Docker Compose bundle..."
# Download Docker Compose bundle from the jore4-docker-compose-bundle
# repository as a ZIP file.
gh api "repos/${repo_owner}/${repo_name}/zipball/${commit_sha}" > "$zip_file"
# Extract ZIP file contents to a temporary directory.
unzip -q "$zip_file" -d /tmp
# Clean untracked files from `docker` directory even if they are git-ignored.
git clean -fx ./docker
echo "Copying JORE4 Docker Compose bundle files to ./docker directory..."
# Copy files from the `docker-compose` directory of the ZIP file to your
# local `docker` directory.
mv "$unzip_target_dir_prefix"-*/docker-compose/* ./docker
# Remove the temporary files and directories created above.
rm -fr "$zip_file" "$unzip_target_dir_prefix"-*
echo "Generating a release version file for the downloaded bundle..."
# Create a release version file containing the SHA digest of the referenced
# commit.
echo "$commit_sha" > ./docker/RELEASE_VERSION.txt
}
start() {
$DOCKER_COMPOSE_CMD up --build -d jore4-auth jore4-testdb
}
stop_all() {
$DOCKER_COMPOSE_CMD stop
}
remove_all() {
$DOCKER_COMPOSE_CMD down
}
build() {
mvn install
}
run_tests() {
mvn test
}
print_usage() {
echo "
Usage: $(basename "$0") <command>
build
Build the project locally.
start
Start Docker containers for authentication service and test database.
You can control which version of the Docker Compose bundle is downloaded by
passing a commit reference to the jore4-docker-compose-bundle repository via
the BUNDLE_REF environment variable. By default, the latest version is
downloaded.
stop
Stop Docker containers.
remove
Stop and remove Docker containers.
test
Run tests locally.
help
Show this usage information.
"
}
COMMAND=${1:-}
if [[ -z $COMMAND ]]; then
print_usage
exit 1
fi
case $COMMAND in
start)
download_docker_compose_bundle
start
;;
stop)
stop_all
;;
remove)
remove_all
;;
help)
usage
;;
build)
build
;;
test)
run_tests
;;
*)
echo ""
echo "Unknown command: '${COMMAND}'"
print_usage
exit 1
;;
esac