Skip to content

Commit 887dfde

Browse files
fix: Lock file script changes to fail fast (#774)
* Lock file script changes to fail fast * Single quoted dir names in lock file script's print statements * Updated delimiter for sed in lock file script to support / in dir path * Fix to lock file automation workflow - actionlint-docker related involving shellcheck --------- Co-authored-by: Ritwik G <100672805+ritwik-g@users.noreply.github.com>
1 parent 5c4f9bc commit 887dfde

2 files changed

Lines changed: 49 additions & 18 deletions

File tree

.github/workflows/pdm-lock-automation.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
IFS=',' read -r -a dir_array <<< "$dirs"
5858
5959
# Print directories being processed
60-
echo "Processing specified directories: ${dir_array[@]}"
60+
echo "Processing specified directories: ${dir_array[*]}"
6161
6262
# Pass directories as command-line arguments to the script
6363
./scripts/pdm-lock.sh "${dir_array[@]}"

scripts/pdm-lock.sh

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,71 @@
11
#!/bin/bash
2+
set -o pipefail
23

34
# Function to update the lockfile in a directory
45
update_lockfile() {
56
dir="$1"
67
file_path="$dir/pyproject.toml"
78

89
if [[ ! -f "$file_path" ]]; then
9-
echo "[$dir] No pyproject.toml found in $dir"
10-
return
10+
echo "[$dir] No pyproject.toml found in '$dir'"
11+
return 0
1112
fi
1213

13-
echo "[$dir] Checking $file_path for changes against origin/main..."
14+
echo "[$dir] Checking '$file_path' for changes against origin/main..."
1415
if ! git diff --quiet origin/main -- "$file_path"; then
15-
echo "[$dir] Changes detected in $file_path, updating pdm.lock for $dir ..."
16+
echo "[$dir] Changes detected in '$file_path', updating pdm.lock for '$dir' ..."
1617

1718
# Move to the directory if it's not root
1819
if [[ "$dir" != "." ]]; then
19-
cd "$dir"
20+
cd "$dir" || return 1
2021
fi
2122

2223
# Set up virtual environment if not exists
2324
if [[ ! -d ".venv" ]]; then
24-
echo "[$dir] Creating virtual environment in directory: $dir"
25-
pdm venv create -w virtualenv --with-pip
25+
echo "[$dir] Creating virtual environment in directory: '$dir'"
26+
pdm venv create -w virtualenv --with-pip 2>&1 | sed "s|^|[$dir] |" || return 1
2627
else
2728
echo "[$dir] Virtual environment already exists in $dir"
2829
fi
2930

3031
# Activate virtual environment
31-
source .venv/bin/activate
32+
source .venv/bin/activate 2>&1 | sed "s|^|[$dir] |" || return 1
3233

3334
# HACK: https://github.com/pdm-project/pdm/issues/3199
3435
# Replace with checking the exit code directly once above issue is fixed
3536
lock_output=$(pdm lock --check 2>&1)
3637
if echo "$lock_output" | grep -q "WARNING: Lockfile is generated on an older version of PDM"; then
37-
echo "[$dir] Updating pdm.lock in $dir due to outdated version..."
38-
pdm lock -G :all -v 2>&1 | sed "s/^/[$dir] /"
38+
echo "[$dir] Updating pdm.lock in '$dir' due to outdated version..."
39+
pdm lock -G :all -v 2>&1 | sed "s|^|[$dir] |" || return 1
3940
elif [[ $? -ne 0 ]]; then
40-
echo "[$dir] Updating pdm.lock in $dir due to detected changes..."
41-
pdm lock -G :all -v 2>&1 | sed "s/^/[$dir] /"
41+
echo "[$dir] Updating pdm.lock in '$dir' due to detected changes..."
42+
pdm lock -G :all -v 2>&1 | sed "s|^|[$dir] |" || return 1
4243
else
43-
echo "[$dir] No changes required for pdm.lock in $dir."
44+
echo "[$dir] No changes required for pdm.lock in '$dir'."
4445
fi
4546

4647
# Go back to root if moved to a subdirectory
4748
if [[ "$dir" != "." ]]; then
48-
cd -
49+
cd - || return 1
4950
fi
5051
else
51-
echo "[$dir] No changes detected in $file_path"
52+
echo "[$dir] No changes detected in '$file_path'"
5253
fi
5354
}
5455

56+
# https://unix.stackexchange.com/a/124148
57+
# Used to list child processes to kill them in case of an error
58+
list_descendants ()
59+
{
60+
local children=$(ps -o pid= --ppid "$1")
61+
62+
for pid in $children
63+
do
64+
list_descendants "$pid"
65+
done
66+
67+
echo "$children"
68+
}
5569

5670
# Default directories list
5771
directories=(
@@ -75,11 +89,28 @@ fi
7589
# To compare against main branch
7690
git fetch origin main
7791

92+
# Array to store the job PIDs and directories
93+
pids=()
94+
dirs=()
7895

7996
# Run lockfile updates in parallel
8097
for dir in "${directories[@]}"; do
8198
update_lockfile "$dir" &
99+
pid=$!
100+
pids+=($pid) # Add the PID of the background job to the array
101+
dirs+=("$dir") # Add the corresponding directory to the array
82102
done
83103

84-
# Wait for all background processes to complete
85-
wait
104+
# Wait for each background process to complete, exit on the first failure
105+
for i in "${!pids[@]}"; do
106+
pid=${pids[$i]}
107+
dir=${dirs[$i]}
108+
echo "[$dir] Waiting for child process with PID: $pid..."
109+
110+
# # Wait for the specific process to finish
111+
if ! wait "$pid"; then
112+
echo "[$dir] Lock file generation failed. Killing other sub-processes..."
113+
kill $(list_descendants $$) 2>/dev/null || true
114+
exit 1
115+
fi
116+
done

0 commit comments

Comments
 (0)