.NET DevOps 接入指南 | 5. 创建流水线测试项目

本文介绍了如何在GitLab上使用GitLabCI/CD流程创建一个新的Asp.NETCore项目,包括初始化项目结构、创建解决方案、添加测试用例以及提交和推送代码到develop分支。
摘要由CSDN通过智能技术生成

创建项目

从demos群组,点击新建项目,会有三个选项:创建空白项目、从模板创建或导入项目。这里直接点击创建空白项目:

2b3f54c67ca0f6a3adcf9cbd59044001.png这里创建了一个名为AspNetCore.CiCd.Demo的项目,用于后续探索GitLab CI/CD。创建成功后,

紧接着点击+``新建文件,按下图步骤创建.gitignroe文件。

57784fe4755b30380731c4c5c64a1073.png
image.png

创建成功后,点击+新建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分支,如下图所示。

3b0973942172fb08b7973f88141f5fe7.png
image.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值