Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use git commands during a github action?

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?

like image 932
Vini.g.fer Avatar asked Feb 20 '26 19:02

Vini.g.fer


2 Answers

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
like image 200
Jhonny Ramirez Zeballos Avatar answered Feb 23 '26 07:02

Jhonny Ramirez Zeballos


Yes, there seems to be issues with running git commands in GitHub Actions given certain scenarios.

In my case it was the fact that my jobs were running in a container. The working directory in my job containers had NOT added to the Git safe.directory configuration.

In a job run in a container you'll need to add the working directory yourself. Do this before running any git commands.

git config --global --add safe.directory <repo-dir>

You can pass ${{ github.workspace }} as input to your actions to get the directory. Or use an Github Action default environment variable.

NOTE: Using actions/checkout@v4 will add the directory it checks out to the safe.directory setting. Sample actions/checkout@v4 output:

Adding repository directory to the temporary git global config as a safe directory
/usr/bin/git config --global --add safe.directory /home/runner/work/example-repo/example-repo

But that is on the host runner and not in job containers.

like image 29
b01 Avatar answered Feb 23 '26 09:02

b01



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!