[英]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.