我正在Gitlab-CI上(在gitlab.com上)运行CI / CD。 我知道如何从一个阶段缓存结果以在另一阶段重用。
但是,我不知道如何缓存我的建筑图像,以防止每次触发构建时都从GCR.io下载它们。这很耗时,而且效率很低。
因此,下面是简化的管道以突出我的问题。
node:lts
图片。cloud-sdk
映像上运行一些特定的测试(使用node:lts
上不可用的工具)docker:dind
。GKE
,所以我需要再次使用所有工具和设置来运行cloud-sdk
,以连接到GKE
。因此,我在每个管道上提取cloud-sdk
图像的两倍,但更重要的是,我在每个管道上提取3个不同的图像。始终完全相同的3个。相同sha1。
如果我可以在本地缓存(Gitlab注册中心?特定的Gitlab缓存?...?),它将加快处理过程并减少带宽消耗,...
您知道实现此目标的任何解决方案吗?
variables:
NODE_ENV: "development"
GCP_PROJECT_ID: my-little-project
IMAGE_NAME: simin-api
stages:
- test
- publish
- deploy
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
test_requirement:
image: gcr.io/google.com/cloudsdktool/cloud-sdk:291.0.0
stage: test
script:
# Authenticate with GKE
- echo "Authentication..."
# Straight forward testing
- ./run-all-tests.sh
environment:
name: dev
lint:
image: node:lts-alpine # Use commit sha instead. Named version here for the sake of reading
stage: test
script:
- npm install lint
- npm run lint
publish-image:
tags:
- docker
- gce
stage: publish
image: docker:19.03.5-dind # Use commit sha instead. Named version here for the sake of reading
services:
- docker:19.03.5-dind
variables:
DOCKER_DRIVER: overlay2
script:
# Do cool stuffs
- echo "Prepare for Docker login..."
- docker login -u _json_key --password-stdin https://gcr.io < ${HOME}/gcloud-service-key.json
# Build image
- docker build -t $IMAGE_NAME .
# Optionally tag the image with the commit short-sha or the tag name
- export IMAGE_VERSION=$([ -z $CI_COMMIT_TAG ] && echo $CI_COMMIT_SHA | cut -c1-8 || echo $CI_COMMIT_TAG)
- echo "Tagging and pushing gcr.io/$GCP_PROJECT_ID/$IMAGE_NAME:$IMAGE_VERSION"
- docker tag $IMAGE_NAME gcr.io/$GCP_PROJECT_ID/$IMAGE_NAME:$IMAGE_VERSION
- docker tag $IMAGE_NAME gcr.io/$GCP_PROJECT_ID/$IMAGE_NAME:latest
- docker push gcr.io/$GCP_PROJECT_ID/$IMAGE_NAME:$IMAGE_VERSION
- docker push gcr.io/$GCP_PROJECT_ID/$IMAGE_NAME:latest
only:
- master
- tags
deploy:
tags:
- docker
- gce
image: gcr.io/google.com/cloudsdktool/cloud-sdk:291.0.0
stage: deploy
script:
# Authenticate with GKE
- echo "Authentication..."
# Set commit tag to use in the deploy.yaml
- export IMAGE_VERSION=$([ -z $CI_COMMIT_TAG ] && echo $CI_COMMIT_SHA | cut -c1-8 || echo $CI_COMMIT_TAG)
# Set commit tag to use in the deploy.yaml
- sed -i "s/TAG_TO_BE_CHANGED/$IMAGE_VERSION/g" kubernetes/deploy.yaml
# Do the deployment
- echo "Deploying image gcr.io/my-little-project/simin-api:$IMAGE_VERSION"
- kubectl apply -f kubernetes/deploy.yaml
environment:
name: dev
# Only run builds for these refs
only:
- master
- tags
干杯
奥利维尔