在Gitlab中加快CI流程

时间:2019-02-28 15:24:52

标签: performance continuous-integration gitlab node-modules

我对Gitlab非常陌生,不知道如何正确设置它。我想知道如何在Gitlab中加快CI的流程,因为目前我的项目需要20m才能完成检查,构建和部署的过程。

我相信原因是因为每个作业都为npm installyarn install进行了另一个运行。我将缓存定义如下,但是并没有加快处理速度:

cache:
 key: ${CI_COMMIT_REF_SLUG}
 paths:
   - node_modules/

我使用的第一张图像是docket:git,是否应该将其更改为另一张图像,以便可以在npm install中运行before_script?还是有其他方法可以加快gitlab ci进程?

编辑: 添加gitlab-ci.yml文件,我删除了一些敏感信息,基本上,与我使用的信息相同

image: docker:git

stages:
- build
- build-image
- build-staging
- build-image-staging
- build-production
- build-image-production
- release
- checkstyle #TO move up
- test #To move up
- deploy

variables:
  CONTAINER_IMAGE: registry
  HOST: ""
  IP: ""
  DOCKER_DRIVER: overlay2

before_script:
  - git checkout -B "$CI_COMMIT_REF_NAME" "$CI_COMMIT_SHA"
  - echo "CI_BUILD_REF_NAME = "$CI_BUILD_REF_NAME
  - BRANCH=$(git rev-parse --abbrev-ref HEAD) && echo "BRANCH = "$BRANCH
  - ID=$(git rev-list --count $BRANCH) && echo "ID = "$ID
  - TAG=$(git describe --abbrev=0 --tags || true) && echo "TAG = "$TAG
  - REGISTRY=$CONTAINER_IMAGE":"$ID"_"$BRANCH
  - DATE=`date '+%Y-%m-%d %H:%M:%S'`
  - echo $'\n\n----------\n'"REGISTRY = "$REGISTRY$'\n'"COMMIT   = "$CI_COMMIT_SHA$'\n'"BRANCH   = "$BRANCH$'\n'"DATE     = "$DATE$'\n----------\n\n'
#  - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.example.com

cache:
 key: ${CI_COMMIT_REF_SLUG}
 paths:
   - node_modules/

analysis-lint:
  image: node:latest
  stage: checkstyle
  script:
    - npm install
    - ./node_modules/@angular/cli/bin/ng lint --type-check


build-integration:
  stage: build
  image: node
  script:
    - yarn install
    - ./node_modules/@angular/cli/bin/ng build ---prod --configuration=integration --aot --output-hashing all --source-map=false
    - npm run webpack:server
  artifacts:
    paths:
      - dist
  only:
    - develop

build-testing:
  stage: build
  image: node
  script:
    - yarn install
    - ./node_modules/@angular/cli/bin/ng build ---prod --configuration=testing --aot --output-hashing all --source-map=false
    - npm run webpack:server
  artifacts:
    paths:
      - dist
  only:
    - /^release.*$/

build-staging:
  stage: build-staging
  image: node
  script:
    - yarn install
    - ./node_modules/@angular/cli/bin/ng build ---prod --configuration=staging --aot --output-hashing all --source-map=false
    - npm run webpack:server
  artifacts:
    paths:
      - dist
  only:
    - /^hotfix.*$/
    - master

build-production:
  stage: build-production
  image: node
  script:
    - yarn install
    - ./node_modules/@angular/cli/bin/ng build ---prod --configuration=production --aot --output-hashing all --source-map=false
    - npm run webpack:server
  artifacts:
    paths:
      - dist
  only:
    - master

build-image-integration:
  stage: build-image
  script:
    - docker build -t $REGISTRY-integration -f Dockerfile --build-arg ENVIRONMENT=integration .
    - docker push $REGISTRY-integration
  only:
    - develop
    - universal

build-image-testing:
  stage: build-image
  script:
    - docker build -t $REGISTRY-testing -f Dockerfile --build-arg ENVIRONMENT=testing .
    - docker push $REGISTRY-testing
  only:
    - /^release.*$/

build-image-staging:
  stage: build-image-staging
  script:
    - docker build -t $REGISTRY-staging -f Dockerfile --build-arg ENVIRONMENT=staging .
    - docker push $REGISTRY-staging
  only:
    - /^hotfix.*$/
    - master

build-image-production:
  stage: build-image-production
  script:
    - docker build -t $REGISTRY-production -f Dockerfile --build-arg ENVIRONMENT=production .
    - docker push $REGISTRY-production
  only:
    - master

release-image-integration:
  stage: release
  script:
    - docker pull $REGISTRY-integration
    - docker tag $REGISTRY-integration $CONTAINER_IMAGE:$BRANCH
    - docker push $CONTAINER_IMAGE:$BRANCH
  only:
    - develop
    - universal

release-image-testing:
  stage: release
  script:
    - docker pull $REGISTRY-testing
    - docker tag $REGISTRY-testing $CONTAINER_IMAGE:$BRANCH
    - docker push $CONTAINER_IMAGE:$BRANCH
    - docker tag $REGISTRY-testing $CONTAINER_IMAGE:release
    - docker push $CONTAINER_IMAGE:release
  only:
    - /^release.*$/

release-image-staging:
  stage: release
  script:
    - docker pull $REGISTRY-staging
    - docker tag $REGISTRY-staging $CONTAINER_IMAGE:$BRANCH-staging
    - docker push $CONTAINER_IMAGE:$BRANCH-staging
  only:
    - /^hotfix.*$/
    - master

release-image-master:
  stage: release
  script:
    - docker pull $REGISTRY-production
    - docker tag $REGISTRY-production $CONTAINER_IMAGE:$BRANCH
    - docker push $CONTAINER_IMAGE:$BRANCH
  only:
    - master

release-image-latest:
  stage: release
  script:
    - docker pull $REGISTRY-production
    - docker tag $REGISTRY-production $CONTAINER_IMAGE:latest
    - docker push $CONTAINER_IMAGE:latest
    - docker tag $REGISTRY-production $CONTAINER_IMAGE
    - docker push $CONTAINER_IMAGE
  only:
    - master

release-image-production:
  stage: release
  script:
    - if [ ! -z "$TAG" ]; then docker pull $REGISTRY-production;docker tag $REGISTRY-production $CONTAINER_IMAGE:$TAG;docker push $CONTAINER_IMAGE:$TAG;fi;
  only:
    - master
    #- /^release.*$/
    #- /^hotfix.*$/

development:
  stage: deploy
  image: appropriate/curl
  script:
    - echo "Deploy to development server"
    - curl
  environment:
    name: development
    url:
  before_script: []
  when: manual
  only:
    - develop

integration-universal:
  stage: deploy
  image: appropriate/curl
  script:
    - echo "Deploy to integration server"
    - curl
  environment:
    name: integration
    url:
  before_script: []
  when: manual
  only:
    - universal

integration:
  stage: deploy
  image: appropriate/curl
  script:
    - echo "Deploy to integration server"
    - curl
  environment:
    name: integration
    url:
  before_script: []
  when: manual
  only:
    - develop

testing:
  stage: deploy
  image: appropriate/curl
  script:
    - echo "Deploy to testing server"
    - curl
  environment:
    name: testing
    url:
  before_script: []
  when: manual
  only:
    - /^release.*$/

staging:
  stage: deploy
  image: appropriate/curl
  script:
    - echo "Deploy to staging server"
    - curl
  environment:
    name: staging
    url:
  before_script: []
  when: manual
  only:
    - /^release.*$/
    - /^hotfix.*$/
    - master

production:
  stage: deploy
  image: appropriate/curl
  script:
    - echo "Deploy to production server"
    - curl
  environment:
    name: production
    url:
  before_script: []
  when: manual
  only:
    - master

1 个答案:

答案 0 :(得分:1)

在没有看到Dockerfile的情况下,可以肯定地说大部分时间都花在构建docker映像上。

为什么您需要有四个执行docker build的不同阶段?您应该将其简化为一个。您仍然可以在其他阶段拉出构建的映像以进行测试和集成。我看到您正在使用不同的--build-arg来构建容器。但是,构建特殊的测试图像有什么意义呢?您应该测试生产图像。

另一件事是查看并行化。 GitLab CI并行执行同一阶段的作业。您的某些工作似乎并不依赖于先前的工作。为什么不在同一阶段执行它们?

此外,我还不太了解您的before_script。为什么需要git checkout? Gitlab CI将自动检出当前提交的分支。