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)
813set -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+
1044ARTIFACTS_DIRECTORY=${ARTIFACTS_DIRECTORY:- " artifacts" }
1145ARTIFACTS_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
1246ARTIFACTS_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.
2761if [ ! -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
3269fi
3673if [[ $( < " ${ARTIFACTS_CHANGE_DETECTION_HASH_FILE_PATH} " ) == " $CURRENT_ARTIFACTS_HASH " ]] ; then
3774 echo 0
3875else
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
0 commit comments