From 15d8290706a4d1994cc39fb6463c76e26dfeb91d Mon Sep 17 00:00:00 2001 From: James Date: Fri, 14 Nov 2025 21:17:55 +0100 Subject: [PATCH 1/2] Improving CICD --- .github/workflows/ci.yml | 65 ++++++++++++++++++++++++++++++++++++++-- Readme.md | 11 ------- 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 61607ba..4a34625 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -139,9 +139,10 @@ jobs: default_branch="${{ github.event.repository.default_branch }}" echo "Default branch is: $default_branch" - # fetch and checkout default branch to make change there + # create a new branch from default branch to avoid push-to-protected-branch issues git fetch origin $default_branch - git checkout $default_branch + branch_name="ci/bump-version-${{ steps.vars.outputs.version }}" + git checkout -b "$branch_name" origin/$default_branch || git checkout -b "$branch_name" current=$(grep -oPm1 "(?<=)[^<]+" "$csproj" || true) echo "Current csproj version: ${current}" @@ -157,14 +158,72 @@ jobs: git add "$csproj" git commit -m "chore: set package version to ${VERSION} (tag ${TAG}) [skip ci]" || echo "No changes to commit" - git push origin $default_branch || echo "Push failed" + git push --set-upstream origin "$branch_name" || echo "Push failed" + + echo "branch_name=${branch_name}" >> $GITHUB_OUTPUT else echo "CSProj already matches tag version" fi + + - name: Create Pull Request for version update + uses: actions/github-script@v7 + env: + VERSION: ${{ steps.vars.outputs.version }} + TAG: ${{ steps.vars.outputs.tag }} + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const headBranch = `ci/bump-version-${process.env.VERSION}`; + const base = `${{ github.event.repository.default_branch }}`; + const owner = context.repo.owner; + const repo = context.repo.repo; + const title = `chore: set package version to ${process.env.VERSION} (tag ${process.env.TAG})`; + const body = `This PR updates TenJames.CompMap.csproj version to ${process.env.VERSION} (created by release workflow for tag ${process.env.TAG}).`; + + // Check for existing open PR from the same head + const existing = await github.pulls.list({ owner, repo, head: `${owner}:${headBranch}`, state: 'open' }); + if (existing.data && existing.data.length > 0) { + console.log(`Found existing PR: ${existing.data[0].html_url}`); + } else { + const pr = await github.pulls.create({ owner, repo, title, head: headBranch, base, body }); + console.log(`Created PR: ${pr.data.html_url}`); + } + - name: Pack run: | dotnet pack TenJames.CompMap/TenJames.CompMap/TenJames.CompMap.csproj -c Release -o ./nupkgs /p:PackageVersion=${{ steps.vars.outputs.version }} + - name: Find generated nupkg + id: find_pkg + run: | + set -e + pkg=$(ls ./nupkgs/*.nupkg | head -n1 || true) + if [ -z "$pkg" ]; then + echo "No nupkg found in ./nupkgs" + exit 1 + fi + echo "package_path=$pkg" >> $GITHUB_OUTPUT + echo "package_name=$(basename $pkg)" >> $GITHUB_OUTPUT + + - name: Create GitHub Release for tag + id: create_release + uses: actions/create-release@v1 + with: + tag_name: ${{ steps.vars.outputs.tag }} + release_name: ${{ steps.vars.outputs.tag }} + body: Release for tag ${{ steps.vars.outputs.tag }} + draft: false + prerelease: false + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Upload nupkg to Release + uses: actions/upload-release-asset@v1 + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ${{ steps.find_pkg.outputs.package_path }} + asset_name: ${{ steps.find_pkg.outputs.package_name }} + asset_content_type: application/octet-stream + - name: Publish to NuGet env: NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }} diff --git a/Readme.md b/Readme.md index f674ed9..ba63c82 100644 --- a/Readme.md +++ b/Readme.md @@ -133,17 +133,6 @@ A workflow was added at `.github/workflows/ci.yml` to automate verification and - On pull requests (opened/synchronized/reopened): - Restores, builds the solution and runs tests (if the `TenJames.CompMap.Tests` project exists). - - If the PR branch belongs to the same repository (not a fork), the workflow increments the patch version in `TenJames.CompMap/TenJames.CompMap/TenJames.CompMap.csproj` (e.g. 0.0.2 -> 0.0.3), commits the change and pushes it back to the PR branch, then adds a comment to the PR with the new version. This helps keep package versions unique for CI package artifacts. - -- On push of a tag matching `v*.*.*` (for example `v1.2.3`): - - The workflow builds the solution, packs `TenJames.CompMap` with the version taken from the tag (strips a leading `v` if present), and attempts to publish the resulting `.nupkg` to NuGet.org. - - Publishing runs only when a `NUGET_API_KEY` secret is set in the repository settings; otherwise the publish step is skipped. - -Notes and requirements: - -- Ensure the repository secret `NUGET_API_KEY` is set if you want automatic publishing to NuGet.org. -- The PR auto-bump only runs when the PR head repo equals this repository (forked PRs are skipped to avoid push permission errors). -- The workflow uses `dotnet 10.x` (actions/setup-dotnet@v3). Adjust the SDK version in the workflow if you need a different runtime. Quick commands (run locally) — fish shell: From 4300926804bbe1cdf62ace888af37950ee382128 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 14 Nov 2025 20:19:20 +0000 Subject: [PATCH 2/2] ci: bump version to 0.0.5 [skip ci] --- TenJames.CompMap/TenJames.CompMap/TenJames.CompMap.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TenJames.CompMap/TenJames.CompMap/TenJames.CompMap.csproj b/TenJames.CompMap/TenJames.CompMap/TenJames.CompMap.csproj index 0ea6af7..a4c3c39 100644 --- a/TenJames.CompMap/TenJames.CompMap/TenJames.CompMap.csproj +++ b/TenJames.CompMap/TenJames.CompMap/TenJames.CompMap.csproj @@ -14,7 +14,7 @@ - 0.0.4 + 0.0.5 Compiletime Mapper Map your object on compile time https://github.com/Ten-James/CompMap