huixisheng / huixisheng.github.com

前端开发者说,记录前端的故事
http://huixisheng.github.io/
12 stars 3 forks source link

gitlab-ci 配置 #116

Open huixisheng opened 3 years ago

huixisheng commented 3 years ago

最佳实践

stages:
  - install-packages
  - lint
  - unit-test

cache:
  # 缓存没有被 git 跟踪的文件
  untracked: true
  policy: pull-push
  # https://docs.gitlab.com/ee/ci/caching/
  # https://docs.gitlab.com/ce/ci/yaml/README.html#cache
  # GitLab CI/CD pipeline configuration reference | GitLab [https://docs.gitlab.com/ee/ci/yaml/README.html#cachekeyfiles](https://docs.gitlab.com/ee/ci/yaml/README.html#cachekeyfiles)
  key:
    files:
      - yarn.lock
    prefix: ${CI_JOB_NAME}
  paths:
    - node_modules

install-packages:
  stage: install-packages
  cache:
    policy: pull-push
    paths:
      - node_modules
  tags:
    - unit-test
  script:
    - yarn --silent

unit-test:
  stage: unit-test
  cache:
    policy: pull
    paths:
      - node_modules
  script:
    - yarn test
  tags:
    - unit-test
  only:
    - master
    - /^release.*$/
    - /^feature.*$/
    - merge_requests
  coverage: /All files\s*\|\s*([\d\.]+)/

lint:
  stage: lint
  only:
    - master
    - /^release.*$/
    - /^feature.*$/
    - merge_requests
  cache:
    policy: pull
    paths:
      - node_modules
  script:
    - yarn lint
  tags:
    - unit-test

历史方案

每次要修改 NODE_MODULES_VERSION 还是比较麻烦的

# 定义 stages
stages:
  - install-packages
  - lint
  - unit-test

# https://docs.gitlab.com/ee/ci/variables/#using-predefined-environment-variables
variables:
  NODE_MODULES_VERSION: "project-1.0.0"

cache:
  untracked: true
  policy: pull-push
  # https://docs.gitlab.com/ee/ci/caching/
  # https://docs.gitlab.com/ce/ci/yaml/README.html#cache
  key: "$NODE_MODULES_VERSION"
  paths:
    - node_modules

install-packages:
  stage: install-packages
  cache:
    policy: pull-push
    paths:
      - node_modules
  # 根据自己实际情况配置
  tags:
    - unit-test
  script:
    - yarn --silent

unit-test:
  stage: unit-test
  # https://segmentfault.com/a/1190000016483568
  cache:
    policy: pull
    paths:
      - node_modules
  script:
    - yarn test
  # 根据自己实际情况配置
  tags:
    - unit-test
  only:
    - master
    - /^release.*$/
    - /^feature.*$/
    - merge_requests
  coverage: /All files\s*\|\s*([\d\.]+)/

lint:
  stage: lint
  # https://segmentfault.com/a/1190000010442764#item-1-1
  only:
    - master
    - /^release.*$/
    - /^feature.*$/
    - merge_requests
  cache:
    policy: pull
    paths:
      - node_modules
  script:
    - yarn lint
  # 根据自己实际情况配置
  tags:
    - unit-test
huixisheng commented 3 years ago