0

我有以下管道的一个版本,它作为单个作业运行,所有这些都花费了不到 6 分钟。然后我更新了管道,将其分解为单独的作业,以便更容易知道哪个失败了,现在总管道是 15 分钟以上。我只能假设每个作业都被认为是自己的管道,拆卸/拆卸过程在四个不同的作业中需要很长时间。我正在寻找有关如何重构此 GitLab CI 以恢复到我原来的 6 或更少时间长度的建议:

image: cirrusci/flutter:stable

before_script:
  - flutter pub get
  - flutter clean
  - flutter --version

stages:
  - build-aot
  - analyze
  - format-check
  - test-on-machine-with-coverage

build-aot:
  stage: build-aot
  script:
    - flutter build aot
  only:
    - master
    - merge_requests
  except:
     variables:
       - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master" 

analyze:
  stage: analyze
  script:
    - flutter analyze
  only:
    - master
    - merge_requests
  except:
     variables:
       - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master" 

format-check:
  stage: format-check
  script:
    - flutter format --set-exit-if-changed lib test
  only:
    - master
    - merge_requests
  except:
     variables:
       - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master" 

test-on-machine-with-coverage:
  stage: test-on-machine-with-coverage
  script:
    - flutter pub global activate junitreport
    - export PATH="$PATH":"$HOME/.pub-cache/bin"
    - flutter test --machine | tojunit -o report.xml
    - flutter test --coverage ./lib 
    - lcov -r coverage/lcov.info '*/__test*__/*' -o coverage/lcov_cleaned.info
    - genhtml coverage/lcov_cleaned.info --output=coverage
  artifacts:
    when: always
    paths:
      - rspec.xml
      - coverage
    reports:
      junit:
        - report.xml
  only:
    - master
    - merge_requests
  except:
     variables:
       - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master"
4

1 回答 1

0

管道通常被定义为一系列作业(根据Gitlab 的文档),因此为了正确编写存储库的 CI/CD 脚本,在其中拆分作业功能时保持与它们在一起时相似的性能,一种常见的做法是实现缓存阶段之间

缓存允许您正确隔离每个作业运行时环境,同时优化速度和带宽,因为它避免了多次下载或构建相同的文件。

现在让我们看一下您的gitlab-ci.yml代码,以实现缓存以优化您的管道执行时间。Flutter 的文档说,一旦你安装了每个包,就会创建一个全局缓存,因此你每次构建只需要安装一次,它位于.pub-cache

image: cirrusci/flutter:stable

variables:
  PUB_CACHE: $CI_PROJECT_DIR/.pub-cache #https://docs.gitlab.com/ee/ci/yaml/README.html#cachepaths

cache:
  key: ${CI_COMMIT_REF_SLUG} #Only use one cache per whole pipeline
  paths:
    - .pub-cache/

before_script:
  - flutter pub cache --all #https://dart.dev/tools/pub/cmd/pub-cache
  - flutter --version

stages:
  - build-aot
  - analyze
  - format-check
  - test-on-machine-with-coverage

build-aot:
  stage: build-aot
  script:
    - flutter build aot
  only:
    - master
    - merge_requests
  except:
    variables:
      - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master" 
    
analyze:
  stage: analyze
  script:
    - flutter analyze
  only:
    - master
    - merge_requests
  except:
     variables:
       - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master" 

format-check:
  stage: format-check
  script:
    - flutter format --set-exit-if-changed lib test
  only:
    - master
    - merge_requests
  except:
     variables:
       - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master" 

test-on-machine-with-coverage:
  stage: test-on-machine-with-coverage
  script:
    - flutter pub global activate junitreport
    - export PATH="$PATH":"$HOME/.pub-cache/bin"
    - flutter test --machine | tojunit -o report.xml
    - flutter test --coverage ./lib 
    - lcov -r coverage/lcov.info '*/__test*__/*' -o coverage/lcov_cleaned.info
    - genhtml coverage/lcov_cleaned.info --output=coverage
  artifacts:
    when: always
    paths:
      - rspec.xml
      - coverage
    reports:
      junit:
        - report.xml
  only:
    - master
    - merge_requests
  except:
     variables:
       - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master"

这仍然会在每个新系列的管道(提交更改时)下载你的包,删除key:标志应该保留缓存,直到你在 Gitlab 的界面上手动删除它。

于 2021-02-18T06:05:47.033 回答