Publish Packages and Examples Tests #2
This file contains 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
# PROCESS | |
# | |
# 1. Build and pack all libraries in the solution | |
# 2. Set up examples to use these local packages | |
# 3. Run tests on examples to verify they work with the latest code | |
# 4. Publish packages to GitHub Packages (on develop branch only) | |
# USAGE | |
# | |
# This workflow is triggered on push to the develop branch or manually via workflow_dispatch. | |
name: Publish Packages and Examples Tests | |
on: | |
workflow_dispatch: | |
push: | |
paths: | |
- "libraries/**" | |
branches: | |
- develop | |
permissions: | |
contents: read | |
jobs: | |
pack-libraries: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
- name: Setup .NET | |
uses: actions/setup-dotnet@3951f0dfe7a07e2313ec93c75700083e2005cbab # 4.3.0 | |
with: | |
dotnet-version: | | |
6.0.x | |
8.0.x | |
- name: Build libraries | |
run: dotnet build ./libraries/ --configuration Release | |
- name: Pack libraries | |
run: | | |
mkdir -p ./packages | |
VERSION_SUFFIX=${{ github.run_id }} | |
dotnet pack ./libraries/ --configuration Release --no-build --output ./packages --version-suffix $VERSION_SUFFIX | |
- name: Upload packages | |
uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 #4.6.1 | |
with: | |
name: nuget-packages | |
path: ./packages/ | |
run-tests: | |
permissions: | |
id-token: write | |
runs-on: ubuntu-latest | |
needs: pack-libraries | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
- name: Set up .NET | |
uses: actions/setup-dotnet@3951f0dfe7a07e2313ec93c75700083e2005cbab # 4.3.0 | |
with: | |
dotnet-version: | | |
6.0.x | |
8.0.x | |
- name: Download packages | |
uses: actions/download-artifact@cc203385981b70ca67e1cc392babf9cc229d5806 # 4.1.9 | |
with: | |
name: nuget-packages | |
path: ./packages/ | |
- name: Configure local NuGet source | |
run: | | |
dotnet nuget add source ${{ github.workspace }}/packages --name local | |
# Ensure we preserve access to NuGet.org | |
- name: Configure NuGet.org source | |
run: | | |
dotnet nuget add source https://api.nuget.org/v3/index.json --name nuget.org | |
- name: Update examples to use local packages | |
run: | | |
find ./examples -name "*.csproj" | while read project; do | |
echo "Updating $project to use local packages" | |
for package in ./packages/*.nupkg; do | |
# Extract package name and version | |
packageName=$(basename $package .nupkg | sed -E 's/(.*)\.([0-9]+\.[0-9]+\.[0-9]+.*)$/\1/') | |
packageVersion=$(basename $package .nupkg | sed -E 's/(.*)\.([0-9]+\.[0-9]+\.[0-9]+.*)$/\2/') | |
# Check if project references this package | |
if grep -q "<PackageReference.*Include=\"$packageName\"" "$project"; then | |
echo " - Updating $packageName to version $packageVersion" | |
# Use --no-restore to avoid restoring during each add | |
dotnet add "$project" package "$packageName" --version "$packageVersion" --source "local" --no-restore | |
fi | |
done | |
done | |
- name: Dotnet restore | |
run: dotnet restore ./examples/ | |
- name: Test Examples | |
run: dotnet test ./examples/ --no-restore --configuration Release --verbosity normal | |
publish-packages: | |
if: github.event_name == 'push' && github.ref == 'refs/heads/develop' | |
needs: run-tests | |
runs-on: ubuntu-latest | |
permissions: | |
packages: write | |
steps: | |
- name: Download packages | |
uses: actions/download-artifact@cc203385981b70ca67e1cc392babf9cc229d5806 # 4.1.9 | |
with: | |
name: nuget-packages | |
path: ./packages/ | |
- name: Setup .NET | |
uses: actions/setup-dotnet@3951f0dfe7a07e2313ec93c75700083e2005cbab # 4.3.0 | |
with: | |
dotnet-version: | | |
6.0.x | |
8.0.x | |
- name: Setup GitHub Packages source | |
run: | | |
dotnet nuget add source https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json \ | |
--name github \ | |
--username ${{ github.actor }} \ | |
--password ${{ secrets.GITHUB_TOKEN }} | |
- name: Publish packages to GitHub Packages | |
run: | | |
for package in ./packages/*.nupkg; do | |
dotnet nuget push $package --source github --api-key ${{ secrets.GITHUB_TOKEN }} | |
done |