11

Trying to auto-apply black and isort to only the files changed on some python code with github actions during a pull request on a self-hosted runner, and after that commit to the PR. But get errors such as Not a git repository on some of the steps. Here is my workflow file:

name: Autolint

on:
  pull_request:
    types: [opened, synchronize]

jobs:
  run-linters:
    name: Run linters
    runs-on: self-hosted
    container:
      image: edlut/azion:monster-action-base
      options: --privileged

    steps:
    - name: Install git 
      run: |
        apt-get install -y git
        git --version
        echo "Path is ... $PATH"
        PATH=$PATH:$(which git)
        echo "Path is ... $PATH"

    - uses: actions/checkout@v2

    - name: Debug - Check if .git folder exists
      run: |
        ls -lah

    - name: Install Python dependencies
      run: pip3 install black isort

    - name: Apply Black
      env:
        BRANCH: ${{ github.head_ref }}
      run: |
        echo "Branch is ... ${BRANCH}"
        git diff --name-only "$GITHUB_BASE_REF..${BRANCH}" | grep .py | xargs black -l 119

    - name: Apply isort
      env:
        BRANCH: ${{ github.head_ref }}
      run: |
        git diff --name-only "$GITHUB_BASE_REF..${BRANCH}" | xargs isort

    - name: Check for modified files
      id: git-check
      run: echo ::set-output name=modified::$(if git status | grep "nothing to commit"; then echo "false"; else echo "true"; fi)

    - name: Push changes
      if: steps.git-check.outputs.modified == 'true'
      run: |
        git config --global user.name 'My Name'
        git config --global user.email '[email protected]'
        git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
        # git commit -am "style: Apply Black style"
        # git push

Can anybody help me out on how to achieve this?

6
  • 2
    From the checkout actions readme page, When Git 2.18 or higher is not in your PATH, falls back to the REST API to download the files.. What version of git is installed and does the current folder contain a .git folder?
    – smac89
    Jul 17, 2020 at 23:02
  • Git is using version 2.25.1, and the current folder doesn't have a .git folder. It should be created when cloning the project right?
    – Vini.g.fer
    Jul 17, 2020 at 23:18
  • Correct. If there is no .git folder, then git commands won't work
    – smac89
    Jul 17, 2020 at 23:28
  • 4
    That's probably because the ref does not exist in your git refs (ls .git/refs/heads), so you need to do a git fetch --all or git fetch origin auto-lint dev. At this point, I would recommend, you just use the git command to fetch the repo and do everything else you need, rather than relying on the checkout action. The checkout action is useful if you just want to work with the repo's files, not for working with refs and other git internals
    – smac89
    Jul 17, 2020 at 23:51
  • 1
    @Vini.g.fer how did you get the .git folder? Maybe add an Answer for it?
    – Paul
    Dec 10, 2020 at 23:52

1 Answer 1

17
name: Version Build

on:
  push:
    branches: [ develop ]

jobs:
  build-version:

    runs-on: ubuntu-latest

    steps:
    - name: Git checkout
      uses: actions/checkout@v2
      with:
        fetch-depth: '0'
    - name: git
      run: |
        # setup the username and email. I tend to use 'GitHub Actions Bot' with no email by default
        git --version
        git config user.name "GitHub Actions Bot"
        git config user.email "<>"
        git status
        git tag
        git describe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.