From e01a2b34ad9f758c67b4b8b21aacf84c5391520b Mon Sep 17 00:00:00 2001 From: JosepFe Date: Tue, 10 Dec 2024 17:50:27 +0100 Subject: [PATCH] github action devon4net-infrastructure-nuget-package-publish (#3) Add new github action devon4net-infrastructure-nuget-package-publish --- ...t-infrastructure-nuget-package-publish.yml | 110 ++++++++++++++++++ .github/workflows/nuget-package.yml | 75 ------------ 2 files changed, 110 insertions(+), 75 deletions(-) create mode 100644 .github/workflows/devon4net-infrastructure-nuget-package-publish.yml delete mode 100644 .github/workflows/nuget-package.yml diff --git a/.github/workflows/devon4net-infrastructure-nuget-package-publish.yml b/.github/workflows/devon4net-infrastructure-nuget-package-publish.yml new file mode 100644 index 00000000..67decf19 --- /dev/null +++ b/.github/workflows/devon4net-infrastructure-nuget-package-publish.yml @@ -0,0 +1,110 @@ +name: Build and Publish NuGet Package + +on: + workflow_dispatch: + inputs: + project: + description: 'Select the project to build and pack' + required: true + default: 'Devon4Net.Infrastructure.JWT' + options: + - 'Devon4Net.Infrastructure.Common' + - 'Devon4Net.Infrastructure.Cors' + - 'Devon4Net.Infrastructure.JWT' + version_type: + description: 'Select the version type' + required: true + default: 'preview' + options: + - 'major' + - 'minor' + - 'patch' + - 'preview' + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup .NET Core + uses: actions/setup-dotnet@v2 + with: + dotnet-version: '8.x' + + - name: Get latest version from GitHub Packages + run: | + PACKAGE_NAME=$(basename "${{ github.event.inputs.project }}" .csproj) + GITHUB_USERNAME="JosepFe" + GITHUB_TOKEN="${{ secrets.TOKEN_GITHUB }}" + + LATEST_VERSION=$(curl -s -u $GITHUB_USERNAME:$GITHUB_TOKEN \ + -H "Accept: application/vnd.github+json" \ + "https://api.github.com/users/$GITHUB_USERNAME/packages/nuget/$PACKAGE_NAME/versions" | \ + jq -r '.[0].name') + + if [ -z "$LATEST_VERSION" ]; then + echo "No version found for package $PACKAGE_NAME." + exit 1 + fi + + echo "Latest version: $LATEST_VERSION" + echo "LATEST_VERSION=$LATEST_VERSION" >> $GITHUB_ENV + + - name: Determine new version + id: version + run: | + VERSION_TYPE="${{ github.event.inputs.version_type }}" + LATEST_VERSION="${{ env.LATEST_VERSION }}" + + # Extract major, minor, patch from latest version (assumes format major.minor.patch or major.minor.patch-preview) + MAJOR=$(echo $LATEST_VERSION | cut -d. -f1) + MINOR=$(echo $LATEST_VERSION | cut -d. -f2) + PATCH=$(echo $LATEST_VERSION | cut -d. -f3 | cut -d- -f1) # Removes preview suffix if present + + # If the latest version has a preview label, extract the preview number + PREVIEW_SUFFIX=$(echo $LATEST_VERSION | grep -oP '(?<=-preview\.)\d+' || echo "0") + + # Increment version based on user input + if [ "$VERSION_TYPE" == "major" ]; then + NEW_VERSION="$((MAJOR + 1)).0.0" + + elif [ "$VERSION_TYPE" == "minor" ]; then + NEW_VERSION="$MAJOR.$((MINOR + 1)).0" + + elif [ "$VERSION_TYPE" == "patch" ]; then + echo $PREVIEW_SUFFIX + if [ "$PREVIEW_SUFFIX" -ne "0" ]; then + NEW_VERSION="$MAJOR.$MINOR.$PATCH" + elif [ "$PREVIEW_SUFFIX" -eq "0" ]; then + NEW_VERSION="$MAJOR.$MINOR.$((PATCH + 1))" + fi + + elif [ "$VERSION_TYPE" == "preview" ]; then + # If the patch version is 0 and the preview is selected, we need to start from patch 1 + if [ "$PATCH" -eq "0" ]; then + PATCH=1 + fi + + # Increment the preview number + NEW_PREVIEW_SUFFIX=$((PREVIEW_SUFFIX + 1)) + NEW_VERSION="$MAJOR.$MINOR.$PATCH-preview.$NEW_PREVIEW_SUFFIX" + fi + echo "New version: $NEW_VERSION" + echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV + + - name: Restore dependencies + run: dotnet restore source/Modules/${{ github.event.inputs.project }}/${{ github.event.inputs.project }}.csproj + + - name: Build project + run: dotnet build source/Modules/${{ github.event.inputs.project }}/${{ github.event.inputs.project }}.csproj --configuration Release --no-restore + + - name: Pack NuGet package + run: dotnet pack source/Modules/${{ github.event.inputs.project }}/${{ github.event.inputs.project }}.csproj --configuration Release --no-build --output ./nuget-packages /p:PackageVersion=${{ env.NEW_VERSION }} + + - name: Push NuGet package to GitHub Packages + run: | + project_dir="/home/runner/work/devon4net/devon4net/nuget-packages" + dotnet nuget push "$project_dir/*.nupkg" --source https://nuget.pkg.github.com/JosepFe/index.json --api-key ${{ secrets.TOKEN_GITHUB }} \ No newline at end of file diff --git a/.github/workflows/nuget-package.yml b/.github/workflows/nuget-package.yml deleted file mode 100644 index 7f7b7a2c..00000000 --- a/.github/workflows/nuget-package.yml +++ /dev/null @@ -1,75 +0,0 @@ -name: Build and Publish NuGet Package - -on: - workflow_dispatch: - inputs: - project: - description: 'Select the project to build' - required: true - default: 'Path/To/Project1.csproj' - options: - - 'Path/To/Project1.csproj' - - 'Path/To/Project2.csproj' - version_type: - description: 'Select the version type' - required: true - default: 'patch' - options: - - 'major' - - 'minor' - - 'patch' - - 'preview' - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v2 - - - name: Setup .NET Core - uses: actions/setup-dotnet@v2 - with: - dotnet-version: '7.x' - - - name: Get latest version from GitHub Packages - run: | - LATEST_VERSION=$(dotnet nuget list source --source "https://nuget.pkg.github.com//index.json" | grep "" | head -n 1 | awk '{print $2}') - echo "Latest version: $LATEST_VERSION" - echo "LATEST_VERSION=$LATEST_VERSION" >> $GITHUB_ENV - - - name: Determine new version - id: version - run: | - VERSION_TYPE="${{ github.event.inputs.version_type }}" - LATEST_VERSION="${{ env.LATEST_VERSION }}" - - # Extract major, minor, patch from latest version (assumes format major.minor.patch or major.minor.patch-preview) - MAJOR=$(echo $LATEST_VERSION | cut -d. -f1) - MINOR=$(echo $LATEST_VERSION | cut -d. -f2) - PATCH=$(echo $LATEST_VERSION | cut -d. -f3 | cut -d- -f1) # Removes preview suffix if present - - # Increment version based on user input - if [ "$VERSION_TYPE" == "major" ]; then - NEW_VERSION="$((MAJOR + 1)).0.0" - elif [ "$VERSION_TYPE" == "minor" ]; then - NEW_VERSION="$MAJOR.$((MINOR + 1)).0" - elif [ "$VERSION_TYPE" == "patch" ]; then - NEW_VERSION="$MAJOR.$MINOR.$((PATCH + 1))" - elif [ "$VERSION_TYPE" == "preview" ]; then - PREVIEW_SUFFIX="-preview" - NEW_VERSION="$MAJOR.$MINOR.$((PATCH + 1))$PREVIEW_SUFFIX" - fi - - echo "New version: $NEW_VERSION" - echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV - - - name: Restore dependencies - run: dotnet restore ${{ github.event.inputs.project }} - - - name: Build project - run: dotnet build ${{ github.event.inputs.project }} --configuration Release --no-restore - - - name: Pack NuGet package - run: dotnet pack ${{ github.event.inputs.project }} --configuration Release --no-build --output ./nuget-packages /p:PackageVersion=${{ env.NEW_VERSION }} \ No newline at end of file