CI 管道中的单独作业导致非常长的管道 运行

Separate Jobs in CI Pipeline result in very long pipeline run

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

管道通常定义为一系列作业(根据 Gitlab's documentation), so in order to properly script your repository's CI/CD where splitting your jobs functions keeps a similar performance to when they were together, a common practice is to implement caching between stages

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

现在让我们看一下您的 gitlab-ci.yml 代码,以实施缓存以优化您的管道执行时间。 Flutter 的文档 says a global cache is created once you install each package, and therefore you only have to install once per build,位于 .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 的界面上手动删除它。