terraform-docs / gh-actions

A Github action for generating Terraform module documentation using terraform-docs and gomplate
Apache License 2.0
143 stars 59 forks source link

Error: Uncommitted change(s) has been found! #124

Open arivictor opened 5 months ago

arivictor commented 5 months ago

Describe the bug

I am trying to do a fail-on-diff check for a terraform module doc on a pull request. The GHA fails with

 * [new tag]         1.0.0      -> 1.0.0
 * [new tag]         1.1.0      -> 1.1.0
::debug working_dir=.
::debug config_file=.terraform-docs.yml
::debug output_mode=replace
::debug output_file=README.md
::debug terraform-docs markdown table --config .terraform-docs.yml --output-mode replace --output-file README.md --output-template <!-- BEGIN_TF_DOCS -->
{{ .Content }}
<!-- END_TF_DOCS --> .
README.md updated successfully
::debug No change in ./ detected

Error: Uncommitted change(s) has been found!

There is no change between my current README.md and the generated one (well, there shouldn't be in theory).

How can we reproduce it?

workflow

name: 'terraform docs'

on:
  pull_request:

permissions:
  contents: read

jobs:
  docs:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        ref: ${{ github.event.pull_request.head.ref }}

    - name: Generate TF Docs
      uses: terraform-docs/gh-actions@v1.0.0
      with:
        config-file: .terraform-docs.yml
        working-dir: .
        output-file: README.md
        output-method: replace
        fail-on-diff: true

config

formatter: markdown table
header-from: main.tf
footer-from: ""
sections:
  show:
    - all
output:
  file: README.md
  mode: replace
  template: |-
    <!-- BEGIN_TF_DOCS -->

    {{ .Content }}

    <!-- END_TF_DOCS -->

sort:
  enabled: true
  by: required

settings:
  anchor: true
  indent: 2
  escape: false
  default: true
  required: true
  type: true

Environment information

arivictor commented 5 months ago

I've also attempted to manually install terraform docs: workflow

jobs:
  docs:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: ${{ github.event.pull_request.head.sha }}

      - name: Check README.md is up to date
        run: |
            curl -Lo ./terraform-docs.tar.gz https://github.com/terraform-docs/terraform-docs/releases/download/v0.17.0/terraform-docs-v0.17.0-linux-amd64.tar.gz
            tar -xzf terraform-docs.tar.gz
            chmod +x terraform-docs
            ./terraform-docs . --output-check

output

Run curl -Lo ./terraform-docs.tar.gz https://github.com/terraform-docs/terraform-docs/releases/download/v0.17.0/terraform-docs-v0.17.0-linux-amd64.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

100 6014k  100 6014k    0     0  19.7M      0 --:--:-- --:--:-- --:--:-- 19.7M
Error: README.md is out of date
Error: Process completed with exit code 1.

But when I run it locally

» terraform-docs . --output-check
README.md is up to date

I'm not sure what the remote is picking up that's causing a difference...

arivictor commented 5 months ago

I saved the results the remote is giving me and then did a diff with my local copy:

diff README.md README2.md
< | <a name="provider_google"></a> [google](#provider_google) | 4.84.0 |
< | <a name="provider_google-beta"></a> [google-beta](#provider_google-beta) | 4.84.0 |
---
> | <a name="provider_google"></a> [google](#provider_google) | < 5.0 |
> | <a name="provider_google-beta"></a> [google-beta](#provider_google-beta) | < 5.0 |

🤔 why

arivictor commented 5 months ago

AHA!

if I run terraform-docs without initialising terraform terraform init it takes the exact provider version from my terraform. If I initialise first, it uses whatever it downloads which happened to be 4.84.0.

So locally I had initialised, but remotely its just running terraform-docs as is.

lukemundy-vgw commented 4 weeks ago

Was running into the same issue. Ran terraform-docs locally but the github actions was failing with the same Error: Uncommitted change(s) has been found! error.

Because I hadn't committed .terraform-lock.hcl, it wasn't present in GHA and so it uses provider version constraints from the required_providers block, compared to locally where it would use the exact version from .terraform.lock.hcl.

Running rm -r .terraform/ .terraform.lock.hcl locally and then re-running terraform-docs fixed the issue and allowed the README generated locally match what gets generated in GHA.

Thanks @arivictor for the fix 🫡