1.Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
EXPOSE 80
EXPOSE 443
COPY /app/publish .
ENTRYPOINT ["dotnet", "coreqi_api.dll"]
2.[.gitlab-ci.yml]
stages:
- build-project
- build-docker
- deploy
variables:
PROJECT_NAME: 'coreqi-api'
NUGET_PACKAGES_DIRECTORY: '.nuget'
#BUILD_DIRECTORY: 'app/build'
PUBLISH_DIRECTORY: 'app/publish'
TAG: fanqi/coreqi-api:latest
cache:
key: "dotnet-bin-cache"
paths:
- '$NUGET_PACKAGES_DIRECTORY'
#- '$BUILD_DIRECTORY'
- '$PUBLISH_DIRECTORY'
policy: pull-push
before_script:
- export IMAGE_FULL_NAME=fanqi/coreqi-api:latest
- export PROJECT_NAME=coreqi-api
build-project:
image: mcr.microsoft.com/dotnet/sdk:6.0
stage: build-project
cache:
key: "dotnet-bin-cache"
paths:
- '$NUGET_PACKAGES_DIRECTORY'
#- '$BUILD_DIRECTORY'
- '$PUBLISH_DIRECTORY'
policy: push
tags:
- docker
script:
- 'dotnet restore --packages $NUGET_PACKAGES_DIRECTORY'
#- 'dotnet build --no-restore -c Release -o $BUILD_DIRECTORY'
- 'dotnet publish -c Release -o $PUBLISH_DIRECTORY /p:UseAppHost=false'
- 'ls $PUBLISH_DIRECTORY'
- 'echo dotnet build success'
artifacts:
name: 'coreqi-api'
paths:
- $PUBLISH_DIRECTORY
expire_in: 1 week
build-docker:
image: docker:stable
stage: build-docker
cache:
key: "dotnet-bin-cache"
paths:
- '$PUBLISH_DIRECTORY'
policy: pull
tags:
- docker
script:
- 'docker build -t $IMAGE_FULL_NAME .'
only:
- main
# tests:
# stage: test
# script:
# - 'dotnet test --no-restore'
deploy:
image: docker:stable
stage: deploy
tags:
- docker
script:
- CONTAINER_NAME=$(docker ps -aq --filter name=$PROJECT_NAME)
- echo $CONTAINER_NAME
- if [[ -n "$CONTAINER_NAME" ]]; then
docker rm -f $CONTAINER_NAME;
fi
- docker run -d --name $PROJECT_NAME -p 10010:80 $IMAGE_FULL_NAME
only:
- main
标签:CI,Gitlab,NAME,Dotnet6,PUBLISH,dotnet,build,DIRECTORY,docker
From: https://www.cnblogs.com/fanqisoft/p/17369739.html