- .NET DevOps 接入指南 | 创建流水线测试项目
- Ruoyi DevOps 流水线方式搭建(KubeSphere)
- .NET DevOps 接入指南 | GitLab 流水线简介
- .NET DevOps 接入指南 | 创建第一条流水线
- .NET DevOps 接入指南 | 使用GitLab流水线构建
- .NET DevOps 接入指南 | 使用GitLab流水线模板
- .NET DevOps 接入指南 | 创建第一个GitLab项目
- .NET DevOps 接入指南 | GitLab 安装
- .NET DevOps 接入指南 | 打通 Ingress
- .NET DevOps 接入指南 | 发布到Kubernetes
这里创建了一个名为AspNetCore.CiCd.Demo
的项目,用于后续探索GitLab CI/CD。创建成功后,
紧接着点击+``新建文件
,按下图步骤创建.gitignroe
文件。
创建成功后,点击+
新建develop
分支,然后点击克隆
复制项目地址,将项目克隆到本地,并签出develop
分支:
PS C:\Users\shengjie> git clone https://gitlab.shengjie.dev/demos/aspnetcore.cicd.demo.git D:\Gitlab\Demos\AspNetCore.CiCd.Demo
Cloning into 'D:\Gitlab\Demos\AspNetCore.CiCd.Demo'...
PS C:\Users\shengjie> cd D:\Gitlab\Demos\AspNetCore.CiCd.Demo\
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> git flow init #git flow 初始化
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> git flow config # 查看当前git flow 配置
Branch name for production releases: main
Branch name for "next release" development: develop
Feature branch prefix: feature/
Bugfix branch prefix: bugfix/
Release branch prefix: release/
Hotfix branch prefix: hotfix/
Support branch prefix: support/
Version tag prefix
```
接下来通过创建启动一个feature来初始化解决方案,该解决方案包含一个AspNetCore的Web项目和XUnit项目,命令行如下:
```
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> git flow feature start InitialSolution # 启动新Feature
Switched to a new branch 'feature/InitialSolution'
Summary of actions:
- A new branch 'feature/InitialSolution' was created, based on 'develop'
- You are now on branch 'feature/InitialSolution'
Now, start committing on your feature. When done, use:
git flow feature finish InitialSolution
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> dotnet new sln -n AspNetCore.CiCd.Demo #创建解决方案
The template "Solution File" was created successfully.
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> dotnet new web -n AspNetCore.CiCd.Web #创建Asp.Net Core Web项目
The template "ASP.NET Core Empty" was created successfully.
...
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> dotnet sln add .\AspNetCore.CiCd.Web\ #添加至解决方案
Project `AspNetCore.CiCd.Web\AspNetCore.CiCd.Web.csproj` added to the solution.
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> dotnet new xunit -n AspNetCore.CiCd.Web.Tests #创建XUnit 项目
The template "xUnit Test Project" was created successfully.
...
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> dotnet sln add .\AspNetCore.CiCd.Web.Tests\ #添加至解决方案
Project `AspNetCore.CiCd.Web.Tests\AspNetCore.CiCd.Web.Tests.csproj` added to the solution.
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> ls
Directory: D:\Gitlab\Demos\AspNetCore.CiCd.Demo
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 10/16/2021 8:23 PM AspNetCore.CiCd.Web
d----- 10/16/2021 8:24 PM AspNetCore.CiCd.Web.Tests
-a---- 10/16/2021 11:20 AM 6132 .gitignore
-a---- 10/16/2021 8:28 PM 2935 AspNetCore.CiCd.Demo.sln
-a---- 10/16/2021 11:04 AM 72 README.md
然后点击AspNetCore.CiCd.Demo.sln
在Visual Studio打开解决方案。然后在Web项目中创建一个素数判断静态类:PrimeCheckService
类,代码如下:
using System;
namespace AspNetCore.CiCd.Web
{
public static class PrimeCheckService
{
/// <summary>
/// 素数:大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数。
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static bool IsPrimeNumber(int num)
{
throw new NotImplementedException("Not implemented yet!");
}
}
}
然后AspNetCore.CiCd.Web.Tests
项目添加对AspNetCore.CiCd.Web
项目的引用。同时修改UnitTest1.cs
,如下所示:
using Xunit;
namespace AspNetCore.CiCd.Web.Tests
{
public class UnitTest1
{
[Fact]
public void IsPrime_InputIs1_ShouldReturnFalse()
{
var isPrime = PrimeCheckService.IsPrimeNumber(1);
Assert.False(isPrime,"1 不是素数!");
}
}
}
由于PrimeCheckService
并未给出具体实现,此时若执行单元测试,必定失败。这个问题暂搁置在此,后续在持续构建阶段来修复该问题。完成项目初始化后,关闭feature,并将本地创建项目提交并推送至gitlab,命令如下:
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> git commit -m 'create aspnetcore cicd demo solution'
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> git flow feature finish
//...
Summary of actions:
- The feature branch 'feature/InitialSolution' was merged into 'develop'
- Feature branch 'feature/InitialSolution' has been locally deleted
- You are now on branch 'develop'
PS D:\Gitlab\Demos\AspNetCore.CiCd.Demo> git push
推送成功后,打开GitLab中刚创建的项目页面,切换至develop
分支,如下图所示。
网友评论