-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sh
executable file
·141 lines (116 loc) · 3.49 KB
/
test.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
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0
# Copyright 2024 The Linux Foundation <matthew.watkins@linuxfoundation.org>
# Thin wrapper script to test workflow YAML code directly from a shell
set -eu -o pipefail
DEBUG="false"
export DEBUG
# Check script arguments
if [ $# -ne 1 ]; then
# Provide a Github action/workflow YAML file as argument
echo "Usage: $0 [workflow YAML file]"; exit 1
else
SOURCE_FILE="$1"
if [ ! -f "$SOURCE_FILE" ]; then
echo "Specified file could not be read: $SOURCE_FILE"; exit 1
fi
SETUP_FILE="setup.txt"
if [ -f "$SETUP_FILE" ]; then
echo "Sourcing script actions/variables from: $SETUP_FILE"
# shellcheck disable=SC1090
source "$SETUP_FILE"
else
echo "No file found specifying inputs: $SETUP_FILE"
fi
fi
# Check for required binaries
GIT_CMD=$(which git)
if [ ! -x "$GIT_CMD" ]; then
echo "GIT command was NOT found in PATH"; exit 1
fi
MKTEMP_CMD=$(which mktemp)
if [ ! -x "$MKTEMP_CMD" ]; then
echo "MKTEMP command was NOT found in PATH"; exit 1
fi
# Script debugging options
if [ $DEBUG = "true" ]; then
# set -xv
SHELL_SCRIPT="extracted.sh"
PATH=".:$PATH"
if [ -f "$SHELL_SCRIPT" ]; then
# Remove any previously extracted code on subsequent runs
rm "$SHELL_SCRIPT"
fi
else
SHELL_SCRIPT=$(mktemp -t script-XXXXXXXX.sh)
fi
# Functions
change_dir_error() {
echo "Could not change directory"; exit 1
}
check_for_local_branch() {
BRANCH="$1"
git show-ref --quiet refs/heads/"$BRANCH"
return $?
}
check_for_remote_branch() {
BRANCH="$1"
git ls-remote --exit-code --heads origin "$BRANCH"
return $?
}
cleanup_on_exit() {
if [ -f "$SHELL_SCRIPT" ]; then
echo "Removing temporary shell code"
rm "$SHELL_SCRIPT"
fi
}
# Main script entry point
# Get organisation and repository name
URL=$(git config --get remote.origin.url)
# Take the above and store it converted as ORG_AND_REPO
# e.g. ModeSevenIndustrialSolutions/test-bootstrap
ORG_AND_REPO=${URL/%.git}
ORG_AND_REPO=${ORG_AND_REPO//:/ }
ORG_AND_REPO=$(echo "$ORG_AND_REPO" | awk '{ print $2 }')
# Variable below is currently unused
# HEAD_BRANCH=$("$GIT_CMD" rev-parse --abbrev-ref HEAD)
REPO_DIR=$(git rev-parse --show-toplevel)
# Change to top-level of GIT repository
CURRENT_DIR=$(pwd)
if [ "$REPO_DIR" != "$CURRENT_DIR" ]; then
echo "Changing directory to: $REPO_DIR"
cd "$REPO_DIR" || change_dir_error
fi
# The section below extracts shell code from the YAML file
echo "Attempting to parse shell code from: $SOURCE_FILE"
EXTRACT="false"
while read -r LINE; do
if [[ "$LINE" = *"#SHELLCODEEND"* ]]; then
EXTRACT="complete"
break
elif [[ "$LINE" = *"#SHELLCODESTART"* ]]; then
EXTRACT="true"
touch "$SHELL_SCRIPT"
chmod a+x "$SHELL_SCRIPT"
continue
elif [ "$EXTRACT" = "true" ]; then
echo "$LINE" >> "$SHELL_SCRIPT"
fi
done < "$SOURCE_FILE"
# Only remove temporary files when NOT debugging
if [ "$DEBUG" != "true" ]; then
trap cleanup_on_exit EXIT
fi
if [ -f "$SHELL_SCRIPT" ] && [ "$DEBUG" = "true" ]; then
echo "Extracted code to file: $SHELL_SCRIPT"
fi
if [ "$EXTRACT" = "complete" ]; then
echo "Executing extracted shell script/code..."
# Shell code executed is temporary and cannot be checked by linting
# https://www.shellcheck.net/wiki/SC1090
# shellcheck disable=SC1090
"$SHELL_SCRIPT"
else
echo "Error: start/stop markers not found in file"
exit 1
fi