diff --git a/docs/index.md b/docs/index.md index 8b799bb1..a71a22d1 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1240,7 +1240,7 @@ Hooks associated to `presync` events are triggered before each release is instal This is the ideal event to execute any commands that may mutate the cluster state as it will not be run for read-only operations like `lint`, `diff` or `template`. `preapply` hooks are triggered before a release is uninstalled, installed, or upgraded as part of `helmfile apply`. -This is the ideal event to hook into when you are going to use `helmfile apply` for every kind of change, and you want the hook to be called only when any kind of change is being made. +This is the ideal event to hook into when you are going to use `helmfile apply` for every kind of change and you want the hook to be triggered regardless of whether the releases have changed or not. Be sure to make each `preapply` hook command idempotent. Otherwise, rerunning helmfile-apply on a transient failure may end up either breaking your cluster, or the hook that runs for the second time will never succeed. `preuninstall` hooks are triggered immediately before a release is uninstalled as part of `helmfile apply`, `helmfile sync`, `helmfile delete`, and `helmfile destroy`. diff --git a/pkg/app/app.go b/pkg/app/app.go index 03d78c43..de06601c 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -1354,30 +1354,20 @@ func (a *App) apply(r *Run, c ApplyConfigProvider) (bool, bool, []error) { } } - for id := range releasesWithNoChange { - r := releasesWithNoChange[id] - if _, err := st.TriggerCleanupEvent(&r, "apply"); err != nil { - a.Logger.Warnf("warn: %v\n", err) - } - } - - if releasesToBeDeleted == nil && releasesToBeUpdated == nil { - if infoMsg != nil { - logger := c.Logger() - logger.Infof("") - logger.Infof(*infoMsg) - } - return true, false, nil + infoMsgStr := "" + if infoMsg != nil { + infoMsgStr = *infoMsg } confMsg := fmt.Sprintf(`%s Do you really want to apply? Helmfile will apply all your changes, as shown above. -`, *infoMsg) +`, infoMsgStr) + interactive := c.Interactive() - if !interactive { - a.Logger.Debug(*infoMsg) + if !interactive && infoMsgStr != "" { + a.Logger.Debug(infoMsgStr) } var applyErrs []error @@ -1457,6 +1447,17 @@ Do you really want to apply? } affectedReleases.DisplayAffectedReleases(c.Logger()) + + for id := range releasesWithNoChange { + r := releasesWithNoChange[id] + if _, err := st.TriggerCleanupEvent(&r, "apply"); err != nil { + a.Logger.Warnf("warn: %v\n", err) + } + } + if releasesToBeDeleted == nil && releasesToBeUpdated == nil { + return true, false, nil + } + return true, true, applyErrs } diff --git a/pkg/app/testdata/testapply/noop/log b/pkg/app/testdata/testapply/noop/log index 611fa46e..607657f3 100644 --- a/pkg/app/testdata/testapply/noop/log +++ b/pkg/app/testdata/testapply/noop/log @@ -34,4 +34,11 @@ second-pass rendering result of "helmfile.yaml.part.0": merged environment: &{default map[] map[]} 2 release(s) found in helmfile.yaml +invoking preapply hooks for 2 groups of releases in this order: +GROUP RELEASES +1 default//foo +2 default//bar + +invoking preapply hooks for releases in group 1/2: default//foo +invoking preapply hooks for releases in group 2/2: default//bar changing working directory back to "/path/to" diff --git a/pkg/app/testdata/testapply_hooks/hooks_for_no-diff_release/log b/pkg/app/testdata/testapply_hooks/hooks_for_no-diff_release/log index 1643d0d3..b2f0bdff 100644 --- a/pkg/app/testdata/testapply_hooks/hooks_for_no-diff_release/log +++ b/pkg/app/testdata/testapply_hooks/hooks_for_no-diff_release/log @@ -2,5 +2,8 @@ hook[prepare] logs | foo hook[prepare] logs | +hook[preapply] logs | foo +hook[preapply] logs | + hook[cleanup] logs | foo hook[cleanup] logs |