terraform-docs / gh-actions

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

Dependabot Ref Checkout Failing #103

Closed TheQueenIsDead closed 1 year ago

TheQueenIsDead commented 1 year ago

Describe the bug

Terraform docs does not seem to check out the ref for dependabot branches

Error

::debug Following files will be committed
M  kubernetes/README.md
[detached HEAD 6d919fc] terraform-docs: automated action
 1 file changed, 3 insertions(+), 3 deletions(-)
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use

    git push origin HEAD:<name-of-remote-branch>

Since I provided a ref (Per the config below), I would expect it to be checked out :-)

How can we reproduce it?

Create a CI run for a small dependabot version PR that has the following config: Config:

    - name: "Render Docs and push changes back to PR"
      uses: terraform-docs/gh-actions@main
      with:
        working-dir: ${{ inputs.working_directory }}
        output-file: README.md
        output-method: inject
        git-push: "true"
        ref: ${{ github.event.pull_request.head.ref }}

Settings:

repare all required actions
Getting action download info
Download action repository 'terraform-docs/gh-actions@main' (SHA:cfde42f79b15256c71f4b79ae1d6acea0f689952)
Run ./.github/actions/docs
Pull down action image 'quay.io/terraform-docs/gh-actions:1.0.0'
Warning: Unexpected input(s) 'ref', valid inputs are ['entryPoint', 'args', 'config-file', 'working-dir', 'atlantis-file', 'find-dir', 'recursive', 'recursive-path', 'output-format', 'output-method', 'output-file', 'template', 'indention', 'git-push', 'git-push-user-name', 'git-push-user-email', 'git-commit-message', 'git-push-sign-off', 'fail-on-diff']
Run terraform-docs/gh-actions@main
  with:
    working-dir: ./kubernetes
    output-file: README.md
    output-method: inject
    git-push: true
    ref: dependabot/terraform/kubernetes/hashicorp/azurerm-3.43.0
    config-file: disabled
    atlantis-file: disabled
    find-dir: disabled
    recursive: false
    recursive-path: modules
    output-format: markdown table
    template: <!-- BEGIN_TF_DOCS -->
  {{ .Content }}
  <!-- END_TF_DOCS -->
    indention: 2
    git-commit-message: terraform-docs: automated action
    git-push-sign-off: false
    fail-on-diff: false

Environment information

TheQueenIsDead commented 1 year ago

I realised that I included the ref argument on the docs step as opposed to the checkout step, user error, apologies.

I did however have to checkout the head_ref attribute instead of the event head ref, as this would cause the repository to still be checked out in a detached state: https://github.com/terraform-docs/gh-actions#auto-commit-changes

jobs:
  ...
  docs:
    if: ${{ github.event_name == 'pull_request' }}
    runs-on: ubuntu-latest
    steps:
      - name: "Checkout"
        uses: actions/checkout@v3
        with:
          ref: ${{ github.head_ref }}

      - name: "Docs"
        uses: ./.github/actions/docs
        with:
          working_directory: ./storage

Docs is just a composite action to reduce code in my monorepo

name: ""
description: ""
inputs:
  working_directory:
    description: "The working directory of the module to scope the docs to."
    required: false
    default: "."
runs:
  using: "composite"
  steps:
    - name: "Render Docs and push changes back to PR"
      uses: terraform-docs/gh-actions@main
      with:
        working-dir: ${{ inputs.working_directory }}
        output-file: README.md
        output-method: inject
        git-push: "true"

This works well for me, but a user could opt to checkout the PR ref, then run git checkout xxx in the next step.