.net 在Github Actions中获取软件包的版本号

8e2ybdfx  于 2023-01-03  发布在  .NET
关注(0)|答案(2)|浏览(277)

我正在尝试设置我的"第一个" GitHub操作,将. NET库的NuGet包发布到我的私人GitHub Packages注册表中。
我希望我的操作从.csproj文件中获取软件包的版本号。我尝试按照这里的说明操作,但看起来他们硬编码了版本号:https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-nuget-registry

如何从.csproj文件中获取版本号?
下面是我的release.yml文件:

name: Publish MyApp NuGet to GitHub Packages

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: 6.0.x
    - name: Restore dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --no-restore
      
  package:
  
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
    - name: Create NuGet package
      run: dotnet pack --configuration Release
  
  publish:
  
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
    - name: Publish to GitHub Packages
      run: dotnet nuget push "bin/Release/MyApp.1.0.0.nupkg" --source "github"

下面是nuget.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <packageSources>
        <clear />
        <add key="github" value="https://nuget.pkg.github.com/MY_GITHUB_COMPANY_ACCOUNT/index.json" />
    </packageSources>
    <packageSourceCredentials>
        <github>
            <add key="Username" value="MY_GITHUB_USERNAME" />
            <add key="ClearTextPassword" value="MY_GITHUB_PERSONAL_ACCESS_TOKEN" />
        </github>
    </packageSourceCredentials>
</configuration>

这里是.csproj文件中我定义包相关信息的部分:

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
    <Authors>MyCompany, LLC</Authors>
    <Company>MyCompany, LLC</Company>
    <Description>MyApp Library</Description>
    <Version>1.2.1</Version>
    <RepositoryUrl>https://github.com/MY_GITHUB_COMPANY_ACCOUNT/my-app</RepositoryUrl>
    <Copyright>MyCompany, LLC (c) 2015 - 2023</Copyright>
</PropertyGroup>
qybjjes1

qybjjes11#

尝试使用Get CSProj Version GH操作。此操作应从. NET .csproj项目文件中检索当前版本,并提供可在后续步骤中使用的输出。

    • 示例:**
...

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Get version
        id: package_version
        uses: KageKirin/get-csproj-version@v1.0.0
        with:
          file: src/project.csproj # Specify your .csproj file path

      - name: Publish to GitHub Packages
        run: dotnet nuget push "bin/Release/MyApp.${{ steps.test.package_version.version }}.nupkg" --source "github"
sczxawaw

sczxawaw2#

有一个更好的方法来获得版本号,所以你不必考虑手动设置,你可以使用GitVersion

- name: Install GitVersion
  uses: gittools/actions/gitversion/setup@v0.9.7
  with:
    versionSpec: '5.x'

- name: Determine Version
  uses: gittools/actions/gitversion/execute@v0.9.7
  with:
    useConfigFile: true

这将设置env var $GITVERSION_SEMVER,然后在推送步骤中,您只需输入:

- name: Your project
  run: |
    dotnet pack -p:Version=$GITVERSION_SEMVER projectToPack.csproj
    dotnet push ...

还有一件事,如果你在.csproj中手动指定version,pack/push会考虑这个数字,所以不需要-p:Version

相关问题