gitlab-ci构建asp.net核心docker

时间:2020-04-03 12:31:24

标签: docker asp.net-core gitlab gitlab-ci

我正在研究gitlab CI / CD项目,以将asp.net core应用程序构建到docker中。 目前,我有2种可能的实现方式。第一个在Dockerfile中具有完整的逻辑,但是我无法以这种方式可视化Gitlab中的各个阶段(构建,测试,发布)。因此,我考虑将主要逻辑移至gitlab-ci.yml文件。但是现在令我困扰的是我必须在2个地方(sdk:3.1,aspnet:3.1.1-alpine3.10)管理映像docker dotnet versions。通过build-arg交付版本是个好主意,还是有一个更优雅的解决方案?

.gitlab-ci.yml

stages:
  - build
  - test
  - docker

build:
  stage: build
  image: mcr.microsoft.com/dotnet/core/sdk:3.1
  only:
    - master
  script:
    - cd src
    - dotnet restore --interactive
    - dotnet build --configuration Release
    - dotnet publish --configuration Release --output ../publish/
  artifacts:
    paths:
      - ./publish/*.*
    expire_in: 1 week
  tags:
  - docker

test:
  stage: test
  image: mcr.microsoft.com/dotnet/core/sdk:3.1
  only:
    - master
  script:
    - cd src
    - dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=../../MyProject.xml"
  artifacts:
    paths:
      - ./MyProject.xml
    reports:
      junit: ./MyProject.xml
  tags:
  - docker

docker:
  stage: docker
  image: docker:stable
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
  only:
    - master
  script:
    - docker login -u "gitlab-ci-token" -p "$CI_JOB_TOKEN" $CI_REGISTRY
    - docker build --tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" --tag "$CI_REGISTRY_IMAGE:latest" --build-arg EXECUTABLE=Test.WebApi.dll .
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"
    - docker push "$CI_REGISTRY_IMAGE:latest"
  tags:
  - docker

Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1.1-alpine3.10
ARG EXECUTABLE

WORKDIR /app
COPY /publish .

ENV ASPNETCORE_URLS "http://*:5000"
ENV ASPNETCORE_ENVIRONMENT "Staging"
CMD ["dotnet", "$EXECUTABLE"]

2 个答案:

答案 0 :(得分:4)

这是我的解决方案,我已经定义了上面的变量,并将其在docker文件中替换为sed

我的解决方案有这两个项目

  • Test.WebApi(WebApi项目)
  • Test.WebApi.UnitTest(单元测试项目)

@ThomasBrüggemann感谢您的启发。

.gitlab-ci.yml

variables:
    PROJECT_NAME: "Test.WebApi"
    BUILD_IMAGE: "mcr.microsoft.com/dotnet/core/sdk:3.1"
    RUNTIME_IMAGE: "mcr.microsoft.com/dotnet/core/aspnet:3.1.1-alpine3.10"

stages:
  - build
  - test
  - docker

build:
  stage: build
  image: $BUILD_IMAGE
  only:
    - master
  script:
    - cd src/$PROJECT_NAME
    - dotnet restore --interactive
    - dotnet build --configuration Release
    - dotnet publish --configuration Release --output ../../publish/
  artifacts:
    paths:
      - ./publish/*
    expire_in: 1 week
  tags:
  - docker

test:
  stage: test
  image: $BUILD_IMAGE
  only:
    - master
  script:
    - cd src/$PROJECT_NAME.UnitTest
    - dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=../../UnitTestResult.xml"
  artifacts:
    paths:
      - ./UnitTestResult.xml
    reports:
      junit: ./UnitTestResult.xml
  tags:
  - docker

docker:
  stage: docker
  image: docker:stable
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
  only:
    - master
  script:
    # Prepare Dockerfile
    - sed -i "s~\$DOCKERIMAGE~$RUNTIME_IMAGE~g" Dockerfile
    - sed -i 's/$ENVIRONMENT/Staging/g' Dockerfile
    - sed -i "s/\$ENTRYPOINT/$PROJECT_NAME.dll/g" Dockerfile

    # Process Dockerfile
    - docker login -u "gitlab-ci-token" -p "$CI_JOB_TOKEN" $CI_REGISTRY
    - docker build --tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" --tag "$CI_REGISTRY_IMAGE:latest" .
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"
    - docker push "$CI_REGISTRY_IMAGE:latest"
  tags:
  - docker

Dockerfile

FROM $DOCKERIMAGE

WORKDIR /app
COPY /publish .

EXPOSE 5000/tcp

ENV ASPNETCORE_URLS "http://*:5000"
ENV ASPNETCORE_ENVIRONMENT "$ENVIRONMENT"
CMD ["dotnet", "$ENTRYPOINT"]

答案 1 :(得分:0)

在GitLab-Pipeline中进行标记时,有一些版本处理:

script:
    - COMMIT_DATE=$(git log -1 --format=%cd --date=iso-strict | grep -o '\([0-9]*\)' | tr -d '\n')
    - VERSION_PREFIX=$CI_COMMIT_TAG
    - VERSION_SUFFIX="${COMMIT_DATE::-6}"
    - echo $VERSION_PREFIX-$VERSION_SUFFIX
    - sed -i "s:<VersionPrefix>.*</VersionPrefix>:<VersionPrefix>$VERSION_PREFIX</VersionPrefix>:g" [PROJECT].csproj
    - dotnet publish --version-suffix $VERSION_SUFFIX -c Release -o ./out
    - docker build --tag "$CI_REGISTRY_IMAGE:$VERSION_PREFIX"
only:
    - tags     

在项目文件中必须

<!-- Version is set by CI-Script do not modify manually -->
<VersionPrefix>0.0.0</VersionPrefix>
<Deterministic>False</Deterministic>

被设置

也许这很有帮助。 无需标记就可以进行类似的操作。