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?
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 ofgit
is installed and does the current folder contain a.git
folder?.git
folder. It should be created when cloning the project right?.git
folder, then git commands won't workls .git/refs/heads
), so you need to do agit fetch --all
orgit fetch origin auto-lint dev
. At this point, I would recommend, you just use thegit
command to fetch the repo and do everything else you need, rather than relying on thecheckout
action. Thecheckout
action is useful if you just want to work with the repo's files, not for working with refs and other git internals