action-setup icon indicating copy to clipboard operation
action-setup copied to clipboard

Review dog command is failing with .git not found error

Open shrutijodevops opened this issue 2 years ago • 2 comments

Hi,

I am trying to use review dog to add a comment/annotation on the PR for any tflint warnings/errors. I have a monorepo and once any PR is made, I am identifying the changed directories in PR and then perform tflint and review dog commands on those changed directories. I am using this action to install review dog and then add tflint warnings as annotations. However, when running the reviewdog command, I get.reviewdog git not founderror

name: tflint
on:
  pull_request:
    paths:
      - 'prod/**'
      - 'non-prod/**'
    branches: [ main ]

jobs:
  changed-files:   ##this is to get changed directories in the git repo via the PR
    name: Changed files
    runs-on: arc-runner-set
    outputs:
      matrix: ${{ steps.changed-files.outputs.all_changed_files }}
    steps:
    
      - name: Install Git
        run: |
          sudo apt update
          sudo apt install git -y

      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Get changed files
        id: changed-files
        uses: tj-actions/changed-files@v37
        with:
          dir_names: true
          dir_names_exclude_current_dir: true
          files_ignore: .github/*
          json: true
          quotepath: false
          escape_json: false
          files_yaml: |
            prod:
              - prod/prod/**
            non-prod:
              - non-prod/qa/**
              - non-prod/dev/**

      - name: List changed files
        run: |
          echo "Changed files: ${{ steps.changed-files.outputs.all_changed_and_modified_files }}"
          echo "Changed directories: ${{ steps.changed-files.outputs.all_changed_files }}"
    
  tflint:
    runs-on: arc-runner-set
    needs: changed-files
    strategy:
      fail-fast: false
      matrix:
        files:  ${{ fromJSON(needs.changed-files.outputs.matrix) }}
    defaults:
      run:
        working-directory: ${{ matrix.files }}
    steps:
    # - name: Print the current directory
    #   run: echo ${{ matrix.files }}
    - uses: actions/checkout@v3
      name: Checkout source code
    
    - uses: actions/cache@v3
      name: Cache plugin dir
      with:
        path: ~/.tflint.d/plugins
        key: tflint-${{ hashFiles('.tflint.hcl') }}

    - name: Install unzip
      run: |
        sudo apt update
        sudo apt install unzip
        sudo apt install curl -y
    
    - uses: terraform-linters/setup-tflint@v3
      name: Setup TFLint
      with:
        tflint_version: v0.49.0

    - name: Show version
      run: tflint --version

    - uses: reviewdog/action-setup@v1     #This works fine
      name: Install review dog
      with:
        reviewdog_version: latest 

    - name: Run TFLint
      id: tflint
      run: |
        tflint --format=json > tflint.json || true
        cat tflint.json
      continue-on-error: true

    - name: "Give detekt permissions"
      run: |
        sudo chown -R root:root $GITHUB_WORKSPACE

    - name: Post TFLint comments on PR
      env:
        REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        cat tflint.json | reviewdog -efm="%f:%l:%c: %m" -name="tflint" -reporter=github-pr-check -level=warning
   
      

I am not sure what I am doing wrong here. Can you please suggest something?

Here is the logs:

Run cat tflint.json | reviewdog -efm="%f:%l:%c: %m" -name="tflint" -reporter=github-pr-check -level=warning
  cat tflint.json | reviewdog -efm="%f:%l:%c: %m" -name="tflint" -reporter=github-pr-check -level=warning
  shell: /usr/bin/bash -e {0}
  env:
    REVIEWDOG_GITHUB_API_TOKEN: ***
reviewdog: .git not found
Error: Process completed with exit code 1.

shrutijodevops avatar Dec 15 '23 07:12 shrutijodevops

This issue is almost two years old but I ran into the same thing today and was able to resolve it. Hope the below summary helps anybody else who runs into it.

reviewdog: .git not found occurs because reviewdog is looking for the .git folder when it goes to calculate how PR comments should be added. i.e. it needs access to the underlying repo history contained in .git. If you're not familiar, every git repo has a .git directory inside of it where the underlying history is stored.

In my case, this was happening because I attempted to run reviewdog on a CI/CD agent that hadn't checked out the repo by the time reviewdog ran.

mikeford avatar Apr 02 '25 19:04 mikeford

Yep. The actionable error message is to propose to user to check if the repo is cloned before the action.

abitrolly avatar Apr 03 '25 06:04 abitrolly