Github Actions Error; cannot see git diff to Master
Asked Answered
C

1

6

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?

Collage answered 30/10, 2019 at 16:42 Comment(0)
E
11

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.

Embryologist answered 31/10, 2019 at 3:40 Comment(3)
thank you so much @peterevans, this is working great. I changed the first line in my bash script to: files=`(git fetch origin master:master) && (git diff --name-only master) and your proposed changes in the workflow file.Collage
@Embryologist I'm not sure I understand "actions/checkout@v1 by default will checkout the merge commit specified by GITHUB_SHA, not the branch that is merging into the base" - what's the functional difference there?Fatuity
@RubenMartinezJr. The difference is the checked out commit SHA refers to a merge commit, not the commit SHA of the branch. I think in the majority of cases (maybe all?) there will be no functional difference because the git diff with the base will be the same.Embryologist

© 2022 - 2024 — McMap. All rights reserved.