hashicorp / terraform-github-actions

Terraform GitHub Actions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Run terraform apply with terraform plan output file

alexbde opened this issue · comments

I try to run terraform apply using output file from terraform plan. How could this be done?

Prerequisite: terraform plan -out example.tfplan.

Running

  - uses: hashicorp/terraform-github-actions@master
    env:
      TF_CLI_ARGS_apply: example.tfplan
      TF_LOG: TRACE
    with:
      tf_actions_version: ${{ env.TF_VERSION }}
      tf_actions_subcommand: 'apply'

Causes error Too many command line arguments. Configuration path expected.

In TRACE log I can see cause for this error is

[INFO] TF_CLI_ARGS_apply value: "example.tfplan"
[INFO] CLI command args: []string{"apply", "example.tfplan", "-auto-approve", "-input=false"}

example.tfplan must be last argument but TF_CLI_ARGS_apply sets it first (as stated in the docs).

How can I achieve running terraform apply with input file example.tfplan via GitHub Actions?

Hello,

What's the use case here? This won't work without some refactoring on how TF_CLI_ARGS handled.

Off the top of my head I can think of two ways to allow this behavior. One way is to write a conditional that removes command line options when TF_CLI_ARGS is present. The other way is to use GitHub Actions args to just pass the raw arguments down to the Terraform command.

The other way is to use GitHub Actions args to just pass the raw arguments down to the Terraform command

This is the way it worked before v0.5, right? Both solutions look good to me, whatever is easier for you. It's just important, that it works ;)

Use case is what is described in your own guide 😄

  1. Initialize the Terraform working directory.
  2. Produce a plan for changing resources to match the current configuration.
  3. Have a human operator review that plan, to ensure it is acceptable.
  4. Apply the changes described by the plan.

Yep before v0.5.0 each subcommand was a separate GitHub Action. The $* variable was using to pass arguments to the command. I'll see what I can do about adding that behavior back given the refactor.

Perfect, thank you. Let me know if I can help you with testing etc.

You can test against the enhancement/args branch. I made some changes that should allow this to work.

      - name: 'Terraform Plan'
        uses: hashicorp/terraform-github-actions@enhancement/args
        with:
          tf_actions_version: 0.12.13
          tf_actions_subcommand: 'plan'
          args: '-out=plan.tfplan'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: 'Terraform Apply'
        uses: hashicorp/terraform-github-actions@enhancement/args
        with:
          tf_actions_version: 0.12.13
          tf_actions_subcommand: 'apply'
          args: 'plan.tfplan'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Closed in #107 which was released as v0.6.0.

Thank you very much, looks great! I love the enhanced docs!

Sorry to bother you again with #109 🙈