Skip to content

ci: ci change

ci: ci change #10

Workflow file for this run

# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
# https://github.com/rhysd/actionlint
name: Publish-Nuget
on:
push:
tags:
- v\\d+\\.\\d+ # for publish package after each release to nuget
branches:
- main # for publish package and each commit to github
env:
FEED_SOURCE: https://api.nuget.org/v3/index.json
FEED_API_KEY: ${{ secrets.FEED_API_KEY }}
NuGetDirectory: ${{ github.workspace}}/nuget
jobs:
# https://www.meziantou.net/publishing-a-nuget-package-following-best-practices-using-github.htm
create-nuget:
if: ${{ contains(fromJson('["main"]'), github.ref_name) || github.event_name == 'release' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
# https://github.com/dotnet/Nerdbank.GitVersioning/blob/main/doc/cloudbuild.md#github-actions
fetch-depth: 0 # avoid shallow clone so nbgv can do its work.
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: "8.0.x"
- name: Cache NuGet Packages
uses: actions/cache@v3
with:
key: vertical-template-nuget
path: ~/.nuget/packages
# https://github.com/dotnet/Nerdbank.GitVersioning/blob/81162b84f1e81864d3c79ddfc4ce9794ae065574/doc/nbgv-cli.md
- name: Install nbgv dotnet tools
run: dotnet tool install --global nbgv
- name: nbgv Version
id: nbgv-version
run: |
version_info=$(nbgv get-version -f json)
echo "$version_info"
echo "Version=$(echo $version_info | grep -Po '(?<=\"Version\":\s\").*?(?=\")')" >> $GITHUB_OUTPUT
echo "AssemblyVersion=$(echo $version_info | grep -Po '(?<=\"AssemblyVersion\":\s\").*?(?=\")')" >> $GITHUB_OUTPUT
echo "NuGetPackageVersion=$(echo $version_info | grep -Po '(?<=\"NuGetPackageVersion\":\s\").*?(?=\")')" >> $GITHUB_OUTPUT
echo "SemVer2=$(echo $version_info | grep -Po '(?<=\"SemVer2\":\s\").*?(?=\")')" >> $GITHUB_OUTPUT
- name: Use Version Outputs
run: |
echo "BuildMetadataFragment: ${{ steps.nbgv-version.outputs.Version }}"
echo "NuGetPackageVersion: ${{ steps.nbgv-version.outputs.AssemblyVersion }}"
echo "ChocolateyPackageVersion: ${{ steps.nbgv-version.outputs.NuGetPackageVersion }}"
echo "NpmPackageVersion: ${{ steps.nbgv-version.outputs.SemVer2 }}"
- name: Restore dependencies
run: dotnet restore Vertical.Slice.Template.sln
- name: Build Version ${{ steps.nbgv-version.outputs.NuGetPackageVersion }}
run: dotnet build Vertical.Slice.Template.sln -c Release --no-restore
# https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-pack
- name: Pack NuGet Package Version ${{ steps.nbgv-version.outputs.NuGetPackageVersion }}
run: dotnet pack vertical-slice-template.csproj -c Release -o ${{ env.NuGetDirectory }}
# Publish the NuGet package as an artifact, so they can be used in the following jobs
- uses: actions/upload-artifact@v3
with:
name: nuget
if-no-files-found: error
retention-days: 7
path: ${{ env.NuGetDirectory }}/*.nupkg
deploy-nuget:
if: ${{ contains(fromJson('["main"]'), github.ref_name) || github.event_name == 'release' }}
# Publish only when creating a GitHub Release
# https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository
# You can update this logic if you want to manage releases differently
runs-on: ubuntu-latest
needs: [create-nuget]
steps:
- uses: actions/checkout@v3
with:
# https://github.com/dotnet/Nerdbank.GitVersioning/blob/main/doc/cloudbuild.md#github-actions
fetch-depth: 0 # avoid shallow clone so nbgv can do its work.
# Download the NuGet package created in the previous job
- uses: actions/download-artifact@v3
with:
name: nuget
path: ${{ env.NuGetDirectory }}
# Install the .NET SDK indicated in the global.json file
- name: Setup .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: "8.0.x"
# https://github.com/dotnet/Nerdbank.GitVersioning/blob/81162b84f1e81864d3c79ddfc4ce9794ae065574/doc/nbgv-cli.md
- name: Install nbgv dotnet tools
run: dotnet tool install -g nbgv
- name: nbgv Version
id: nbgv-version
run: |
$version = nbgv get-version -f json | ConvertFrom-Json
echo "version=$($version.NuGetPackageVersion)" >> "$GITHUB_OUTPUT"
# for publish package to github for each commit
- name: Publish NuGet Package Version ${{ steps.nbgv-version.outputs.version }} to GitHub
run: dotnet nuget push ${{ env.NuGetDirectory }}/*.nupkg --skip-duplicate
if: github.event_name == 'push' && startswith(github.ref, 'refs/heads')
# Publish all NuGet packages to NuGet.org
# Use --skip-duplicate to prevent errors if a package with the same version already exists.
# If you retry a failed workflow, already published packages will be skipped without error.
# https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-push
- name: Publish NuGet Package Version ${{ steps.nbgv-version.outputs.version }} to Nuget
run: dotnet nuget push ${{ env.NuGetDirectory }}/*.nupkg --skip-duplicate --source ${{ env.FEED_SOURCE }} --api-key ${{ env.FEED_API_KEY }}
if: github.event_name == 'push' && startswith(github.ref, 'refs/tags')