目錄
1.開發工具
2.GitLab服務器搭建
3.新建webapi
4.Dockerfile配置
5.配置docker-compose.yml
6.配置.gitlab-ci.yml
7.在GitLab上添加一個新項目
8.GitLib Runner安裝
9.提交代碼到gitlab
10.在GitLab上查看運行狀態
11.本地訪問
> ### 1.開發工具 + VSCode + 插件:C#、Docker
2.GitLab服務器搭建
3.新建webapi
-
dotnet new webapi --name Demo
-
在Program中修改啟動地址:.UseUrls("http://*:80")
4.Dockerfile配置
-
在VSCode中打開命令面板:Ctrl+Shift+P
-
輸入:ADD Docker Files to Workspace
- 選擇 ASP.Net Core
- 選擇 Linux
- 80
-
默認配置可能有問題,修改Dockerfile文件,配置如下
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 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 mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/out .
EXPOSE 80
ENTRYPOINT ["dotnet", "Demo.dll"]
- 確保文件在項目根目錄下
5.配置docker-compose.yml
-
在項目根目錄下新建docker-compose.yml文件
-
添加配置如下,格式非常重要
version: '3'
services:
web:
build: .
container_name: aspnetcore
ports:
- '8080:80'
6.配置.gitlab-ci.yml
-
在項目根目錄下新建.gitlab-ci.yml文件
-
添加配置如下
rtest:
script:
- docker-compose up -d --build --force-recreate
7.在GitLab上添加一個新項目
8.GitLib Runner安裝
-
環境:win10
-
注冊一個runner
- 使用Powershell運行:./gitlab-runner.exe register
- 輸入gitlab地址:http://127.0.0.1
- 輸入令牌:在gitlab的項目=>設置=>CI/CD=>Runner中 找到注冊令牌
- 輸出描述:test
- 輸入tags:beta
- 輸入excutor方式:shell
-
安裝:gitlab-runner.exe install
-
啟動:gitlab-runner.exe start
9.提交代碼到gitlab
-
每次提交會觸發gitlab runner,實現自動化部署
-
git命令如下
git init
git remote add origin ssh://地址
git add .
git commit -m "Initial commit"
git push -u origin master
10.在GitLab上查看運行狀態
11.本地訪問:http://localhost:8080/api/values