Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Actions Error; cannot see git diff to Master

I am trying to find a way to lint only markdown files that have changed on the current branch. I wrote a bash script that runs fine locally, but breaks in Github Actions.

Expected Outcome: lint only markdown files that have been changed on the branch in GitHub Actions on pull-request.

#!/bin/bash
files=`git diff --name-only master`
for x in $files;
do
    if [ ${x: -3} == ".md" ]
    then
        node_modules/.bin/markdownlint $x
    fi
done

I call the script in package.json

"scripts": {
    "test": "bash mdlint.sh"
  }

I then call the bash script in GitHub Actions workflow:

name: CI

on: 
  pull_request:
    branches:
    - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
      with:
        fetch-depth: 1
    - uses: actions/setup-node@master
    - name: lint all markdownfiles
      run: |
        npm install
        npm run test

This is the Error in GitHub Actions:

  shell: /bin/bash -e {0}
npm WARN Gatsby_site No repository field.
npm WARN Gatsby_site No license field.

added 33 packages from 26 contributors and audited 43 packages in 0.952s
found 0 vulnerabilities


> @ test /home/runner/work/Gatsby_site/Gatsby_site
> bash mdlint.sh

fatal: ambiguous argument 'master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Is there a way to make GitHub actions run the linter only on the files that changed on the current branch?

like image 271
C-Dev Avatar asked Sep 07 '26 23:09

C-Dev


1 Answers

I think you can resolve it by associating master with origin master like this:

git fetch origin master:master
git diff --name-only master

Be aware that events that trigger on: pull_request workflows are not associated with a branch, they are associated with a merge commit SHA. So actions/checkout@v1 by default will checkout the merge commit specified by GITHUB_SHA, not the branch that is merging into the base. See the documentation here.

You can override this default behaviour and checkout the branch merging into the base like this:

      - uses: actions/checkout@master
        with:
          ref: ${{ github.head_ref }}

I'm not sure if this will be necessary for your use case, but there might be unintended effects when running diff on the merge commit.

like image 188
peterevans Avatar answered Sep 10 '26 14:09

peterevans