Skip to content

Commit eb2e9b8

Browse files
committed
Write change detection file AFTER successful scan
1 parent 438007e commit eb2e9b8

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

scripts/detectChangedArtifacts.sh

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,45 @@
22

33
# Detect changed files in the artifacts directory with a text file containing the last hash code of the contents.
44
# The hash value is generated based on all files (their names and properties) within the artifacts directory.
5-
# A change is detected when the current hash and the stored one differ.
5+
# A change is detected when the current hash and the stored differ.
6+
#
7+
# Command line options:
8+
# --readonly Detect changes without creating or updating the change detection file (stateless).
9+
# A second call without this option will be needed for the change detection to work.
10+
# This is helpful to decide if an operation should be done based on changes while waiting for its success to finally save the change state.
611

712
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
813
set -o errexit -o pipefail
914

15+
# Function to display script usage
16+
usage() {
17+
echo "Usage: $0 [--readonly]"
18+
exit 1
19+
}
20+
21+
# Default values
22+
readonlyMode=false
23+
24+
# Parse command line arguments
25+
while [[ $# -gt 0 ]]; do
26+
key="$1"
27+
case $key in
28+
--readonly)
29+
readonlyMode=true
30+
shift
31+
;;
32+
*)
33+
echo "detectChangedArtifacts: Error: Unknown option: ${key}"
34+
usage
35+
;;
36+
esac
37+
shift || true # ignore error when there are no more arguments
38+
done
39+
40+
if ${readonlyMode}; then
41+
echo "detectChangedArtifacts: Readonly mode activated. Change detection file won't be created." >&2
42+
fi
43+
1044
ARTIFACTS_DIRECTORY=${ARTIFACTS_DIRECTORY:-"artifacts"}
1145
ARTIFACTS_CHANGE_DETECTION_HASH_FILE=${ARTIFACTS_CHANGE_DETECTION_HASH_FILE:-"artifactsChangeDetectionHash.txt"} # Name of the file that contains the hash code of the file list for change detection
1246
ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH="./${ARTIFACTS_DIRECTORY}/$ARTIFACTS_CHANGE_DETECTION_HASH_FILE"
@@ -25,8 +59,11 @@ CURRENT_ARTIFACTS_HASH="$( find "./$ARTIFACTS_DIRECTORY" -type f -not -name "${A
2559

2660
# Assume that the files where changed if the file containing the hash of the file list does not exist yet.
2761
if [ ! -f "${ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH}" ] ; then
28-
# Create the file containing the hash of the files list to a new file for the next call
29-
echo "${CURRENT_ARTIFACTS_HASH}" > "${ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH}"
62+
if ! ${readonlyMode}; then
63+
# Create the file containing the hash of the files list to a new file for the next call
64+
echo "${CURRENT_ARTIFACTS_HASH}" > "${ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH}"
65+
echo "detectChangedArtifacts: Change detection file created" >&2
66+
fi
3067
echo 1
3168
exit 0
3269
fi
@@ -36,7 +73,10 @@ fi
3673
if [[ $(< "${ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH}") == "$CURRENT_ARTIFACTS_HASH" ]] ; then
3774
echo 0
3875
else
39-
# Write the updated hash into the file containing the hash of the files list for the next call
40-
echo "$CURRENT_ARTIFACTS_HASH" > "${ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH}"
76+
if ! ${readonlyMode}; then
77+
# Write the updated hash into the file containing the hash of the files list for the next call
78+
echo "$CURRENT_ARTIFACTS_HASH" > "${ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH}"
79+
echo "detectChangedArtifacts: Change detection file updated" >&2
80+
fi
4181
echo 2
42-
fi
82+
fi

scripts/resetAndScanChanged.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ set -o errexit -o pipefail
1414
# CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes.
1515
# This way non-standard tools like readlink aren't needed.
1616
SCRIPTS_DIR=${SCRIPTS_DIR:-$( CDPATH=. cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P )} # Repository directory containing the shell scripts
17-
echo "resetAndScanChanged SCRIPTS_DIR=$SCRIPTS_DIR"
17+
echo "resetAndScanChanged SCRIPTS_DIR=${SCRIPTS_DIR}"
1818

1919
# Scan and analyze Artifacts when they were changed
20-
changeDetectionReturnCode=$( source "$SCRIPTS_DIR/detectChangedArtifacts.sh" )
20+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedArtifacts.sh" --readonly)
2121
if [[ "${changeDetectionReturnCode}" == "0" ]] ; then
2222
echo "resetAndScanChanged: Artifacts unchanged. Scan skipped."
2323
else
2424
echo "resetAndScanChanged: Detected change (${changeDetectionReturnCode}). Resetting database and scanning artifacts."
2525
source "${SCRIPTS_DIR}/resetAndScan.sh"
26+
changeDetectionReturnCode=$( source "${SCRIPTS_DIR}/detectChangedArtifacts.sh")
2627
fi

0 commit comments

Comments
 (0)