Skip to content

Clean up stale hook jobs before helm upgrade to fix BackoffLimitExceeded - #836

Merged
ricardobernardino2024 merged 3 commits into
release-17.0.0from
chunk/clean-failed-hook-jobs-before-helm-upgrade
Aug 31, 2026
Merged

Clean up stale hook jobs before helm upgrade to fix BackoffLimitExceeded#836
ricardobernardino2024 merged 3 commits into
release-17.0.0from
chunk/clean-failed-hook-jobs-before-helm-upgrade

Conversation

@circleci-app

@circleci-app circleci-app Bot commented Aug 31, 2026

Copy link
Copy Markdown

Problem

deploy_to_doks_release (job 30795) fails at the Install Codacy step with:

Error: UPGRADE FAILED: release codacy-release failed, and has been rolled back due to atomic being set:
post-upgrade hooks failed: job failed: BackoffLimitExceeded

The root cause: a previous failed deployment (with --atomic) rolls back but leaves post-upgrade hook Jobs (e.g. DB migrations) in the codacy-release namespace. These Jobs have a hook-succeeded delete policy, so they survive the rollback. On the next attempt, Helm finds the pre-existing failed Job with the same name and immediately reports BackoffLimitExceeded without even running the hook body.

Fix

Add a clean_jobs target in .do/Makefile that deletes all Jobs in the namespace before the upgrade:

.PHONY: clean_jobs
clean_jobs:
    -kubectl delete jobs --all -n ${NAMESPACE} --ignore-not-found=true

Wire it into deploy_to_doks_from_chartmuseum between setup_helm_repos and helm_install_from_chart_museum. The - prefix suppresses Make errors if no jobs exist, and --ignore-not-found=true makes kubectl itself non-fatal when the namespace is clean.

This is safe because codacy-release is a dedicated Helm release namespace — any jobs present before an upgrade are stale hook artifacts from prior failed runs.

https://app.circleci.com/agents/gh/codacy/chat/6104887e-b55b-423f-8544-1876b003d7f1

Previous failed deployments leave hook jobs (e.g. DB migrations) behind in
the codacy-release namespace. When helm tries to run the same post-upgrade
hook on the next attempt it finds an already-failed job and immediately
reports BackoffLimitExceeded, causing the deploy_to_doks_release CI job to
fail (see job 30795).

Add a clean_jobs Makefile target that deletes all jobs in the namespace
before the helm upgrade runs. The codacy-release namespace is a dedicated
Helm release namespace, so any lingering jobs are stale hook artifacts from
prior failed runs and are safe to remove.

AI-Generated: true
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

The PR successfully addresses the 'BackoffLimitExceeded' blocker for Helm upgrades by introducing a job cleanup step. While the technical implementation aligns with the stated intent, it introduces a significant operational risk by using kubectl delete jobs --all. This command is destructive and will terminate unrelated workloads if the namespace is shared.

Furthermore, there is a consistency gap between deployment paths: the cleanup logic is integrated into the ChartMuseum workflow but omitted from the local directory deployment flow. While Codacy grades the PR as being up to standards, these logic and safety concerns should be addressed to ensure deployment stability across all environments.

About this PR

  • The deployment logic is inconsistent. If stale jobs cause 'BackoffLimitExceeded' errors, this will still occur when developers or CI systems use the 'deploy_to_doks_from_local_dir' target. Ensure the cleanup is applied to all deployment workflows.

Test suggestions

  • Verify 'clean_jobs' successfully deletes all existing Jobs in a populated namespace.
  • Verify 'clean_jobs' does not fail when no Jobs exist in the namespace (idempotency).
  • Verify 'deploy_to_doks_from_chartmuseum' executes 'clean_jobs' specifically after context setup but before 'helm_install_from_chart_museum'.
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Verify 'clean_jobs' successfully deletes all existing Jobs in a populated namespace.
2. Verify 'clean_jobs' does not fail when no Jobs exist in the namespace (idempotency).
3. Verify 'deploy_to_doks_from_chartmuseum' executes 'clean_jobs' specifically after context setup but before 'helm_install_from_chart_museum'.
Low confidence findings
  • The PR lacks automated verification scripts to ensure the 'kubectl' command behaves correctly within the CI/CD pipeline environment without failing the build.

TIP Improve review quality by adding custom instructions
TIP How was this review? Give us feedback

Comment thread .do/Makefile Outdated
Comment thread .do/Makefile Outdated
ricardobernardino2024 and others added 2 commits August 31, 2026 21:13
Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com>
@ricardobernardino2024
ricardobernardino2024 merged commit 2b42911 into release-17.0.0 Aug 31, 2026
9 checks passed
@ricardobernardino2024
ricardobernardino2024 deleted the chunk/clean-failed-hook-jobs-before-helm-upgrade branch August 31, 2026 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant