Basic CICD #2
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CI | |
| permissions: | |
| contents: write | |
| packages: write | |
| on: | |
| pull_request: | |
| types: [opened, synchronize, reopened] | |
| push: | |
| tags: | |
| - 'v*.*.*' | |
| jobs: | |
| build: | |
| name: Restore, Build and Test | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Setup .NET | |
| uses: actions/setup-dotnet@v3 | |
| with: | |
| dotnet-version: '10.x' | |
| - name: Restore | |
| run: dotnet restore | |
| - name: Build | |
| run: dotnet build TenJames.CompMap.sln -c Release --no-restore | |
| - name: Test (if present) | |
| run: | | |
| if [ -d "TenJames.CompMap/TenJames.CompMap.Tests" ]; then | |
| dotnet test TenJames.CompMap/TenJames.CompMap.Tests -c Release --no-build --verbosity normal | |
| else | |
| echo "No tests found" | |
| fi | |
| bump-version: | |
| name: Bump package version (patch) on PR | |
| if: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository }} | |
| needs: build | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout (full) | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Setup Git | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| - name: Bump package version (patch) in TenJames.CompMap.csproj | |
| id: bump | |
| run: | | |
| set -e | |
| csproj="TenJames.CompMap/TenJames.CompMap/TenJames.CompMap.csproj" | |
| if [ ! -f "$csproj" ]; then | |
| echo "CSProj not found: $csproj" | |
| exit 0 | |
| fi | |
| # extract version like 1.2.3 | |
| version=$(grep -oPm1 "(?<=<Version>)[^<]+" "$csproj" || true) | |
| if [ -z "$version" ]; then | |
| echo "No <Version> found in $csproj" | |
| exit 0 | |
| fi | |
| echo "Current version: $version" | |
| IFS='.' read -r major minor patch <<< "$version" | |
| if [ -z "$patch" ]; then | |
| echo "Version format unexpected: $version" | |
| exit 1 | |
| fi | |
| new_patch=$((patch + 1)) | |
| new_version="${major}.${minor}.${new_patch}" | |
| # update the csproj | |
| sed -i "s|<Version>${version}</Version>|<Version>${new_version}</Version>|" "$csproj" | |
| git add "$csproj" | |
| git commit -m "ci: bump version to ${new_version} [skip ci]" || echo "No changes to commit" | |
| # push back to the PR branch | |
| git push origin HEAD:refs/heads/${{ github.event.pull_request.head.ref }} || echo "Push failed (maybe from a fork)" | |
| echo "new_version=${new_version}" >> $GITHUB_OUTPUT | |
| publish: | |
| name: Pack, update csproj and publish on tag | |
| if: startsWith(github.ref, 'refs/tags/') | |
| needs: build | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Setup .NET | |
| uses: actions/setup-dotnet@v3 | |
| with: | |
| dotnet-version: '10.x' | |
| - name: Determine package version from tag | |
| id: vars | |
| run: | | |
| TAG=${GITHUB_REF#refs/tags/} | |
| VERSION=${TAG#v} | |
| echo "tag=${TAG}" >> $GITHUB_OUTPUT | |
| echo "version=${VERSION}" >> $GITHUB_OUTPUT | |
| - name: Configure Git for pushing | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| - name: Update csproj version and push to default branch | |
| env: | |
| VERSION: ${{ steps.vars.outputs.version }} | |
| TAG: ${{ steps.vars.outputs.tag }} | |
| run: | | |
| set -e | |
| csproj="TenJames.CompMap/TenJames.CompMap/TenJames.CompMap.csproj" | |
| if [ ! -f "$csproj" ]; then | |
| echo "CSProj not found: $csproj" | |
| exit 0 | |
| fi | |
| default_branch="${{ github.event.repository.default_branch }}" | |
| echo "Default branch is: $default_branch" | |
| # fetch and checkout default branch to make change there | |
| git fetch origin $default_branch | |
| git checkout $default_branch | |
| current=$(grep -oPm1 "(?<=<Version>)[^<]+" "$csproj" || true) | |
| echo "Current csproj version: ${current}" | |
| echo "Desired version: ${VERSION}" | |
| if [ "${current}" != "${VERSION}" ]; then | |
| if grep -q "<Version>" "$csproj"; then | |
| sed -i "s|<Version>.*</Version>|<Version>${VERSION}</Version>|" "$csproj" | |
| else | |
| # insert Version element after the first PropertyGroup using perl to avoid quoting issues | |
| perl -0777 -pe "s/(<PropertyGroup>\s*)/$1 <Version>${VERSION}<\/Version>\n/s if !/<Version>/s" -i "$csproj" | |
| fi | |
| 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" | |
| else | |
| echo "CSProj already matches tag version" | |
| fi | |
| - name: Pack | |
| run: | | |
| dotnet pack TenJames.CompMap/TenJames.CompMap/TenJames.CompMap.csproj -c Release -o ./nupkgs /p:PackageVersion=${{ steps.vars.outputs.version }} | |
| - name: Publish to NuGet | |
| env: | |
| NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }} | |
| run: | | |
| if [ -z "$NUGET_API_KEY" ]; then | |
| echo "NUGET_API_KEY not set - skipping publish" | |
| exit 0 | |
| fi | |
| dotnet nuget push ./nupkgs/*.nupkg -k "$NUGET_API_KEY" -s https://api.nuget.org/v3/index.json --skip-duplicate |