0
votes

I have a aspnet core mvc project put under GitLab, it use DockerFile to containerise docker image. Now I want to utilise GitLab CI feature by having build and test 2 stages.

My plan is build is for docker build command; test is docker pull and run command. I have complete most of the .gitlab.ci.yml code, but I encounter an issue for test stage.

It looks like the gitlab shared runner docker image dotnet cli version is lower than my expected version. Here is the output:

 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]

This is my 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"]

Here is my .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
}

I have been trying sudo apt-get, apt-get, apk add, but no hope, any suggestion?

1
Please add the build part and your dockerfile. - RiWe
@RiWe I added Dockerfile and ci build part - Kira Hao
Does the image work as expected on your local machine? The only thing that comes into my mind is that microsoft/dotnet:aspnetcore-runtime uses a different dotnet version than microsoft/dotnet:2.2-sdk. - RiWe
@RiWe I think I found the answer, you can take a look on my answer below - Kira Hao

1 Answers

0
votes

So I manage to make it work with following configuration.

  1. put image and service into stage section.
build:
 image: docker:latest
 services:
   - docker:dind
...
test:
 image: docker:latest
 services:
   - docker:dind
   - microsoft/dotnet

by doing this, the gitlab ci runner able to pick up latest Microsoft dotnet environment, and it able to make let docker run