【问题标题】:ESLint doesn't break build with Github ActionsESLint 不会破坏 Github Actions 的构建
【发布时间】:2021-07-11 22:21:21
【问题描述】:

我是第一次使用 Github Actions。我正在使用 Vue-CLI 并且我想创建一个作业,在 push 上对我的文件进行 lint,如果有 ESLint'errors,则中断构建过程。

这是我的package.json 的脚本:

"scripts": {
  "serve": "vue-cli-service serve",
  "build": "vue-cli-service build",
  "lint": "vue-cli-service lint",
},

这是我的.github/workflows/main.yml 文件:

name: Lint files on push

on: push

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install modules
        run: npm install
      - name: Run ESLint
        run: npm run lint

我以this template 为灵感。正如您在下面的屏幕截图中看到的那样。该作业已成功完成,但不会破坏构建或修复掉毛。我做错了什么?

【问题讨论】:

    标签: javascript github continuous-integration eslint github-actions


    【解决方案1】:

    您可以指定--no-fix,这样它就不会自动修复错误/警告并将--max-warnings 设置为0,因为您的项目中似乎有一个警告并希望将其视为错误。

    在你的package.json

    {
      ....
      "scripts": {
        "lint": "vue-cli-service lint --no-fix --max-warnings 0",
        ....
      }
    }
    

    checkout documentation

    日志输出:

    warning: Delete `⏎········` (prettier/prettier) at src/components/QuestionInfo.vue:5:25:
      3 |     <div class="question_card container">
      4 |       <BaseTag
    > 5 |         :class="['tag', 
        |                         ^
      6 |         'p-5', 'pb-0', 'pl-0', 'ml-5']"
      7 |         :tag-name="this.tag"
      8 |       />
    
    
    1 warning found.
    1 warning potentially fixable with the `--fix` option.
    Eslint found too many warnings (maximum: 0).
    

    【讨论】:

    • 感谢您的回答。我找到了一种方法来整理文件并提交它们。您认为最好在推送时修复它们还是只显示错误以便开发人员可以解决它们?所以基本上什么是最好的?您的解决方案只有错误还是像我的一样自动修复它们?老实说,我不知道这里的最佳做法是什么?
    • @ManuelAbascal 如果您提交并推送 github 操作,您需要记住每次在工作流完成运行后拉取。如果您忘记拉取,可能会导致烦人的合并,因为当您仍在同一分支(在同一文件上)工作时,任何文件可能已经被您的 github 操作修改。我认为这取决于你的工作方式,如果只在一个特定的分支上工作,谁在工作,如果很多人在同一个分支上工作,这可能不是一个好主意
    • 这真的很有趣,因为这正是我刚刚经历的!由于您提到的内容,我想最好不要自动修复警告。对于团队协作,新开发人员可能会感到困惑。我将改用您的解决方案,如果我想要警告自动修复,我可以随时为他们使用 githooks。谢! @伯特兰
    • @ManuelAbascal 不客气。我创建了一个covid19 New York scraper,它会抓取一个网站并自动将 Github 操作的结果推送到特定文件夹中的同一分支,我经常忘记自己在推送之前拉动(尽管我是唯一一个从事该项目的人)
    • 哈哈,我认为最好不要在 CI 中自动修复!我为你的项目加了星标! ;)
    【解决方案2】:

    我发现了这个问题。我正在对文件进行 linting,但由于我没有提交和推送新更改,因此我在 PR 中看不到更改。

    我已经重构了我的Github Action's workflow 以检查文件并将它们推送到存储库。

    name: Lint files on push
    
    on: push
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
          - name: Installing node_modules
            run: npm install
          - name: Running ESLint
            run: npm run lint
          - name: Setting Github user.email
            run: git config --global user.email "<github-email-goes-here>"
          - name: Setting Github user.name
            run: git config --global user.name "<github-name-goes-here>"
          - name: Adding fixes for committing
            run: git add .
          - name: Committing ESLint fixes
            run: git commit -m "fixed ESLint issues"
          - name: Pushing fixes to current PR branch
            run: git push
    

    您可以分别在终端输入git config --global user.emailgit config --global user.name 来获取您的user.emailuser.name

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-29
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 2012-09-15
      • 2016-01-02
      • 1970-01-01
      相关资源
      最近更新 更多