dotnet语义版本控制和使用github动作发布

发布于 2025-02-08 16:36:12 字数 1826 浏览 1 评论 0原文

我有一个dotnet项目,并且正在尝试构建CI/CD管道,该管道使用GitHub操作进行以下操作:

  • 在PR或主更改上构建项目。
  • 测试项目对PR或主更改进行测试。
  • 在主更改上,可以计算一个SEMVER(理想情况下,也是一个变形程序,但这并不重要)。
  • 在主更改上,使用此SEMVER,构建Nuget软件包,然后将其发布到GitHub和Nuget。

到目前为止,我拥有建立和测试项目的工作流程。这似乎很好。但是,它也有一个发布步骤,似乎从未运行过(我想如果是错误的(?)),我真的不确定从semver的semver部分从哪里开始想。

name: .NET Build, Test and Publish

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

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      DISCOS_API_KEY: ${{ secrets.DISCOS_API_KEY }}
      DISCOS_API_URL: http://localhost:3000/api/
    steps:
    - uses: actions/checkout@v2
      with: 
        submodules: recursive
    - name: Build docker stack
      run: docker-compose -f src/DISCOSweb-Sdk/DISCOSweb-Sdk.Tests/docker-compose.yml up -d --force-recreate --build
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 6.0.x
    - name: Restore dependencies
      run: dotnet restore src/DISCOSweb-Sdk/DISCOSweb-Sdk.sln
    - name: Build
      run: dotnet build --no-restore src/DISCOSweb-Sdk/DISCOSweb-Sdk.sln
    - name: Test
      run: dotnet test --no-build --verbosity normal src/DISCOSweb-Sdk/DISCOSweb-Sdk.sln
  publish: # This stage is never run
    if: github.event.pull_request.merged == 'true'
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Publish
        id: publish_nuget
        uses: brandedoutcast/publish-nuget@v2
        with:
          PACKAGE_NAME: DISCOSweb-Sdk
          PROJECT_FILE_PATH: "**/DISCOSweb-Sdk.csproj"
          BUILD_CONFIGURATION: Release
          NUGET_KEY: ${{secrets.NUGET_KEY}}
          VERSION_STATIC: 1.0.0 # Replace with something better

我觉得这应该是相对普遍且直接的事情,因此,任何帮助都将不胜感激。

I have a dotnet project and am trying to build a CI/CD pipeline that does the following using Github Actions:

  • Builds the project on a PR or master change.
  • Tests the project on a PR or master change.
  • On a master change calculates a semver (ideally a changelog too but that's less important).
  • On a master change uses this semver, builds a nuget package, and publishes it to GitHub and Nuget.

So far, I have this workflow that builds and tests my project. This seems to work nicely. However, it also has a publish step, which never seems to be run (I guess the if is wrong(?)) and I'm really not sure where to start with the SemVer part of what I want.

name: .NET Build, Test and Publish

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

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      DISCOS_API_KEY: ${{ secrets.DISCOS_API_KEY }}
      DISCOS_API_URL: http://localhost:3000/api/
    steps:
    - uses: actions/checkout@v2
      with: 
        submodules: recursive
    - name: Build docker stack
      run: docker-compose -f src/DISCOSweb-Sdk/DISCOSweb-Sdk.Tests/docker-compose.yml up -d --force-recreate --build
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 6.0.x
    - name: Restore dependencies
      run: dotnet restore src/DISCOSweb-Sdk/DISCOSweb-Sdk.sln
    - name: Build
      run: dotnet build --no-restore src/DISCOSweb-Sdk/DISCOSweb-Sdk.sln
    - name: Test
      run: dotnet test --no-build --verbosity normal src/DISCOSweb-Sdk/DISCOSweb-Sdk.sln
  publish: # This stage is never run
    if: github.event.pull_request.merged == 'true'
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Publish
        id: publish_nuget
        uses: brandedoutcast/publish-nuget@v2
        with:
          PACKAGE_NAME: DISCOSweb-Sdk
          PROJECT_FILE_PATH: "**/DISCOSweb-Sdk.csproj"
          BUILD_CONFIGURATION: Release
          NUGET_KEY: ${{secrets.NUGET_KEY}}
          VERSION_STATIC: 1.0.0 # Replace with something better

I feel like this should be a relatively common and straightforward thing to do so any assistance would be really appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请恋爱 2025-02-15 16:36:12

我设法通过分配CI和CD工作流程并利用一些非常有用的软件包来实现这一目标。


CI工作流程

第一个工作流程在PR上运行,并推向掌握。值得注意的是,当分支保护开启时,推动掌握是合并PR的代名词。

name: .NET Continuous Integration

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

jobs:
  test:
    name: Test Project (Mock API)
    runs-on: ubuntu-latest
    env:
      DISCOS_API_KEY: ${{ secrets.DISCOS_API_KEY }}
      DISCOS_API_URL: http://localhost:3000/api/
    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: 6.0.x
    - name: Build docker stack
      run: docker-compose -f ./src/DiscosWebSdk/DiscosWebSdk.Tests/docker-compose.yml up -d --force-recreate --build
    - name: Run tests against mock API
      run: dotnet test --logger GitHubActions ./src/DiscosWebSdk/DiscosWebSdk.sln

CD工作流

CD工作流还有更多。它运行到主机上,但仅当对包含SDK的实际源代码的文件夹进行更改(这就是paths Block所做的)。

然后,我使用mathieudutour/ >用计算的语义版本(使用常规提交计算)来标记提交。

然后,使用-p:packageversion = $ {{steps.tag_version.outputs.new_version}}选项dotnet nuget pack在此版本上标记软件包构建。

ncipollo/Release-Action@v1还在GitHub上创建了一个版本。

最后,像往常一样,使用dotnet nuget push将发行版构建并将其推送到github和nuget。

name: .NET Continuous Deployment

on:
  push:
    branches: [ master ]
    paths: 
      - src/DiscosWebSdk/DiscosWebSdk/**
  workflow_dispatch:
jobs:

  test:
    name: Test Project (Real API)
    runs-on: ubuntu-latest
    env:
      DISCOS_API_KEY: ${{ secrets.DISCOS_API_KEY }}
      DISCOS_API_URL: https://discosweb.esoc.esa.int/api/
    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: 6.0.x
    - name: Test against real API
      run: dotnet test --logger GitHubActions ./src/DiscosWebSdk/DiscosWebSdk.sln

  semantic-release:
    needs: test
    name: Create a Package Release
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3 # Need the full commit history for conventional commit
    - name: Setup .NET
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: 6.0.x
    - name: Bump version and push tag
      id: tag_version
      uses: mathieudutour/[email protected]
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
    - name: Create a GitHub release
      uses: ncipollo/release-action@v1
      with:
        tag: ${{ steps.tag_version.outputs.new_tag }}
        name: Release ${{ steps.tag_version.outputs.new_tag }}
        body: ${{ steps.tag_version.outputs.changelog }}
    - name: Create Nuget Package
      run: dotnet build -c Release ./src/DiscosWebSdk/DiscosWebSdk/DiscosWebSdk.csproj && dotnet pack -c Release -p:PackageVersion=${{ steps.tag_version.outputs.new_version }} -o . ./src/DiscosWebSdk/DiscosWebSdk/DiscosWebSdk.csproj
    - name: Upload Package for Publishing
      uses: actions/upload-artifact@v3
      with:
        name: PackedLib
        path: ./*.nupkg


  github-publish:
    needs: semantic-release
    name: Publish to Github
    runs-on: ubuntu-latest
    steps:
    - name: Download built project  
      uses: actions/download-artifact@v3
      with:
        name: PackedLib
    - name: Setup .NET
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: 6.0.x
    - name: Push Package to GitHub
      run: dotnet nuget push --api-key ${{secrets.GITHUB_TOKEN}} --source "https://nuget.pkg.github.com/hughesjs/index.json" *.nupkg


  nuget-publish:
    needs: semantic-release
    name: Publish to Nuget
    runs-on: ubuntu-latest
    steps:
    - name: Download built project  
      uses: actions/download-artifact@v3
      with:
        name: PackedLib
    - name: Setup .NET
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: 6.0.x
    - name: Push Package to Nuget
      run: dotnet nuget push --api-key ${{secrets.NUGET_KEY}} --source "https://api.nuget.org/v3/index.json" *.nupkg

I managed to accomplish this by splitting my CI and CD workflows, and making use of some very helpful packages.


CI Workflow

This first workflow runs on PRs and pushes to master. It's worth noting that when branch protection is on, a push to master is synonymous with a merged PR.

name: .NET Continuous Integration

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

jobs:
  test:
    name: Test Project (Mock API)
    runs-on: ubuntu-latest
    env:
      DISCOS_API_KEY: ${{ secrets.DISCOS_API_KEY }}
      DISCOS_API_URL: http://localhost:3000/api/
    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: 6.0.x
    - name: Build docker stack
      run: docker-compose -f ./src/DiscosWebSdk/DiscosWebSdk.Tests/docker-compose.yml up -d --force-recreate --build
    - name: Run tests against mock API
      run: dotnet test --logger GitHubActions ./src/DiscosWebSdk/DiscosWebSdk.sln

CD Workflow

There's a bit more to the CD workflow. It runs on pushes to master but only when there are changes made to the folder containing the actual source code for the SDK (that's what the paths block does).

I then used the mathieudutour/[email protected] action to tag the commit with a calculated semantic version (calculated using conventional commits).

The package build is then tagged with this version using the -p:PackageVersion=${{ steps.tag_version.outputs.new_version }} option on dotnet nuget pack.

The ncipollo/release-action@v1 also creates a release on Github.

Finally, building and pushing the release to Github and Nuget is done using dotnet nuget push as usual.

name: .NET Continuous Deployment

on:
  push:
    branches: [ master ]
    paths: 
      - src/DiscosWebSdk/DiscosWebSdk/**
  workflow_dispatch:
jobs:

  test:
    name: Test Project (Real API)
    runs-on: ubuntu-latest
    env:
      DISCOS_API_KEY: ${{ secrets.DISCOS_API_KEY }}
      DISCOS_API_URL: https://discosweb.esoc.esa.int/api/
    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: 6.0.x
    - name: Test against real API
      run: dotnet test --logger GitHubActions ./src/DiscosWebSdk/DiscosWebSdk.sln

  semantic-release:
    needs: test
    name: Create a Package Release
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3 # Need the full commit history for conventional commit
    - name: Setup .NET
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: 6.0.x
    - name: Bump version and push tag
      id: tag_version
      uses: mathieudutour/[email protected]
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
    - name: Create a GitHub release
      uses: ncipollo/release-action@v1
      with:
        tag: ${{ steps.tag_version.outputs.new_tag }}
        name: Release ${{ steps.tag_version.outputs.new_tag }}
        body: ${{ steps.tag_version.outputs.changelog }}
    - name: Create Nuget Package
      run: dotnet build -c Release ./src/DiscosWebSdk/DiscosWebSdk/DiscosWebSdk.csproj && dotnet pack -c Release -p:PackageVersion=${{ steps.tag_version.outputs.new_version }} -o . ./src/DiscosWebSdk/DiscosWebSdk/DiscosWebSdk.csproj
    - name: Upload Package for Publishing
      uses: actions/upload-artifact@v3
      with:
        name: PackedLib
        path: ./*.nupkg


  github-publish:
    needs: semantic-release
    name: Publish to Github
    runs-on: ubuntu-latest
    steps:
    - name: Download built project  
      uses: actions/download-artifact@v3
      with:
        name: PackedLib
    - name: Setup .NET
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: 6.0.x
    - name: Push Package to GitHub
      run: dotnet nuget push --api-key ${{secrets.GITHUB_TOKEN}} --source "https://nuget.pkg.github.com/hughesjs/index.json" *.nupkg


  nuget-publish:
    needs: semantic-release
    name: Publish to Nuget
    runs-on: ubuntu-latest
    steps:
    - name: Download built project  
      uses: actions/download-artifact@v3
      with:
        name: PackedLib
    - name: Setup .NET
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: 6.0.x
    - name: Push Package to Nuget
      run: dotnet nuget push --api-key ${{secrets.NUGET_KEY}} --source "https://api.nuget.org/v3/index.json" *.nupkg
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文