|
| 1 | +name: CI |
| 2 | + |
| 3 | +permissions: |
| 4 | + contents: write |
| 5 | + packages: write |
| 6 | + |
| 7 | +on: |
| 8 | + pull_request: |
| 9 | + types: [opened, synchronize, reopened] |
| 10 | + push: |
| 11 | + tags: |
| 12 | + - 'v*.*.*' |
| 13 | + |
| 14 | +jobs: |
| 15 | + build: |
| 16 | + name: Restore, Build and Test |
| 17 | + runs-on: ubuntu-latest |
| 18 | + steps: |
| 19 | + - name: Checkout |
| 20 | + uses: actions/checkout@v4 |
| 21 | + with: |
| 22 | + fetch-depth: 0 |
| 23 | + |
| 24 | + - name: Setup .NET |
| 25 | + uses: actions/setup-dotnet@v3 |
| 26 | + with: |
| 27 | + dotnet-version: '10.x' |
| 28 | + |
| 29 | + - name: Restore |
| 30 | + run: dotnet restore |
| 31 | + |
| 32 | + - name: Build |
| 33 | + run: dotnet build TenJames.CompMap.sln -c Release --no-restore |
| 34 | + |
| 35 | + - name: Test (if present) |
| 36 | + run: | |
| 37 | + if [ -d "TenJames.CompMap/TenJames.CompMap.Tests" ]; then |
| 38 | + dotnet test TenJames.CompMap/TenJames.CompMap.Tests -c Release --no-build --verbosity normal |
| 39 | + else |
| 40 | + echo "No tests found" |
| 41 | + fi |
| 42 | +
|
| 43 | + bump-version: |
| 44 | + name: Bump package version (patch) on PR |
| 45 | + if: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository }} |
| 46 | + needs: build |
| 47 | + runs-on: ubuntu-latest |
| 48 | + steps: |
| 49 | + - name: Checkout (full) |
| 50 | + uses: actions/checkout@v4 |
| 51 | + with: |
| 52 | + fetch-depth: 0 |
| 53 | + |
| 54 | + - name: Setup Git |
| 55 | + run: | |
| 56 | + git config user.name "github-actions[bot]" |
| 57 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 58 | +
|
| 59 | + - name: Bump package version (patch) in TenJames.CompMap.csproj |
| 60 | + id: bump |
| 61 | + run: | |
| 62 | + set -e |
| 63 | + csproj="TenJames.CompMap/TenJames.CompMap/TenJames.CompMap.csproj" |
| 64 | + if [ ! -f "$csproj" ]; then |
| 65 | + echo "CSProj not found: $csproj" |
| 66 | + exit 0 |
| 67 | + fi |
| 68 | +
|
| 69 | + # extract version like 1.2.3 |
| 70 | + version=$(grep -oPm1 "(?<=<Version>)[^<]+" "$csproj" || true) |
| 71 | + if [ -z "$version" ]; then |
| 72 | + echo "No <Version> found in $csproj" |
| 73 | + exit 0 |
| 74 | + fi |
| 75 | +
|
| 76 | + echo "Current version: $version" |
| 77 | +
|
| 78 | + IFS='.' read -r major minor patch <<< "$version" |
| 79 | + if [ -z "$patch" ]; then |
| 80 | + echo "Version format unexpected: $version" |
| 81 | + exit 1 |
| 82 | + fi |
| 83 | +
|
| 84 | + new_patch=$((patch + 1)) |
| 85 | + new_version="${major}.${minor}.${new_patch}" |
| 86 | +
|
| 87 | + # update the csproj |
| 88 | + sed -i "s|<Version>${version}</Version>|<Version>${new_version}</Version>|" "$csproj" |
| 89 | +
|
| 90 | + git add "$csproj" |
| 91 | + git commit -m "ci: bump version to ${new_version} [skip ci]" || echo "No changes to commit" |
| 92 | +
|
| 93 | + # push back to the PR branch |
| 94 | + git push origin HEAD:refs/heads/${{ github.event.pull_request.head.ref }} || echo "Push failed (maybe from a fork)" |
| 95 | +
|
| 96 | + echo "new_version=${new_version}" >> $GITHUB_OUTPUT |
| 97 | +
|
| 98 | + publish: |
| 99 | + name: Pack, update csproj and publish on tag |
| 100 | + if: startsWith(github.ref, 'refs/tags/') |
| 101 | + needs: build |
| 102 | + runs-on: ubuntu-latest |
| 103 | + steps: |
| 104 | + - name: Checkout |
| 105 | + uses: actions/checkout@v4 |
| 106 | + with: |
| 107 | + fetch-depth: 0 |
| 108 | + |
| 109 | + - name: Setup .NET |
| 110 | + uses: actions/setup-dotnet@v3 |
| 111 | + with: |
| 112 | + dotnet-version: '10.x' |
| 113 | + |
| 114 | + - name: Determine package version from tag |
| 115 | + id: vars |
| 116 | + run: | |
| 117 | + TAG=${GITHUB_REF#refs/tags/} |
| 118 | + VERSION=${TAG#v} |
| 119 | + echo "tag=${TAG}" >> $GITHUB_OUTPUT |
| 120 | + echo "version=${VERSION}" >> $GITHUB_OUTPUT |
| 121 | +
|
| 122 | + - name: Configure Git for pushing |
| 123 | + run: | |
| 124 | + git config user.name "github-actions[bot]" |
| 125 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 126 | +
|
| 127 | + - name: Update csproj version and push to default branch |
| 128 | + env: |
| 129 | + VERSION: ${{ steps.vars.outputs.version }} |
| 130 | + TAG: ${{ steps.vars.outputs.tag }} |
| 131 | + run: | |
| 132 | + set -e |
| 133 | + csproj="TenJames.CompMap/TenJames.CompMap/TenJames.CompMap.csproj" |
| 134 | + if [ ! -f "$csproj" ]; then |
| 135 | + echo "CSProj not found: $csproj" |
| 136 | + exit 0 |
| 137 | + fi |
| 138 | +
|
| 139 | + default_branch="${{ github.event.repository.default_branch }}" |
| 140 | + echo "Default branch is: $default_branch" |
| 141 | +
|
| 142 | + # fetch and checkout default branch to make change there |
| 143 | + git fetch origin $default_branch |
| 144 | + git checkout $default_branch |
| 145 | +
|
| 146 | + current=$(grep -oPm1 "(?<=<Version>)[^<]+" "$csproj" || true) |
| 147 | + echo "Current csproj version: ${current}" |
| 148 | + echo "Desired version: ${VERSION}" |
| 149 | +
|
| 150 | + if [ "${current}" != "${VERSION}" ]; then |
| 151 | + if grep -q "<Version>" "$csproj"; then |
| 152 | + sed -i "s|<Version>.*</Version>|<Version>${VERSION}</Version>|" "$csproj" |
| 153 | + else |
| 154 | + # insert Version element after the first PropertyGroup using perl to avoid quoting issues |
| 155 | + perl -0777 -pe "s/(<PropertyGroup>\s*)/$1 <Version>${VERSION}<\/Version>\n/s if !/<Version>/s" -i "$csproj" |
| 156 | + fi |
| 157 | +
|
| 158 | + git add "$csproj" |
| 159 | + git commit -m "chore: set package version to ${VERSION} (tag ${TAG}) [skip ci]" || echo "No changes to commit" |
| 160 | + git push origin $default_branch || echo "Push failed" |
| 161 | + else |
| 162 | + echo "CSProj already matches tag version" |
| 163 | + fi |
| 164 | + - name: Pack |
| 165 | + run: | |
| 166 | + dotnet pack TenJames.CompMap/TenJames.CompMap/TenJames.CompMap.csproj -c Release -o ./nupkgs /p:PackageVersion=${{ steps.vars.outputs.version }} |
| 167 | +
|
| 168 | + - name: Publish to NuGet |
| 169 | + env: |
| 170 | + NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }} |
| 171 | + run: | |
| 172 | + if [ -z "$NUGET_API_KEY" ]; then |
| 173 | + echo "NUGET_API_KEY not set - skipping publish" |
| 174 | + exit 0 |
| 175 | + fi |
| 176 | + dotnet nuget push ./nupkgs/*.nupkg -k "$NUGET_API_KEY" -s https://api.nuget.org/v3/index.json --skip-duplicate |
0 commit comments