title: Running pre-commit in GitHub workflow on pull request
body: <p>I am currently using Azure DevOps and am migrating to GitHub. We have one pipeline that runs when a pull request in made active as it first caches the files, compares the main branch to the feature branch, and runs pre-commit on those changed files.</p>
  <p>The ADO pipeline looks like:</p>
  <pre class="lang-yaml prettyprint-override"><code>---
  # Pipeline to run pre-commit hooks
  
  trigger: none
  
  pool:
    vmImage: ubuntu-latest
  
  variables:
    PRE_COMMIT_HOME: $(Pipeline.Workspace)/pre-commit-cache
  
  steps:
    - checkout: self
      persistCredentials: true
  
    - bash: echo &quot;##vso[task.setvariable variable=PY]`python -V`&quot;
      displayName: Get python version
    - task: CacheBeta@0
      inputs:
        key: pre-commit | .pre-commit-config.yaml | &quot;$(PY)&quot;
        path: $(PRE_COMMIT_HOME)
  
    - bash: |
        git remote set-head origin --auto
        [[ -n &quot;${SYSTEM_PULLREQUEST_TARGETBRANCH}&quot; ]] &amp;&amp; COMPARE_BASE=&quot;origin/${SYSTEM_PULLREQUEST_TARGETBRANCH#refs/heads/}&quot;
        CHANGED_FILES=`git diff-tree --diff-filter=ACMRT --name-only -r ${COMPARE_BASE:-origin/HEAD}..${BUILD_SOURCEVERSION}`
        if [[ -z &quot;${CHANGED_FILES}&quot; ]]; then
          echo '##vso[task.logissue type=error]Can not detect changed files'
          echo '##vso[task.complete result=Failed;]'
        else
          for file in ${CHANGED_FILES}; do
            echo &quot;##[debug]${file}&quot;
          done
          sudo ln -s /usr/bin/pwsh /usr/bin/powershell
          pip install --quiet pre-commit
          pre-commit run --files ${CHANGED_FILES} || echo &quot;Exit status is $?&quot;
        fi
      env:
        AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
      displayName: 'Run pre-commit'
  
    - pwsh: |
        # Construct the REST URL to obtain Build ID
        $uri = &quot;$(System.TeamFoundationCollectionUri)/$(System.TeamProjectId)/_apis/build/builds/$(Build.BuildId)/logs/8?api-version=6.0&quot;
  
        # Invoke the REST call and capture the results
        Write-Output &quot;Getting the logs from the following URI: $uri&quot;
        $log= Invoke-RestMethod -Uri $uri -Method Get -ContentType &quot;application/json&quot; -Headers @{Authorization = &quot;Bearer $Env:SYSTEM_ACCESSTOKEN&quot;}
        $logs
  
        if($log -match &quot;exit code: 1&quot;)
        {
          Write-Host &quot;##vso[task.setvariable variable=testsStatus]failed&quot;
          # Option 1: fail the pipeline
          Write-Error &quot;The tests not passed! Please check Run pre-commit step for details&quot;
        } elseif ($log -match &quot;exit code: 3&quot;) {
          Write-Host &quot;##vso[task.setvariable variable=testsStatus]succeeded&quot;
          Write-Output &quot;Succeeded with exit code 3 - baseline file update&quot;
        } else {
          Write-Output &quot;No error codes found in pre-commit. Tests passed successfully&quot;
        }
      env:
        SYSTEM_ACCESSTOKEN: $(System.AccessToken)
      displayName: 'Check pre-commit status'
  </code></pre>
  <p>I am new to using GitHub actions and so far I have:</p>
  <pre class="lang-yaml prettyprint-override"><code>name: Pre-commit
  
  on:
    pull_request:
      branches: [ &quot;main&quot; ]
    workflow_dispatch:
  
  jobs:
    build:
      runs-on: ubuntu-latest
  
      steps:
        - uses: actions/checkout@v3
          with:
            fetch-depth: 0
  
        - name: set PY
          run: echo &quot;PY=$(python -VV | sha256sum | cut -d' ' -f1)&quot; &gt;&gt; $GITHUB_ENV
        - uses: actions/cache@v1
          with:
            path: ~/.cache/pre-commit
            key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
            
        - name: Get changed files
          id: changes
          run: |
            echo &quot;::set-output name=all::$(git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | xargs)&quot;
            
    lint:
      runs-on: ubuntu-latest
      needs: build
      steps:
        - name: echo changed files
          shell: bash
          run:
            git remote set-head origin --auto
            sudo ln -s /usr/bin/pwsh /usr/bin/powershell
            pip install --quiet pre-commit
            pre-commit run --files ${{needs.build.outputs.all}} || echo &quot;Exit status is $?&quot;
  </code></pre>
  <p>With this GitHub action I am getting an error in the lint job saying <code>fatal: not a git repository (or any of the parent directories): .git</code>. Is the issue within the lint job or a syntax elsewhere? The first job does not give any errors or telling messages.</p>