我在网上搜索了如何加快CI的构建时间,我实现了这些方法,但是,每次我将代码推送到gitlab时,它都会从头(docker)重新构建映像,而无需使用任何缓存,即使什么都没有改变。
编辑 这是我的.gitlab-ci.yml文件的样子:
# ### Define variables
#
variables:
OBJECTS_DIRECTORY: 'obj'
NUGET_PACKAGES_DIRECTORY: '.nuget'
SOURCE_CODE_PATH: 'src/'
REACT_PATH: ClientApp
DOCKER_DRIVER: overlay2
# ### Define stage list
stages:
- build
- push
- test
- deploy
cache:
# Per-stage and per-branch caching.
key: "$CI_JOB_STAGE-$CI_COMMIT_REF_SLUG"
paths:
- '$SOURCE_CODE_PATH$REACT_PATH/node_modules/'
build:
image: docker:18.09
stage: build
services:
- docker:18.09-dind
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker pull $CI_REGISTRY_IMAGE:latest || true
script:
- >
docker build
--pull
--cache-from $CI_REGISTRY_IMAGE:latest -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
push latest:
image: docker:18.09
stage: push
services:
- docker:18.09-dind
variables:
GIT_STRATEGY: none
only:
- master
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
- docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:latest
- docker push $CI_REGISTRY_IMAGE:latest
这是我的Dockerfile的样子:
FROM microsoft/dotnet:2.2-sdk AS build-env
WORKDIR /source
RUN curl -sL https://deb.nodesource.com/setup_11.x | bash - \
&& apt-get install -y nodejs
COPY ./src/*.csproj .
RUN dotnet restore
COPY ./src/ ./
RUN dotnet publish "./Spa.csproj" --output "./dist" --configuration Release --no-restore
FROM microsoft/dotnet:2.2-aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /source/dist .
RUN apt-get update \
&& apt-get install -y apt-transport-https lsb-release ca-certificates \
&& curl -S https://packages.sury.org/php/apt.gpg --output /etc/apt/trusted.gpg.d/php.gpg \
&& echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list \
&& apt-get update
RUN apt-get install -y php7.2-cli
COPY deployment.ini /app
ADD ./src/appsettings.Sample.json /app/appsettings.json
EXPOSE 80
ENTRYPOINT ["dotnet", "Spa.dll", "--environment=Production"]
一切看起来都很正确,所以我不知道为什么它在构建阶段不使用缓存