Gitlab CI Docker和Aspnetcore运行时版本问题

问题描述 投票:0回答:1

我在GitLab下有一个aspnet核心mvc项目,它使用DockerFile容器化docker映像。现在,我想通过构建和测试2个阶段来利用GitLab CI功能。

我的计划是构建用于docker build命令;测试是docker pull and run命令。我已经完成了大多数.gitlab.ci.yml代码,但在测试阶段遇到了问题。

看来gitlab共享运行器docker映像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-getapt-getapk add,但没有希望,有任何建议吗?

bash docker gitlab gitlab-ci alpine
1个回答
0
投票

所以我设法使其与以下配置一起使用。

  1. 将图像和服务放入阶段部分。
build:
 image: docker:latest
 services:
   - docker:dind
...
test:
 image: docker:latest
 services:
   - docker:dind
   - microsoft/dotnet

通过这样做,gitlab ci运行程序能够获取最新的Microsoft dotnet环境,并且能够使docker运行

© www.soinside.com 2019 - 2024. All rights reserved.