GitLab CI with JS Linting

最后都变了- 提交于 2019-12-07 02:34:16

问题


I have 0 experience with GitLab Continuous Integration and I need to setup a job to run ESLint on .js files.

I've already read the GitLab CI and Pipeline documentations, along with some Git Hooks, but I still have no idea how to setup this, so any detailed and starting from the very beginning answer is appreciated.

Thanks,


回答1:


First you need to setup your CI and have some runners available so they can run your continuous integration jobs. The easiest way for this is to use gitlab-ci-multi-runner (project is here along with documentation) along with the docker executor that will run your CI jobs in docker containers. Once you have configured some runners, add them to your Gitlab project so they're available to run jobs.

Once that's taken care of, you need to add a .gitlab-ci.yml file to your project. This file is used to describe the jobs that need to run during continuous integration etc. Here is an example (assuming you install eslint using npm)

image: node:latest

stages:
  - lint

eslint:
  stage: lint
  script:
    # Install ESLint in this docker container
    - npm install -g eslint
    # Configure ESLint (will read your .eslintrc file)
    - eslint --init
    # Run ESLint
    - eslint <your_js_file>

Add your .gitlab-ci.yml file, commit and push the changes. The CI pipeline should start and run the above steps.




回答2:


If you'd like to have comments to your PRs here is an example with eslint and pronto. (we have ruby app so we check ruby code style too )

image: 'circleci/ruby:2.5.1-node-browsers'

codestyle:
  script:
  - sudo apt -y install cmake
  # install eslint dependencies
  - sudo npm install -g eslint
  - sudo npm install -g eslint-plugin-babel
  - sudo npm install -g eslint-plugin-react
  - sudo npm install -g eslint-plugin-import
  - sudo npm install -g babel-eslint
  - sudo npm install -g eslint-config-airbnb
  - sudo npm install -g eslint-plugin-jsx-a11y
  # install pronto runners
  - gem install pronto --no-ri
  - gem install pronto-rubocop --no-ri
  - gem install rubocop-rspec --no-ri
  - gem install pronto-eslint_npm --no-ri
  # run linters
  - vendor/ruby/bin/pronto run -f gitlab -c origin/dev --exit-code

You can also run linters separately:

- vendor/ruby/bin/pronto run -r eslint_npm -f gitlab -c origin/dev --exit-code

This piece -f gitlab -c origin/dev tells linters to check only chenged lines of code.

Also if you use pronto-eslint_npm and want to check files in specific folder add

.pronto_eslint_npm.yml that will contain needed regex. (in my case it has next line)

files_to_lint: app\/frontend\/\S*(\.js|\.es6|\.jsx)$


来源:https://stackoverflow.com/questions/43318464/gitlab-ci-with-js-linting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!