Skip to content

Commit b92ba4d

Browse files
Fix exit code handling in auto-renew function
The original code had a critical bug where exit codes from certificate renewal failures were not properly captured due to bash subshell issues with pipelines. When using a pipeline with '|', each command runs in a subshell, so changes to variables inside the while loop don't persist to the parent shell. This fix: - Replaces the pipeline approach with a temporary file to avoid subshell issues - Properly captures and reports failures during certificate renewal - Ensures that if any app fails to renew, the function will correctly report failure instead of always reporting success This is particularly important for automated renewal processes where proper error reporting is critical for monitoring and alerting.
1 parent 658fef4 commit b92ba4d

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

command-functions

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,23 @@ cmd-letsencrypt-auto-renew() {
3838
# For all apps, sorted by ascending time left until renewal.
3939
# This way, we'll prioritize apps that need to be renewed soon
4040
# if we should hit a rate limit along the way.
41-
fn-letsencrypt-list-apps-with-expiry \
42-
| sort -nk5 \
43-
| while IFS=$'\t' read -r -a appExpiry; do
44-
45-
if [[ ${appExpiry[4]} -lt 0 ]]; then
46-
dokku_log_info1 "${appExpiry[0]} needs renewal"
47-
dokku letsencrypt:enable "${appExpiry[0]}" || EXIT_CODE=$?
48-
else
49-
days_left=$(fn-letsencrypt-format-timediff "${appExpiry[4]}")
50-
dokku_log_verbose "${appExpiry[0]} still has $days_left days left before renewal"
41+
# Store the list in a temporary file to avoid subshell issues with pipelines
42+
local temp_file=$(mktemp)
43+
fn-letsencrypt-list-apps-with-expiry | sort -nk5 > "$temp_file"
44+
45+
while IFS=$'\t' read -r -a appExpiry; do
46+
if [[ ${appExpiry[4]} -lt 0 ]]; then
47+
dokku_log_info1 "${appExpiry[0]} needs renewal"
48+
if ! dokku letsencrypt:enable "${appExpiry[0]}"; then
49+
EXIT_CODE=1
5150
fi
52-
53-
done
51+
else
52+
days_left=$(fn-letsencrypt-format-timediff "${appExpiry[4]}")
53+
dokku_log_verbose "${appExpiry[0]} still has $days_left days left before renewal"
54+
fi
55+
done < "$temp_file"
56+
57+
rm -f "$temp_file"
5458

5559
dokku_log_info2 "Finished auto-renewal"
5660
if [[ "$EXIT_CODE" != 0 ]]; then

0 commit comments

Comments
 (0)