gruntwork-io / terragrunt-action

A GitHub Action for installing and running Terragrunt
Apache License 2.0
99 stars 38 forks source link

Outputting plan file to specific path fails #55

Open Mornor opened 5 months ago

Mornor commented 5 months ago

Describe the bug The following commands fails to redirect the plan file

tg_command: plan -out {{ github.workspace }}/terragrunt.tfplan

To Reproduce

name: Deploy customer changes
on: push

env:
  TF_VERSION: 1.5.1
  TG_VERSION: 0.55.21

jobs:
  terragrunt-plan:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Plan
        uses: gruntwork-io/terragrunt-action@v2
        with:
          tf_version: ${{ env.TF_VERSION }}
          tg_version: ${{ env.TG_VERSION }}
          tg_dir: ${{ env.WORKING_DIR }}
          tg_command: plan -out {{ github.workspace }}/terragrunt.tfplan

Expected behavior

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.

Actual behaviour

│ Error: Failed to write plan file
│ 
│ The plan file could not be written: open
│ /home/runner/work/<path>/terragrunt.tfplan:
│ no such file or directory.
╵

Other notes The following command works

tg_command: plan -out $(pwd)terragrunt.tfplan

But I would expect one of these commands to work instead

tg_command: plan -out {{ github.workspace }}/terragrunt.tfplan
tg_command: plan -out "{{ github.workspace }}/terragrunt.tfplan"
tg_command: plan -out $(pwd)/terragrunt.tfplan

Nice to have

Versions

Related Might be related to #40

levkohimins commented 5 months ago

Hi @Mornor, the issue is the forward slash, right?

Mornor commented 5 months ago

Yes, it should work with a forward slash (so a full path) like so

tg_command: plan -out {{ github.workspace }}/terragrunt.tfplan
tg_command: plan -out "{{ github.workspace }}/terragrunt.tfplan"
tg_command: plan -out $(pwd)/terragrunt.tfplan

But it doesn't (Error: Failed to write plan file ...).

However, this works (without forward slash)

tg_command: plan -out $(pwd)terragrunt.tfplan

IMO, we should be able to give a full path for the plan file as output.

levkohimins commented 5 months ago

@Mornor, -out is a terraform flag, Terragrunt simply passes this flag to Terraform when running the terraform plan command. You should create this issue in Terraform issues.

Mornor commented 5 months ago

The corresponding command (terraform plan -out terraform.tfplan) works fine. Locally issuing following command works as well, so this make me think the issue is related to the Terragrunt GH Action itself

$> rm -rf $(pwd)/terragrunt.tfplan
$> terragrunt plan -out $(pwd)/terragrunt.tfplan
No changes. Your infrastructure matches the configuration.

$> terragrunt show $(pwd)/terragrunt.tfplan
No changes. Your infrastructure matches the configuration.

However, it seems the terragrunt command itself doesn't work as expected. Locally, this does not produce any plan.

$> terragrunt plan -out terragrunt.tfplan

But this does

$> terragrunt plan -out $(pwd)/terragrunt.tfplan

However, not with Github Action (Error: Failed to write plan file [...] terragrunt.tfplan: no such file or │ directory

This might be related to a core Terragrunt issue.

denis256 commented 5 months ago

Hi, AFAIK, *workspace variables in case of runners point to the wrong locations, use /github/workspace path to save/load plan files In my tests:

github.workspace = /home/runner/work/terragrunt-tests/terragrunt-tests
GITHUB_WORKSPACE = /home/runner/work/terragrunt-tests/terragrunt-tests
runner.workspace = /home/runner/work/terragrunt-tests
RUNNER_WORKSPACE = /home/runner/work/terragrunt-tests

when during execution, working directory is mounted in /github/workspace

https://github.com/actions/runner/issues/2058

Mornor commented 5 months ago

Hi @denis256,

Big up to you, as I managed to make it work with a combination of your solution and some mv commands:

    - name: Plan
      uses: gruntwork-io/terragrunt-action@v2
      with:
        tf_version: ${{ inputs.tf_version }}
        tg_version: ${{ inputs.tg_version }}
        tg_dir: ${{ inputs.customer_dir }}/${{ inputs.customer }}/${{ inputs.dir_to_deploy }}
        tg_command: plan -out /github/workspace/terragrunt.tfplan

     - name: Move plan file
        working-directory: ${{ github.workspace }}
        run: mv terragrunt.tfplan ${{ inputs.customer_dir }}/${{ inputs.customer }}/${{ inputs.dir_to_deploy }}/terragrunt.tfplan   

I don't really like or understand the combination of {{ github.workspace }} and /github/workspace/ (and I still think there's something not working properly under the hood), but at least it works!