Skip to content

fix ci to deploy correctly, and cache the build #229

fix ci to deploy correctly, and cache the build

fix ci to deploy correctly, and cache the build #229

Workflow file for this run

name: Build and Test
on:
workflow_dispatch:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
DOTNET_NOLOGO: true
jobs:
analyze:
runs-on: ubuntu-latest
outputs:
has_changes: ${{ steps.check.outputs.has_changes }}
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Check for changes
id: check
run: |
# Manual triggers always run the full pipeline
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "has_changes=true" >> $GITHUB_OUTPUT
exit 0
fi
BASE_REF="${{ github.event_name == 'pull_request' && 'origin/main' || 'HEAD~1' }}"
CHANGED=$(git diff --name-only "$BASE_REF" HEAD 2>/dev/null || true)
if [ -n "$CHANGED" ]; then
echo "has_changes=true" >> $GITHUB_OUTPUT
else
echo "has_changes=false" >> $GITHUB_OUTPUT
fi
build:
needs: analyze
if: needs.analyze.outputs.has_changes == 'true'
runs-on: ubuntu-latest
outputs:
target: ${{ steps.scope.outputs.target }}
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- uses: actions/setup-dotnet@v5
- uses: actions/cache@v4
with:
path: ~/.nuget/packages
key: nuget-${{ hashFiles('**/*.csproj', '**/Directory.Packages.props') }}
restore-keys: nuget-
- name: Install dotnet-affected
run: dotnet tool install -g dotnet-affected
- name: Determine build scope
id: scope
run: |
# Manual triggers always build the full solution
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "::notice::Manual trigger — building full solution"
echo "target=Vulthil.SharedKernel.slnx" >> $GITHUB_OUTPUT
exit 0
fi
BASE_REF="${{ github.event_name == 'pull_request' && 'origin/main' || 'HEAD~1' }}"
dotnet affected --from "$BASE_REF" --to HEAD 2>/dev/null || true
if [ -f affected.proj ]; then
echo "target=affected.proj" >> $GITHUB_OUTPUT
else
echo "::notice::No affected projects — falling back to full solution build"
echo "target=Vulthil.SharedKernel.slnx" >> $GITHUB_OUTPUT
fi
- name: Restore
run: dotnet restore ${{ steps.scope.outputs.target }}
- name: Build
run: dotnet build ${{ steps.scope.outputs.target }} --configuration Release --no-restore
- name: Save build output
uses: actions/cache/save@v4
with:
path: |
**/bin/Release
**/obj
affected.proj
key: build-output-${{ github.run_id }}-${{ github.run_attempt }}
test:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- uses: actions/setup-dotnet@v5
- name: Restore build output
uses: actions/cache/restore@v4
with:
path: |
**/bin/Release
**/obj
affected.proj
key: build-output-${{ github.run_id }}-${{ github.run_attempt }}
fail-on-cache-miss: true
- name: Test
run: dotnet test ${{ needs.build.outputs.target }} --configuration Release --no-build
pack:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- uses: actions/setup-dotnet@v5
- name: Restore build output
uses: actions/cache/restore@v4
with:
path: |
**/bin/Release
**/obj
affected.proj
key: build-output-${{ github.run_id }}-${{ github.run_attempt }}
fail-on-cache-miss: true
- name: Pack
run: dotnet pack ${{ needs.build.outputs.target }} --configuration Release --no-build -o ./nupkgs
- name: Check if nupkgs exist
id: check_nupkgs
run: |
if [ -z "$(ls -A ./nupkgs/*.nupkg 2>/dev/null)" ]; then
echo "found=false" >> $GITHUB_OUTPUT
else
echo "found=true" >> $GITHUB_OUTPUT
fi
- name: Upload NuGet Packages
if: steps.check_nupkgs.outputs.found == 'true'
uses: actions/upload-artifact@v6
with:
name: nuget-packages
path: ./nupkgs/*.nupkg
deploy_preview:
needs: [test, pack]
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v7
with:
name: nuget-packages
path: ./nupkgs
continue-on-error: true
- name: Push to GitHub Packages
run: |
if [ -d "./nupkgs" ]; then
dotnet nuget push "./nupkgs/*.nupkg" \
--api-key ${{ secrets.GITHUB_TOKEN }} \
--source https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json \
--skip-duplicate
fi
create_release:
needs: [test, pack]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- uses: actions/setup-dotnet@v5
- name: Install NBGV
run: dotnet tool install -g nbgv
- name: Get Version Tag
id: versioning
run: |
VERSION=$(nbgv get-version -v SemVer2)
echo "tag=v$VERSION" >> $GITHUB_OUTPUT
- name: Create Release
uses: elgohr/Github-Release-Action@v5
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
title: "Release ${{ steps.versioning.outputs.tag }}"
tag: ${{ steps.versioning.outputs.tag }}
prerelease: false
deploy:
needs: create_release
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v7
with:
name: nuget-packages
path: ./nupkgs
- name: Push to GitHub Packages
run: |
dotnet nuget push "./nupkgs/*.nupkg" \
--api-key ${{ secrets.GITHUB_TOKEN }} \
--source https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json \
--skip-duplicate
#--api-key ${{ secrets.NUGET_API_KEY }} \
#--source https://api.nuget.org/v3/index.json \