Skip to content

Commit 658c71d

Browse files
authored
DIGGER_PLANFILE in apply runs (#2087)
* DIGGER_PLANFILE in apply runs
1 parent b8b9365 commit 658c71d

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

docs/ce/howto/custom-commands.mdx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ You can add extra arguments to the plan command by setting the `extra_args` key
4545
However in some cases if you wish to override the plan command entirely you can do it by excluding the plan in the steps and having your command specified in the run like so:
4646

4747
```
48-
4948
workflows:
5049
default:
5150
plan:
@@ -55,4 +54,21 @@ workflows:
5554
- run: terraform plan -input=false -refresh -no-color -out $DIGGER_PLANFILE
5655
```
5756

58-
Note that you need to use the -out flag to write the output to the $DIGGER_PLANFILE env variable, since this will be used in postprocessing steps by digger.
57+
Note that you need to use the -out flag to write the output to the $DIGGER_PLANFILE env variable, since this will be used in postprocessing steps by digger.
58+
59+
Similarly for the apply step you can use the $DIGGER_PLANFILE env variable to point to the plan file to apply. Note that this will only work when you have the [plan persistence](/ce/howto/store-plans-in-a-bucket)
60+
configured. If plan persistence is not configured, the $DIGGER_PLANFILE environment variable will not be set during the apply step. Here is an example with both plan and apply commands overriden:
61+
62+
```
63+
workflows:
64+
default:
65+
plan:
66+
steps:
67+
- init
68+
- run: terraform plan -input=false -refresh -no-color -out $DIGGER_PLANFILE
69+
apply:
70+
steps:
71+
- init
72+
- run: terraform apply -input=false -no-color $DIGGER_PLANFILE
73+
74+
```

libs/execution/execution.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin
224224
return step.Action == "plan"
225225
})
226226

227+
// setting additional env vars for run step
228+
if d.RunEnvVars == nil {
229+
d.RunEnvVars = make(map[string]string)
230+
}
231+
227232
for _, step := range planSteps {
228233
slog.Info("Running step", "action", step.Action)
229234
if step.Action == "init" {
@@ -266,10 +271,7 @@ func (d DiggerExecutor) Plan() (*iac_utils.IacSummary, bool, bool, string, strin
266271
slog.Info("Running command",
267272
"command", step.Value,
268273
"project", d.ProjectNamespace+"#"+d.ProjectName)
269-
// setting additional env vars for run step
270-
if d.RunEnvVars == nil {
271-
d.RunEnvVars = make(map[string]string)
272-
}
274+
273275
slog.Debug("adding plan file path to environment", "DIGGER_PLANFILE", d.PlanPathProvider.LocalPlanFilePath())
274276
d.RunEnvVars["DIGGER_PLANFILE"] = d.PlanPathProvider.LocalPlanFilePath()
275277
_, _, err := d.CommandRunner.Run(d.ProjectPath, step.Shell, commands, d.RunEnvVars)
@@ -382,6 +384,11 @@ func (d DiggerExecutor) Apply() (*iac_utils.IacSummary, bool, string, error) {
382384
}
383385
}
384386

387+
if d.RunEnvVars == nil {
388+
slog.Debug("RunEnvVars is nil, creating new map")
389+
d.RunEnvVars = make(map[string]string)
390+
}
391+
385392
for _, step := range applySteps {
386393
if step.Action == "init" {
387394
stdout, stderr, err := d.TerraformExecutor.Init(step.ExtraArgs, d.StateEnvVars)
@@ -411,6 +418,11 @@ func (d DiggerExecutor) Apply() (*iac_utils.IacSummary, bool, string, error) {
411418
if os.Getenv("ACTIVATE_VENV") == "true" {
412419
commands = append(commands, fmt.Sprintf("source %v/.venv/bin/activate", os.Getenv("GITHUB_WORKSPACE")))
413420
}
421+
422+
if plansFilename != nil {
423+
slog.Debug("adding plan file path to environment", "DIGGER_PLANFILE", *plansFilename)
424+
d.RunEnvVars["DIGGER_PLANFILE"] = *plansFilename
425+
}
414426
commands = append(commands, step.Value)
415427
slog.Info("Running command",
416428
"command", step.Value,

0 commit comments

Comments
 (0)