Move to GitHub Actions (#181) #18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI/CD | |
on: | |
push: | |
branches: [ master ] | |
pull_request: | |
branches: [ master ] | |
release: | |
types: [ published ] | |
env: | |
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1 | |
DOTNET_NOLOGO: true | |
NuGetDirectory: ${{ github.workspace}}/nuget | |
defaults: | |
run: | |
shell: pwsh | |
jobs: | |
create_nuget: | |
runs-on: ubuntu-latest | |
name: Create NuGet packages | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 # Get all history to allow automatic versioning using MinVer | |
- name: Install .NET | |
uses: actions/setup-dotnet@v4 | |
- name: Cache NuGet packages | |
uses: actions/cache@v4 | |
with: | |
path: ~/.nuget/packages | |
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-nuget- | |
- name: Restore dependencies | |
run: dotnet restore | |
- name: Create NuGet package | |
run: dotnet msbuild -t:Pack src/UTF-unknown.csproj -p:Configuration=Release -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg -p:ContinuousIntegrationBuild=true -p:EmbedUntrackedSources=true -p:PackageOutputPath=${{ env.NuGetDirectory }} -verbosity:minimal -p:FileVersion=2.1.${{ github.run_number }} | |
- name: Upload packages to artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: nuget | |
if-no-files-found: error | |
retention-days: 7 | |
path: ${{ env.NuGetDirectory }}/* | |
validate_nuget: | |
runs-on: ubuntu-latest | |
needs: [ create_nuget ] | |
steps: | |
- name: Install .NET | |
uses: actions/setup-dotnet@v4 | |
- name: Download NuGet package | |
uses: actions/download-artifact@v4 | |
with: | |
name: nuget | |
path: ${{ env.NuGetDirectory }} | |
- name: Install nuget validator | |
run: dotnet tool update Meziantou.Framework.NuGetPackageValidation.Tool --global | |
# Validate metadata and content of the NuGet package | |
# https://www.nuget.org/packages/Meziantou.Framework.NuGetPackageValidation.Tool#readme-body-tab | |
- name: Validate package | |
run: meziantou.validate-nuget-package (Get-ChildItem "${{ env.NuGetDirectory }}/*.nupkg") | |
run_test: | |
runs-on: ${{ matrix.os }} | |
strategy: | |
matrix: | |
os: [ ubuntu-latest, windows-latest, macos-latest ] | |
include: | |
# Only test on all frameworks on Windows (closest to original AppVeyor setup) | |
- os: windows-latest | |
test-framework: '' | |
# Test only .NET 8.0 on Linux and macOS for efficiency | |
- os: ubuntu-latest | |
test-framework: '--framework net8.0' | |
- os: macos-latest | |
test-framework: '--framework net8.0' | |
name: Run tests on ${{ matrix.os }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Install .NET | |
uses: actions/setup-dotnet@v4 | |
- name: Cache NuGet packages | |
uses: actions/cache@v4 | |
with: | |
path: ~/.nuget/packages | |
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-nuget- | |
- name: Restore dependencies | |
run: dotnet restore | |
- name: Run tests | |
run: dotnet test --configuration Release --no-restore --logger trx --results-directory "TestResults-${{ matrix.os }}" ${{ matrix.test-framework }} | |
- name: Upload test results | |
uses: actions/upload-artifact@v4 | |
if: always() | |
with: | |
name: test-results-${{ matrix.os }} | |
path: TestResults-${{ matrix.os }}/*.trx | |
deploy: | |
# 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 | |
if: github.event_name == 'release' | |
runs-on: ubuntu-latest | |
needs: [ validate_nuget, run_test ] | |
steps: | |
- name: Download NuGet package | |
uses: actions/download-artifact@v4 | |
with: | |
name: nuget | |
path: ${{ env.NuGetDirectory }} | |
- name: Install .NET | |
uses: actions/setup-dotnet@v4 | |
# Publish the NuGet package to NuGet.org | |
# To publish to NuGet.org, you need to create an account and generate an API key | |
# - Create an account on NuGet.org | |
# - Go to https://www.nuget.org/account/apikeys | |
# - Create a new API key | |
# - Copy the API key and add it as a secret in your GitHub repository with the name NUGET_API_KEY | |
- name: Publish NuGet package to nuget.org | |
env: | |
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }} | |
run: | | |
foreach($file in (Get-ChildItem "${{ env.NuGetDirectory }}" -Recurse -Include *.nupkg, *.snupkg)) { | |
dotnet nuget push $file --source https://api.nuget.org/v3/index.json --skip-duplicate | |
} |