Github 操作错误;无法看到 git diff 到 Master

C-D*_*Dev 6 bash linter github-actions

我正在尝试找到一种方法来仅检查当前分支上已更改的 Markdown 文件。我编写了一个 bash 脚本,它在本地运行良好,但在 Github Actions 中中断。

预期结果:仅对拉取请求的 GitHub Actions 分支上已更改的 Markdown 文件进行 lint 处理。

#!/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
Run Code Online (Sandbox Code Playgroud)

我在 package.json 中调用脚本

"scripts": {
    "test": "bash mdlint.sh"
  }
Run Code Online (Sandbox Code Playgroud)

然后,我在 GitHub Actions 工作流程中调用 bash 脚本:

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
Run Code Online (Sandbox Code Playgroud)

这是 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>...]'
Run Code Online (Sandbox Code Playgroud)

有没有办法让 GitHub actions 仅对当前分支上更改的文件运行 linter?

pet*_*ans 11

我认为你可以通过master这样origin master的关联来解决它:

git fetch origin master:master
git diff --name-only master
Run Code Online (Sandbox Code Playgroud)

请注意,触发on: pull_request工作流的事件与分支无关,它们与合并提交 SHA 相关联。因此,actions/checkout@v1默认情况下将签出由 指定的合并提交GITHUB_SHA,而不是合并到基础的分支。请参阅此处的文档

您可以覆盖此默认行为并检查分支合并到基础中,如下所示:

      - uses: actions/checkout@master
        with:
          ref: ${{ github.head_ref }}
Run Code Online (Sandbox Code Playgroud)

我不确定这对于您的用例是否是必要的,但在diff合并提交上运行时可能会产生意想不到的影响。