diff --git a/.github/workflows/nuget-package-template.yml b/.github/workflows/nuget-package-template.yml new file mode 100644 index 0000000..a900208 --- /dev/null +++ b/.github/workflows/nuget-package-template.yml @@ -0,0 +1,88 @@ +name: Build and Push NuGet Package Template + +on: + workflow_call: + inputs: + project_name: + required: true + type: string + description: "The name of the project" + project_path: + required: true + type: string + description: "The relative path to the project directory" + nuget_package_name: + required: true + type: string + description: "The name of the NuGet package" + +env: + DOTNET_VERSION: '8.0.x' + +jobs: + build-and-package: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'success' }} # Ensures it only runs on success + permissions: + packages: write + contents: write + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: ${{ env.DOTNET_VERSION }} + + - name: Set up curl and jq + run: sudo apt-get install -y curl jq + + - name: Check for custom version in commit message or check the feed for the latest version and increment it + id: check_custom_version + run: | + COMMIT_HASH=$(git log -n 10 --pretty=format:"%H %s" | grep -P '\(#update package version to \d+\.\d+\.\d+\)' | grep -oP '^\w+' | head -n 1) + + if [[ -n "$COMMIT_HASH" ]]; then + echo "Found commit with version update indicator: $COMMIT_HASH" + if git rev-parse "processed-nuget-version-${COMMIT_HASH}" >/dev/null 2>&1; then + echo "This commit has already been processed for version update. Skipping." + else + CUSTOM_VERSION=$(git show -s --format=%s $COMMIT_HASH | grep -oP '\(#update package version to \K([0-9]+\.[0-9]+\.[0-9]+)') + if [[ -n "$CUSTOM_VERSION" ]]; then + echo "Using custom version: $CUSTOM_VERSION" + echo "NEW_VERSION=$CUSTOM_VERSION" >> $GITHUB_ENV + git tag "processed-nuget-version-${COMMIT_HASH}" + git push origin "processed-nuget-version-${COMMIT_HASH}" + else + echo "Failed to extract version from commit message. Exiting." + exit 1 + fi + fi + fi + + if [[ -z "$CUSTOM_VERSION" ]]; then + PACKAGE_ID="${{ inputs.nuget_package_name }}" + FEED_URL="https://nuget.pkg.github.com/DFE-Digital/query?q=$PACKAGE_ID" + LATEST_VERSION=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "$FEED_URL" | jq -r '.data[0].version') + + if [[ -z "$LATEST_VERSION" || "$LATEST_VERSION" == "null" ]]; then + echo "No existing version found in the feed. Defaulting to version 1.0.0" + NEW_VERSION="1.0.0" + else + echo "Latest version is $LATEST_VERSION" + IFS='.' read -r -a VERSION_PARTS <<< "$LATEST_VERSION" + NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.$((VERSION_PARTS[2] + 1))" + echo "Incrementing to new version: $NEW_VERSION" + fi + + echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV + fi + + - name: Build, pack and publish + working-directory: ${{ inputs.project_path }} + run: | + dotnet build -c Release + dotnet pack -c Release -p:PackageVersion=${{ env.NEW_VERSION }} --output . + dotnet nuget push "*.nupkg" --api-key ${{ secrets.GITHUB_TOKEN }} --source https://nuget.pkg.github.com/DFE-Digital/index.json diff --git a/.github/workflows/pack-caching.yml b/.github/workflows/pack-caching.yml new file mode 100644 index 0000000..b6e895d --- /dev/null +++ b/.github/workflows/pack-caching.yml @@ -0,0 +1,15 @@ +name: Build and Push NuGet Package for DfE.CoreLibs.Caching + +on: + workflow_run: + workflows: [".NET Build and Test for DfE.CoreLibs.Caching"] + types: + - completed + +jobs: + build-and-package: + uses: ./.github/workflows/nuget-package-template.yml + with: + project_name: DfE.CoreLibs.Caching + project_path: src/DfE.CoreLibs.Caching + nuget_package_name: DfE.CoreLibs.Caching diff --git a/src/DfE.CoreLibs.Caching/ServiceCollectionExtensions.cs b/src/DfE.CoreLibs.Caching/ServiceCollectionExtensions.cs index c688d21..f621eef 100644 --- a/src/DfE.CoreLibs.Caching/ServiceCollectionExtensions.cs +++ b/src/DfE.CoreLibs.Caching/ServiceCollectionExtensions.cs @@ -3,6 +3,7 @@ using DfE.CoreLibs.Caching.Settings; using Microsoft.Extensions.Configuration; + namespace Microsoft.Extensions.DependencyInjection { public static class ServiceCollectionExtensions @@ -16,4 +17,4 @@ public static IServiceCollection AddServiceCaching( return services; } } -} \ No newline at end of file +}