CI 管道中的单独作业导致管道运行时间很长

时间:2021-02-17 23:47:02

标签: flutter continuous-integration gitlab-ci github-ci

我有一个作为单个作业运行的以下管道版本,所有这一切都用了不到 6 分钟。然后我更新了管道,将其分解为单独的作业,以便更容易知道哪个失败了,现在总管道超过 15 分钟。我只能假设每个 Job 都被认为是自己的管道,在四个不同的作业中需要花费很长时间的拆卸/关闭过程。我正在寻找有关如何重构此 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"

1 个答案:

答案 0 :(得分:0)

管道通常定义为一系列作业(根据 Gitlab's documentation),因此为了正确编写存储库的 CI/CD 脚本,其中拆分作业功能保持与它们在一起时相似的性能,{ {3}}。

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

现在让我们看看您的 gitlab-ci.yml 代码,以实现缓存以优化您的管道执行时间。 Flutter 的文档 a common practice is to implement caching between stages,位于 .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 的界面上手动擦除它。