Skip to content

Commit

Permalink
feat: no apply on empty plan (#21)
Browse files Browse the repository at this point in the history
* feat(runner): skip storing plan binary and sum if no diff is found

* fix(runner): runner should save lastPlanDate even if there's no change
  • Loading branch information
AlanLonguet authored Dec 23, 2022
1 parent 544259d commit ef767bd
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,20 @@ func (r *Runner) init() error {

func (r *Runner) plan() {
log.Print("Launching terraform plan")
_, err := r.terraform.Plan(context.Background(), tfexec.Out(PlanArtifact))
diff, err := r.terraform.Plan(context.Background(), tfexec.Out(PlanArtifact))
if err != nil {
log.Printf("Terraform plan errored: %s", err)
return
}
log.Printf("Setting last plan date cache at key %s", r.config.Runner.Layer.PlanDate)
err = r.cache.Set(r.config.Runner.Layer.PlanDate, []byte(strconv.FormatInt(time.Now().Unix(), 10)), 3600)
if err != nil {
log.Fatalf("Could not put plan date in cache: %s", err)
}
if !diff {
log.Printf("Terraform plan diff empty, no subsequent apply should be launched")
return
}
plan, err := os.ReadFile(fmt.Sprintf("%s/%s", r.terraform.WorkingDir(), PlanArtifact))
if err != nil {
log.Fatalf("Could not read plan output: %s", err)
Expand All @@ -112,11 +121,6 @@ func (r *Runner) plan() {
if err != nil {
log.Fatalf("Could not put plan checksum in cache: %s", err)
}
log.Printf("Setting last plan date cache at key %s", r.config.Runner.Layer.PlanDate)
err = r.cache.Set(r.config.Runner.Layer.PlanDate, []byte(strconv.FormatInt(time.Now().Unix(), 10)), 3600)
if err != nil {
log.Fatalf("Could not put plan date in cache: %s", err)
}
}

func (r *Runner) apply() {
Expand Down

0 comments on commit ef767bd

Please sign in to comment.