【发布时间】:2020-05-23 11:48:52
【问题描述】:
我在 GitLab 下有一个 aspnet core mvc 项目,它使用 DockerFile 来容器化 docker 镜像。现在我想通过构建和测试 2 个阶段来利用 GitLab CI 功能。
我的计划是构建用于 docker build 命令;测试是 docker pull 和 run 命令。我已经完成了大部分 .gitlab.ci.yml 代码,但在测试阶段遇到了问题。
看起来 gitlab shared runner docker image dotnet cli 版本低于我的预期版本。这是输出:
It was not possible to find any compatible framework version
The specified framework 'Microsoft.AspNetCore.App', version '2.2.0' was not found.
- Check application dependencies and target a framework version installed at:
/usr/share/dotnet/
- Installing .NET Core prerequisites might help resolve this problem:
https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
- The .NET Core framework and SDK can be installed from:
https://aka.ms/dotnet-download
- The following versions are installed:
2.1.15 at [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
这是我的Dockerfile
FROM microsoft/dotnet:2.2-sdk AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
# override the request to 5000
ENV ASPNETCORE_URLS=http://+:5002
# override to 5002
EXPOSE 5002
ENTRYPOINT ["dotnet", "ASPNetApp_2.dll"]
这是我的.gitlab.ci.yml
image: docker:latest
stages:
- build
- test
services:
- docker:dind
variables:
IMAGE_NAME: ${CI_REGISTRY}/${CI_PROJECT_PATH}
build:
only:
- master
- develop
before_script:
- apk update && apk add bash
- chmod +x ./function.sh
- . function.sh
- get_image_tag
- echo $TAG
- docker login registry.gitlab.com -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD}
script:
- docker build -t $IMAGE_NAME:$TAG .
- docker push $IMAGE_NAME:$TAG
after_script:
- docker logout ${CI_REGISTRY}
stage: build
tags:
- docker
test:
stage: test
before_script:
- apk update && apk add bash
- apt-get install aspnetcore-runtime-2.2
- chmod +x ./function.sh
- . function.sh
- get_image_tag
- echo $TAG
- docker login registry.gitlab.com -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD}
script:
- docker pull microsoft/dotnet:2.2-aspnetcore-runtime
- docker pull $IMAGE_NAME:$TAG
- docker run -p 5002 $IMAGE_NAME:$TAG -it
after_script:
- docker logout ${CI_REGISTRY}
function.sh
#!/bin/bash
function get_image_tag(){
if [ "$CI_COMMIT_REF_SLUG" == "master" ]; then
export TAG="latest";
else
export TAG=$CI_COMMIT_REF_SLUG;
fi
}
我一直在尝试sudo apt-get、apt-get、apk add,但没有希望,有什么建议吗?
【问题讨论】:
-
请添加构建部分和您的 dockerfile。
-
@RiWe 我添加了 Dockerfile 和 ci 构建部分
-
图像在您的本地计算机上是否按预期工作?我唯一想到的是
microsoft/dotnet:aspnetcore-runtime使用的 dotnet 版本与microsoft/dotnet:2.2-sdk不同。 -
@RiWe 我想我找到了答案,你可以看看下面我的答案
标签: bash docker gitlab gitlab-ci alpine