1、顶级目录下放置Directory.Build.props文件
为每个软件产品,分配一块独立的windows盘符,在根目录下放置名为Directory.Build.props的文件即可。这个文件名是特定的,必须叫这个。
如果公司有很多产品,引用的同一个包的版本可能是不同的,那么在磁盘根目录下就不要有Directory.Build.props文件,而是在产品的顶级目录里放置Directory.Build.props。
产品如果引用的包很多,就要合理使用PropertityGroup来分组。
包的版本号变量,当然需要有一个命名规则,我才用的是把包的名称的小数点用下划线替换,末尾加上“_Version”。
Directory.Build.props完整内容如下:
<Project> <PropertyGroup> <LangVersion>latest</LangVersion> <!--<Nullable>enable</Nullable>--> <NoWarn>$(NoWarn);NU1603;CS8618;CS8625;CS1998;CS8600;CS8629;CS8603;8601;8602;8603;8604</NoWarn> <!--<ImplicitUsings>enable</ImplicitUsings>--> <PackageProjectUrl>https://zimuceping.alida.group</PackageProjectUrl> <Product>易经改变人生</Product> <Authors>David Xiong</Authors> <PackageReleaseNotes>https://zimuceping.alida.group/ReleaseNotes.md</PackageReleaseNotes> <Copyright>Copyright 2022 (c) 阿力达</Copyright> </PropertyGroup> <PropertyGroup> <AbpVersion>7.2.2</AbpVersion> <LeptonX_Version>2.2.*-*</LeptonX_Version> <IdentityModel_Version>6.0.0</IdentityModel_Version> <IdentityModel_OidcClient_Version>5.1.0</IdentityModel_OidcClient_Version> <CommunityToolkit_Mvvm_Version>8.0.0</CommunityToolkit_Mvvm_Version> <Microsoft_Windows_CsWin32_Version>0.2.164-beta</Microsoft_Windows_CsWin32_Version> <System_IdentityModel_Tokens_Jwt_Version>6.24.0</System_IdentityModel_Tokens_Jwt_Version> <Blazorise_Version>1.2.0</Blazorise_Version> <ConfigureAwait_Fody_Version>3.3.1</ConfigureAwait_Fody_Version> <Fody_Version>6.5.3</Fody_Version> </PropertyGroup> <PropertyGroup> <Microsoft_AspNetCore_Authentication_OAuth_Version>2.2.0</Microsoft_AspNetCore_Authentication_OAuth_Version> <Microsoft_AspNetCore_Diagnostics_HealthChecks_Version>2.2.0</Microsoft_AspNetCore_Diagnostics_HealthChecks_Version> <Microsoft_AspNetCore_Hosting_Version>2.2.7</Microsoft_AspNetCore_Hosting_Version> <Microsoft_AspNetCore_Version>7.0.0</Microsoft_AspNetCore_Version> <Microsoft_AspNetCore_Rewrite_Version>2.2.0</Microsoft_AspNetCore_Rewrite_Version> <Microsoft_AspNetCore_Http_Version>2.2.2</Microsoft_AspNetCore_Http_Version> <Microsoft_AspNetCore_Http_Abstractions_Version>2.2.0</Microsoft_AspNetCore_Http_Abstractions_Version> <Microsoft_AspNetCore_Mvc_DataAnnotations_Version>2.2.0</Microsoft_AspNetCore_Mvc_DataAnnotations_Version> </PropertyGroup> </Project>
2、子目录中的解决方案的项目里如何使用版本号变量?
很简单,把每个包节点的Version里面的内容,换成美元符号开头,变量名称放在小括号里即可,如下面所示:
<Project Sdk="Microsoft.NET.Sdk.Razor"> <Import Project="..\..\common.props" /> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="BootstrapBlazor" Version="$(Bootstrap_Blazor_Version)"> <IncludeAssets>runtime; compile; build; native; analyzers; buildtransitive</IncludeAssets> <ExcludeAssets>contentFiles</ExcludeAssets> <PrivateAssets>contentfiles; analyzers</PrivateAssets> </PackageReference> </ItemGroup> <ItemGroup> <PackageReference Include="Yee.Change.Foundation.Blazor" Version="$(Change_Brain_Version)"> <IncludeAssets>runtime; compile; build; native; analyzers; buildtransitive</IncludeAssets> <ExcludeAssets>contentFiles</ExcludeAssets> <PrivateAssets>contentfiles; analyzers</PrivateAssets> </PackageReference> </ItemGroup> <ItemGroup> <PackageReference Include="Yee.Change.Divination.Blazor" Version="$(Change_Divination_Version)"> <IncludeAssets>runtime; compile; build; native; analyzers; buildtransitive</IncludeAssets> <ExcludeAssets>contentFiles</ExcludeAssets> <PrivateAssets>contentfiles; analyzers</PrivateAssets> </PackageReference> <PackageReference Include="Yee.Change.Rms.Blazor" Version="$(Change_Rms_Version)"> <IncludeAssets>runtime; compile; build; native; analyzers; buildtransitive</IncludeAssets> <ExcludeAssets>contentFiles</ExcludeAssets> <PrivateAssets>contentfiles; analyzers</PrivateAssets> </PackageReference> <PackageReference Include="Yee.Change.Common.Blazor" Version="$(Change_Common_Version)"> <IncludeAssets>runtime; compile; build; native; analyzers; buildtransitive</IncludeAssets> <ExcludeAssets>contentFiles</ExcludeAssets> <PrivateAssets>contentfiles; analyzers</PrivateAssets> </PackageReference> <PackageReference Include="Yee.Change.Calendar.Blazor" Version="$(Change_Calendar_Version)"> <IncludeAssets>runtime; compile; build; native; analyzers; buildtransitive</IncludeAssets> <ExcludeAssets>contentFiles</ExcludeAssets> <PrivateAssets>contentfiles; analyzers</PrivateAssets> </PackageReference> </ItemGroup> <ItemGroup> <PackageReference Include="Volo.Abp.AutoMapper" Version="$(AbpVersion)" /> <PackageReference Include="Volo.Abp.AspNetCore.Components.Web.Theming" Version="$(AbpVersion)" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\Yee.Change.Lianshan.RuleSet.Application.Contracts\Yee.Change.Lianshan.RuleSet.Application.Contracts.csproj" /> </ItemGroup> </Project>
3、软件产品模块很多的时候,各个模块的版本号建议放在独立的文件里。
我们用ABP框架,就是因为模块太多,很多很多。建议在产品根目录下放一个子文件夹,我这里叫做”version-numbers”。
比如我的产品有5个模块,那么我在子文件夹”version-numbers”下就会有5个文件,分别是
Directory.Build.Change.Account.props
<Project> <PropertyGroup> <Change_Account_Version>2022.51.138</Change_Account_Version> </PropertyGroup> </Project>
Directory.Build.Change.Pay.props
<Project> <PropertyGroup> <Change_Pay_Version>2022.11.6142</Change_Pay_Version> </PropertyGroup> </Project>
Directory.Build.Change.Shop.props
<Project> <PropertyGroup> <Change_Shop_Version>2022.51.262</Change_Shop_Version> </PropertyGroup> </Project>
Directory.Build.Change.Config.props
<Project> <PropertyGroup> <Change_Config_Version>2022.51.115</Change_Config_Version> </PropertyGroup> </Project>
Directory.Build.Change.WeChatMp.props
<Project> <PropertyGroup> <Change_WeChatMp_Version>2022.51.288</Change_WeChatMp_Version> </PropertyGroup> </Project>
在产品根目录的Directory.Build.props文件中,添加对上述5个文件的引用
<Project> <Import Project="version-numbers/Directory.Build.Change.Config.props" /> <Import Project="version-numbers/Directory.Build.Change.Account.props" /> <Import Project="version-numbers/Directory.Build.Change.Shop.props" /> <Import Project="version-numbers/Directory.Build.Change.WeChatMp.props" /> <Import Project="version-numbers/Directory.Build.Change.Pay.props" /> <PropertyGroup> <LangVersion>latest</LangVersion> <!--<Nullable>enable</Nullable>--> <NoWarn>$(NoWarn);NU1603;CS8618;CS8625;CS1998;CS8600;CS8629;CS8603;8601;8602;8603;8604</NoWarn> <!--<ImplicitUsings>enable</ImplicitUsings>--> <PackageProjectUrl>https://zimuceping.alida.group</PackageProjectUrl> <Product>易经改变人生</Product> <Authors>David Xiong</Authors> <PackageReleaseNotes>https://zimuceping.alida.group/ReleaseNotes.md</PackageReleaseNotes> <Copyright>Copyright 2022 (c) 阿力达</Copyright> </PropertyGroup> <PropertyGroup> <AbpVersion>7.2.2</AbpVersion> <LeptonX_Version>2.2.*-*</LeptonX_Version> <IdentityModel_Version>6.0.0</IdentityModel_Version> <IdentityModel_OidcClient_Version>5.1.0</IdentityModel_OidcClient_Version> <CommunityToolkit_Mvvm_Version>8.0.0</CommunityToolkit_Mvvm_Version> <Microsoft_Windows_CsWin32_Version>0.2.164-beta</Microsoft_Windows_CsWin32_Version> <System_IdentityModel_Tokens_Jwt_Version>6.24.0</System_IdentityModel_Tokens_Jwt_Version> <Blazorise_Version>1.2.0</Blazorise_Version> <ConfigureAwait_Fody_Version>3.3.1</ConfigureAwait_Fody_Version> <Fody_Version>6.5.3</Fody_Version> </PropertyGroup> <PropertyGroup> <Microsoft_AspNetCore_Authentication_OAuth_Version>2.2.0</Microsoft_AspNetCore_Authentication_OAuth_Version> <Microsoft_AspNetCore_Diagnostics_HealthChecks_Version>2.2.0</Microsoft_AspNetCore_Diagnostics_HealthChecks_Version> <Microsoft_AspNetCore_Hosting_Version>2.2.7</Microsoft_AspNetCore_Hosting_Version> <Microsoft_AspNetCore_Version>7.0.0</Microsoft_AspNetCore_Version> <Microsoft_AspNetCore_Rewrite_Version>2.2.0</Microsoft_AspNetCore_Rewrite_Version> <Microsoft_AspNetCore_Http_Version>2.2.2</Microsoft_AspNetCore_Http_Version> <Microsoft_AspNetCore_Http_Abstractions_Version>2.2.0</Microsoft_AspNetCore_Http_Abstractions_Version> <Microsoft_AspNetCore_Mvc_DataAnnotations_Version>2.2.0</Microsoft_AspNetCore_Mvc_DataAnnotations_Version> </PropertyGroup> <Project>
就是这么简单。
4、模块自动集成的时候,要更新上述版本号文件。
在.gitlab-ci.yml中增加一个场景,一般是打包、单元测试后,再更新版本号
更新版本号,我这里采用的是SSH自动登入服务器,去更新服务器目录下的对应文件的对应XML字段即可。
更新版本号: # This job runs in the test stage. stage: update-version # It only starts when the job in the build stage completes successfully. before_script: - mkdir -p ~/.ssh - chmod 700 ~/.ssh #- ssh-keyscan $APP_SERVER > ~/.ssh/known_hosts #- chmod 644 ~/.ssh/known_hosts - cd /root/.ssh - cp /home/gitlab-runner/ssh/* . script: - echo "更新版本号……" - cd /home/gitlab-runner/scripts #- ssh david@$APP_SERVER #- docker ps - bash update-version.sh "Directory.Build.Change.Pay.props" "Change_Pay_Version" $BRAIN_VERSION_ID
代码签入后,Gitlab Runner运行的控制台日志如下:
5、开发团队的成员,用脚本拉取服务器上的版本号定义的文件
我们是上面第4步Gitlab Runner每次自动打包(集成)完成之后,会更新上述文件。
然后我们用Nginx,把上述文件用Web方式提供下载
下边的脚本,放在version-numbers目录里,文件名我是叫做download.bat,内容是:
@echo off del Directory.* /q for /f %%i in (urls.txt) do (echo %%i wget %%i --content-disposition ) dotnet nuget locals plugins-cache --clear exit
上述脚本里面要引用urls.txt,文件里放置的是要下载的文件列表,内容如下
http://dev.abcdefg.cn/Directory.Build.Change.Account.props http://dev.abcdefg.cn/Directory.Build.Change.Config.props http://dev.abcdefg.cn/Directory.Build.Change.Shop.props http://dev.abcdefg.cn/Directory.Build.Change.Pay.props http://dev.abcdefg.cn/Directory.Build.Change.WeChatMp.props
因为上述windows批处理命令,调用了wget命令,所以要去wget官网下载相应的dll
version-numbers下的完整内容,截图如下:
团队约定一个规则,每天早上上班的时候,或者收到通知的时候,双击一下download.bat文件即可。
6、Dockerfile里面,也多了这么一个下载的过程
要分别下载放在根目录
依次下载其它版本号定义文件,放在version-numbers子目录
Dockerfile完整示例如下
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. FROM docker.abcdefg.cn/dotnet/aspnet:7.0 AS base WORKDIR /app EXPOSE 80 FROM docker.abcdefg.cn/dotnet/sdk:7.0 AS build WORKDIR /version-numbers RUN wget http://dev.abcdefg.cn/Directory.Build.Change.Account.props RUN wget http://dev.abcdefg.cn/Directory.Build.Change.Config.props RUN wget http://dev.abcdefg.cn/Directory.Build.Change.Shop.props RUN wget http://dev.abcdefg.cn/Directory.Build.Change.Pay.props RUN wget http://dev.abcdefg.cn/Directory.Build.Change.WeChatMp.props WORKDIR / RUN wget http://dev.abcdefg.cn/Directory.Build.props --content-disposition WORKDIR /src COPY ["NuGet.Config", "."] COPY ["src/Yee.Change.BlazorOne.Blazor/Yee.Change.BlazorOne.Blazor.csproj", "src/Yee.Change.BlazorOne.Blazor/"] COPY ["src/Yee.Change.BlazorOne.Application/Yee.Change.BlazorOne.Application.csproj", "src/Yee.Change.BlazorOne.Application/"] COPY ["src/Yee.Change.BlazorOne.Domain/Yee.Change.BlazorOne.Domain.csproj", "src/Yee.Change.BlazorOne.Domain/"] COPY ["src/Yee.Change.BlazorOne.Domain.Shared/Yee.Change.BlazorOne.Domain.Shared.csproj", "src/Yee.Change.BlazorOne.Domain.Shared/"] COPY ["src/Yee.Change.BlazorOne.Application.Contracts/Yee.Change.BlazorOne.Application.Contracts.csproj", "src/Yee.Change.BlazorOne.Application.Contracts/"] COPY ["src/Yee.Change.BlazorOne.HttpApi/Yee.Change.BlazorOne.HttpApi.csproj", "src/Yee.Change.BlazorOne.HttpApi/"] COPY ["src/Yee.Change.BlazorOne.EntityFrameworkCore/Yee.Change.BlazorOne.EntityFrameworkCore.csproj", "src/Yee.Change.BlazorOne.EntityFrameworkCore/"] RUN dotnet nuget locals plugins-cache --clear RUN dotnet restore "src/Yee.Change.BlazorOne.Blazor/Yee.Change.BlazorOne.Blazor.csproj" COPY . . WORKDIR "/src/src/Yee.Change.BlazorOne.Blazor" RUN dotnet build "Yee.Change.BlazorOne.Blazor.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "Yee.Change.BlazorOne.Blazor.csproj" -c Release -o /app/publish /p:UseAppHost=false FROM base AS final WORKDIR /app COPY --from=publish /app/publish . #修改时区为东八区 RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime RUN echo 'Asia/Shanghai' >/etc/timezone ENTRYPOINT ["dotnet", "Yee.Change.BlazorOne.Blazor.dll"]
就是这么简单。由于指定了精确的版本号,Visual Studio还原NuGet包的效率,比用星号要高多了。然后配合公司的规则,就能实现对项目/产品使用的包的版本的精确控制。
标签:版本号,Yee,BlazorOne,Build,props,Directory,Change From: https://www.cnblogs.com/amisoft/p/17436443.html