[英]Speed up CI process in Gitlab
我对 Gitlab 很陌生,我不知道如何正确设置它。 我想知道我们如何在Gitlab中加快CI过程,因为目前我的项目花了20m来完成检查、构建和部署的过程。
我相信原因是因为每个作业都会再次运行npm install
或yarn install
。 我将缓存定义如下,但它没有加快进程:
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
我使用的第一个图像是docket:git
,我应该将它更改为另一个图像以便我可以在before_script
运行npm install
吗? 或者还有其他方法可以加快 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
在没有看过Dockerfile
,可以肯定地说大部分时间都花在构建Dockerfile
上还是相对安全的。
为什么你需要有 4 个不同的阶段来执行docker build
? 你应该把它归结为一个。 您仍然可以在其他阶段拉取构建的镜像以进行测试和集成。 我看到您正在使用不同的--build-arg
构建容器。 但是,构建一个特殊的图像进行测试有什么意义呢? 您应该测试您的生产图像。
另一件事是研究并行化。 GitLab CI 并行执行同一阶段的作业。 你的一些工作似乎不依赖于前一个。 为什么不在同一阶段执行它们呢?
此外,我不完全理解您的before_script
。 为什么需要git checkout
? Gitlab CI 会自动检出你当前提交的分支。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.