Skip to content

Commit 56d4477

Browse files
authored
refactor(shell): FTRS-20911 Fix shell script reliability issues (#841)
1 parent b13490f commit 56d4477

29 files changed

+112
-112
lines changed

scripts/docker/dgoss.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ error() {
2424
cleanup() {
2525
set +e
2626
{ kill "$log_pid" && wait "$log_pid"; } 2> /dev/null
27-
if [ -n "$CONTAINER_LOG_OUTPUT" ]; then
27+
if [[ -n "$CONTAINER_LOG_OUTPUT" ]]; then
2828
cp "$tmp_dir/docker_output.log" "$CONTAINER_LOG_OUTPUT"
2929
fi
3030
rm -rf "$tmp_dir"
@@ -47,7 +47,7 @@ run(){
4747
case "$GOSS_FILES_STRATEGY" in
4848
mount)
4949
info "Starting $CONTAINER_RUNTIME container"
50-
if [ "$CONTAINER_RUNTIME" == "podman" -a $# == 2 ]; then
50+
if [[ "$CONTAINER_RUNTIME" == "podman" && $# == 2 ]]; then
5151
id=$($CONTAINER_RUNTIME run -d -v "$tmp_dir:/goss:z" "${@:2}" sleep infinity)
5252
else
5353
id=$($CONTAINER_RUNTIME run -d -v "$tmp_dir:/goss:z" "${@:2}")
@@ -113,7 +113,7 @@ case "$1" in
113113
fi
114114
[[ $GOSS_SLEEP ]] && { info "Sleeping for $GOSS_SLEEP"; sleep "$GOSS_SLEEP"; }
115115
info "Container health"
116-
if [ "true" != "$($CONTAINER_RUNTIME inspect -f '{{.State.Running}}' "$id")" ]; then
116+
if [[ "true" != "$($CONTAINER_RUNTIME inspect -f '{{.State.Running}}' "$id")" ]]; then
117117
$CONTAINER_RUNTIME logs "$id" >&2
118118
error "the container failed to start"
119119
fi

scripts/docker/docker.lib.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function docker-build() {
5151

5252
# Tag the image with all the stated versions, see the documentation for more details
5353
for version in $(_get-all-effective-versions) latest; do
54-
if [ ! -z "$version" ]; then
54+
if [[ -n "$version" ]]; then
5555
$DOCKER_CMD tag "${tag}" "${DOCKER_IMAGE}:${version}"
5656
fi
5757
done
@@ -150,7 +150,7 @@ function version-create-effective-file() {
150150
local version_file="$dir/VERSION"
151151
local build_datetime=${BUILD_DATETIME:-$(date -u +'%Y-%m-%dT%H:%M:%S%z')}
152152

153-
if [ -f "$version_file" ]; then
153+
if [[ -f "$version_file" ]]; then
154154
# shellcheck disable=SC2002
155155
cat "$version_file" | \
156156
sed "s/\(\${yyyy}\|\$yyyy\)/$(date --date="${build_datetime}" -u +"%Y")/g" | \
@@ -190,9 +190,9 @@ function docker-get-image-version-and-pull() {
190190
# match it by name and version regex, if given.
191191
local versions_file="${TOOL_VERSIONS:=$(git rev-parse --show-toplevel)/.tool-versions}"
192192
local version="latest"
193-
if [ -f "$versions_file" ]; then
193+
if [[ -f "$versions_file" ]]; then
194194
line=$(grep "docker/${name} " "$versions_file" | sed "s/^#\s*//; s/\s*#.*$//" | grep "${match_version:-".*"}")
195-
[ -n "$line" ] && version=$(echo "$line" | awk '{print $2}')
195+
[[ -n "$line" ]] && version=$(echo "$line" | awk '{print $2}')
196196
fi
197197

198198
# Split the image version into two, tag name and digest sha256.
@@ -201,7 +201,7 @@ function docker-get-image-version-and-pull() {
201201

202202
# Check if the image exists locally already
203203
if ! $DOCKER_CMD images | awk '{ print $1 ":" $2 }' | grep -q "^${name}:${tag}$"; then
204-
if [ "$digest" != "latest" ]; then
204+
if [[ "$digest" != "latest" ]]; then
205205
# Pull image by the digest sha256 and tag it
206206
$DOCKER_CMD pull \
207207
--platform linux/amd64 \
@@ -234,7 +234,7 @@ function _create-effective-dockerfile() {
234234
# Dockerfile.effective file, otherwise docker won't use it.
235235
# See https://docs.docker.com/build/building/context/#filename-and-location
236236
# If using podman, this requires v5.0.0 or later.
237-
if [ -f "${dir}/Dockerfile.dockerignore" ]; then
237+
if [[ -f "${dir}/Dockerfile.dockerignore" ]]; then
238238
cp "${dir}/Dockerfile.dockerignore" "${dir}/Dockerfile.effective.dockerignore"
239239
fi
240240
cp "${dir}/Dockerfile" "${dir}/Dockerfile.effective"
@@ -252,19 +252,19 @@ function _replace-image-latest-by-specific-version() {
252252
local dockerfile="${dir}/Dockerfile.effective"
253253
local build_datetime=${BUILD_DATETIME:-$(date -u +'%Y-%m-%dT%H:%M:%S%z')}
254254

255-
if [ -f "$versions_file" ]; then
255+
if [[ -f "$versions_file" ]]; then
256256
# First, list the entries specific for Docker to take precedence, then the rest but exclude comments
257257
content=$(grep " docker/" "$versions_file"; grep -v " docker/" "$versions_file" ||: | grep -v "^#")
258258
echo "$content" | while IFS= read -r line; do
259-
[ -z "$line" ] && continue
259+
[[ -z "$line" ]] && continue
260260
line=$(echo "$line" | sed "s/^#\s*//; s/\s*#.*$//" | sed "s;docker/;;")
261261
name=$(echo "$line" | awk '{print $1}')
262262
version=$(echo "$line" | awk '{print $2}')
263263
sed -i "s;\(FROM .*\)${name}:latest;\1${name}:${version};g" "$dockerfile"
264264
done
265265
fi
266266

267-
if [ -f "$dockerfile" ]; then
267+
if [[ -f "$dockerfile" ]]; then
268268
# shellcheck disable=SC2002
269269
cat "$dockerfile" | \
270270
sed "s/\(\${yyyy}\|\$yyyy\)/$(date --date="${build_datetime}" -u +"%Y")/g" | \
@@ -314,7 +314,7 @@ function _get-effective-tag() {
314314

315315
local tag=$DOCKER_IMAGE
316316
version=$(_get-effective-version)
317-
if [ ! -z "$version" ]; then
317+
if [[ -n "$version" ]]; then
318318
tag="${tag}:${version}"
319319
fi
320320
echo "$tag"
@@ -336,9 +336,9 @@ function _get-git-branch-name() {
336336

337337
local branch_name=$(git rev-parse --abbrev-ref HEAD)
338338

339-
if [ -n "${GITHUB_HEAD_REF:-}" ]; then
339+
if [[ -n "${GITHUB_HEAD_REF:-}" ]]; then
340340
branch_name=$GITHUB_HEAD_REF
341-
elif [ -n "${GITHUB_REF:-}" ]; then
341+
elif [[ -n "${GITHUB_REF:-}" ]]; then
342342
# shellcheck disable=SC2001
343343
branch_name=$(echo "$GITHUB_REF" | sed "s#refs/heads/##")
344344
fi

scripts/docker/tests/docker.test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function main() {
4444
done
4545
echo "Total: ${#tests[@]}, Passed: $(( ${#tests[@]} - status )), Failed: $status"
4646
test-docker-suite-teardown
47-
[ $status -gt 0 ] && return 1 || return 0
47+
[[ $status -gt 0 ]] && return 1 || return 0
4848
}
4949

5050
# ==============================================================================

scripts/githooks/check-branch-name.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ function check_git_branch_name {
1515

1616
function check_git_branch_name_format {
1717
BUILD_BRANCH="$1"
18-
if [ "$BUILD_BRANCH" != 'main' ] && ! [[ $BUILD_BRANCH =~ (hotfix|task)\/(fdos|dosis|sia|ftrs)-([0-9]{1,5})(_|-)([A-Za-z0-9])([A-Za-z0-9_-]{9,45})$ ]] ; then
18+
if [[ "$BUILD_BRANCH" != 'main' ]] && ! [[ $BUILD_BRANCH =~ (hotfix|task)\/(fdos|dosis|sia|ftrs)-([0-9]{1,5})(_|-)([A-Za-z0-9])([A-Za-z0-9_-]{9,45})$ ]] ; then
1919
echo 1
2020
fi
2121
}
2222

2323
BRANCH_NAME=${BRANCH_NAME:-$(git rev-parse --abbrev-ref HEAD)}
2424
check_git_branch_name "$BRANCH_NAME"
2525

26-
[ $? != 0 ] && exit_code=1 ||:
26+
[[ $? != 0 ]] && exit_code=1 ||:
2727
exit $exit_code

scripts/githooks/check-markdown-format.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function main() {
5151
;;
5252
esac
5353

54-
if [ -n "$files" ]; then
54+
if [[ -n "$files" ]]; then
5555
if command -v markdownlint > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then
5656
files="$files" run-markdownlint-natively
5757
else

scripts/githooks/check-projects-make-pre-commit.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ REPO_ROOT="$(git rev-parse --show-toplevel)"
66

77
# Function to check if a Makefile has a pre-commit target
88
has_pre_commit_target() {
9-
if [ -f "$1/Makefile" ]; then
9+
if [[ -f "$1/Makefile" ]]; then
1010
grep -q "^pre-commit:" "$1/Makefile" && return 0
1111
fi
1212
return 1
@@ -68,9 +68,9 @@ while read -r makefile; do
6868
done < "$TEMP_FILE"
6969

7070
# Check if any directories were processed
71-
if [ $FOUND_DIRECTORIES -eq 0 ]; then
71+
if [[ $FOUND_DIRECTORIES -eq 0 ]]; then
7272
echo "No directories with Makefiles and staged changes found"
73-
elif [ $EXIT_CODE -eq 0 ]; then
73+
elif [[ $EXIT_CODE -eq 0 ]]; then
7474
echo "All pre-commit checks passed"
7575
else
7676
echo "One or more pre-commit checks failed"

scripts/githooks/scan-secrets.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function get-cmd-to-run() {
5757
;;
5858
esac
5959
# Include base line file if it exists
60-
if [ -f "$dir/scripts/config/.gitleaks-baseline.json" ]; then
60+
if [[ -f "$dir/scripts/config/.gitleaks-baseline.json" ]]; then
6161
cmd="$cmd --baseline-path $dir/scripts/config/.gitleaks-baseline.json"
6262
fi
6363
# Include the config file

scripts/shellscript-linter.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function main() {
2121

2222
cd "$(git rev-parse --show-toplevel)"
2323

24-
[ -z "${file:-}" ] && echo "WARNING: 'file' variable not set, defaulting to itself"
24+
[[ -z "${file:-}" ]] && echo "WARNING: 'file' variable not set, defaulting to itself"
2525
local file=${file:-scripts/shellscript-linter.sh}
2626
if command -v shellcheck > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then
2727
file="$file" run-shellcheck-natively

scripts/workflow/app-build-deploy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export_terraform_workspace_name
1515

1616
FULL_SERVICE_NAME="${SERVICE_NAME}"
1717

18-
if [ "${TERRAFORM_WORKSPACE_NAME}" != "default" ]; then
18+
if [[ "${TERRAFORM_WORKSPACE_NAME}" != "default" ]]; then
1919
FULL_SERVICE_NAME="${FULL_SERVICE_NAME}-${TERRAFORM_WORKSPACE_NAME}"
2020
fi
2121

scripts/workflow/bootstrapper.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ if [[ ! "$ACTION" =~ ^(plan|apply|destroy) ]]; then
3434
EXPORTS_SET=1
3535
fi
3636

37-
if [ -z "$AWS_REGION" ] ; then
37+
if [[ -z "$AWS_REGION" ]] ; then
3838
echo Set AWS_REGION to name of the AWS region to host the terraform state bucket
3939
EXPORTS_SET=1
4040
fi
4141

42-
if [ -z "$PROJECT" ] ; then
42+
if [[ -z "$PROJECT" ]] ; then
4343
echo Set PROJECT to identify if account is for dos or cm
4444
EXPORTS_SET=1
4545
else
@@ -49,7 +49,7 @@ else
4949
fi
5050
fi
5151

52-
if [ -z "$ENVIRONMENT" ] ; then
52+
if [[ -z "$ENVIRONMENT" ]] ; then
5353
echo Set ENVIRONMENT to identify if account is for mgmt, dev, test, sandpit, int, ref, non-prod, preprod or prod
5454
EXPORTS_SET=1
5555
else
@@ -59,13 +59,13 @@ else
5959
fi
6060
fi
6161

62-
if [ $EXPORTS_SET = 1 ] ; then
62+
if [[ $EXPORTS_SET = 1 ]] ; then
6363
echo One or more required exports not correctly set
6464
exit 1
6565
fi
6666

6767
ENV_TF_VARS_FILE="$ENVIRONMENT/environment.tfvars"
68-
if ! [ -f "$ENVIRONMENTS_DIR/$ENV_TF_VARS_FILE" ] ; then
68+
if [[ ! -f "$ENVIRONMENTS_DIR/$ENV_TF_VARS_FILE" ]] ; then
6969
echo "No environment variables defined for $ENVIRONMENT environment"
7070
exit 1
7171
fi
@@ -150,22 +150,22 @@ function github_runner_stack {
150150
cd "$STACK_DIR" || exit
151151
# if no stack tfvars create temporary one
152152
TEMP_STACK_TF_VARS_FILE=0
153-
if [ ! -f "$ROOT_DIR/$INFRASTRUCTURE_DIR/$STACK_TF_VARS_FILE" ] ; then
153+
if [[ ! -f "$ROOT_DIR/$INFRASTRUCTURE_DIR/$STACK_TF_VARS_FILE" ]] ; then
154154
touch "$ROOT_DIR/$INFRASTRUCTURE_DIR/$STACK_TF_VARS_FILE"
155155
TEMP_STACK_TF_VARS_FILE=1
156156
fi
157157

158158
# init terraform
159159
terraform-initialise
160160

161-
if [ -n "$ACTION" ] && [ "$ACTION" = 'plan' ] ; then
161+
if [[ -n "$ACTION" && "$ACTION" = 'plan' ]] ; then
162162
terraform plan \
163163
-var-file "$ROOT_DIR"/"$INFRASTRUCTURE_DIR"/$COMMON_TF_VARS_FILE \
164164
-var-file "$ROOT_DIR"/"$INFRASTRUCTURE_DIR"/$STACK_TF_VARS_FILE \
165165
-var-file "$ENVIRONMENTS_DIR/$ENV_TF_VARS_FILE"
166166
fi
167167

168-
if [ -n "$ACTION" ] && [ "$ACTION" = 'apply' ] ; then
168+
if [[ -n "$ACTION" && "$ACTION" = 'apply' ]] ; then
169169
terraform apply -auto-approve \
170170
-var-file "$ROOT_DIR"/"$INFRASTRUCTURE_DIR"/$COMMON_TF_VARS_FILE \
171171
-var-file "$ROOT_DIR"/"$INFRASTRUCTURE_DIR"/$STACK_TF_VARS_FILE \
@@ -209,27 +209,27 @@ fi
209209
cd "$STACK_DIR" || exit
210210
# if no stack tfvars create temporary one
211211
TEMP_STACK_TF_VARS_FILE=0
212-
if [ ! -f "$ROOT_DIR/$INFRASTRUCTURE_DIR/$STACK_TF_VARS_FILE" ] ; then
212+
if [[ ! -f "$ROOT_DIR/$INFRASTRUCTURE_DIR/$STACK_TF_VARS_FILE" ]] ; then
213213
touch "$ROOT_DIR/$INFRASTRUCTURE_DIR/$STACK_TF_VARS_FILE"
214214
TEMP_STACK_TF_VARS_FILE=1
215215
fi
216216

217217
# init terraform
218218
terraform-initialise
219219

220-
if [ -n "$ACTION" ] && [ "$ACTION" = 'plan' ] ; then
220+
if [[ -n "$ACTION" && "$ACTION" = 'plan' ]] ; then
221221
terraform plan \
222222
-var-file "$ROOT_DIR"/"$INFRASTRUCTURE_DIR"/$COMMON_TF_VARS_FILE \
223223
-var-file "$ROOT_DIR"/"$INFRASTRUCTURE_DIR"/$STACK_TF_VARS_FILE \
224224
-var-file "$ENVIRONMENTS_DIR/$ENV_TF_VARS_FILE"
225225
fi
226-
if [ -n "$ACTION" ] && [ "$ACTION" = 'apply' ] ; then
226+
if [[ -n "$ACTION" && "$ACTION" = 'apply' ]] ; then
227227
terraform apply -auto-approve \
228228
-var-file "$ROOT_DIR"/"$INFRASTRUCTURE_DIR"/$COMMON_TF_VARS_FILE \
229229
-var-file "$ROOT_DIR"/"$INFRASTRUCTURE_DIR"/$STACK_TF_VARS_FILE \
230230
-var-file "$ENVIRONMENTS_DIR/$ENV_TF_VARS_FILE"
231231
fi
232-
if [ -n "$ACTION" ] && [ "$ACTION" = 'destroy' ] ; then
232+
if [[ -n "$ACTION" && "$ACTION" = 'destroy' ]] ; then
233233
terraform destroy -auto-approve \
234234
-var-file "$ROOT_DIR"/"$INFRASTRUCTURE_DIR"/$COMMON_TF_VARS_FILE \
235235
-var-file "$ROOT_DIR"/"$INFRASTRUCTURE_DIR"/$STACK_TF_VARS_FILE \

0 commit comments

Comments
 (0)