|
| 1 | +name: Build and Test |
| 2 | +on: |
| 3 | + push: |
| 4 | + pull_request: |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + runPublish: |
| 8 | + description: 'Publish Nuget ?' |
| 9 | + required: true |
| 10 | + default: 'false' |
| 11 | + type: boolean |
| 12 | + |
| 13 | +jobs: |
| 14 | + build: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + outputs: |
| 17 | + semver: ${{ steps.gitversion.outputs.semVer }} |
| 18 | + fullsemver: ${{ steps.gitversion.outputs.fullSemVer }} |
| 19 | + nugetversion: ${{ steps.gitversion.outputs.fullSemVer }} |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Checkout code |
| 23 | + uses: actions/checkout@v5 |
| 24 | + with: |
| 25 | + fetch-depth: 0 # Required for GitVersion |
| 26 | + |
| 27 | + - name: Install GitVersion |
| 28 | + uses: gittools/actions/gitversion/[email protected] |
| 29 | + with: |
| 30 | + versionSpec: '6.x' |
| 31 | + |
| 32 | + - name: Determine Version |
| 33 | + uses: gittools/actions/gitversion/[email protected] |
| 34 | + id: gitversion |
| 35 | + |
| 36 | + - name: Format NuGet version |
| 37 | + run: | |
| 38 | + packageVersion="${{ steps.gitversion.outputs.majorMinorPatch }}.${{steps.gitversion.outputs.preReleaseNumber}}" |
| 39 | + echo "packageVersion=$packageVersion" >> $GITHUB_OUTPUT |
| 40 | + id: formatversion |
| 41 | + |
| 42 | + - name: Display GitVersion outputs |
| 43 | + run: | |
| 44 | + echo "Version: ${{ steps.gitversion.outputs.semVer }}" |
| 45 | + echo "AssemblyVersion: ${{ steps.gitversion.outputs.assemblySemVer }}" |
| 46 | + echo "FileVersion: ${{ steps.gitversion.outputs.assemblySemFileVer }}" |
| 47 | + echo "NuGet Version: ${{ steps.gitversion.outputs.fullSemVer }}" |
| 48 | + echo "Package Version: ${{ steps.formatversion.outputs.packageVersion }}" |
| 49 | +
|
| 50 | + - name: Setup .NET |
| 51 | + uses: actions/setup-dotnet@v5 |
| 52 | + with: |
| 53 | + dotnet-version: '8.0.x' |
| 54 | + |
| 55 | + - name: Build project |
| 56 | + working-directory: src |
| 57 | + run: >- |
| 58 | + dotnet build --configuration Release |
| 59 | + /p:Version=${{ steps.gitversion.outputs.assemblySemVer }} |
| 60 | + /p:AssemblyVersion=${{ steps.gitversion.outputs.assemblySemVer }} |
| 61 | + /p:FileVersion=${{ steps.gitversion.outputs.assemblySemFileVer }} |
| 62 | + /p:PackageVersion=${{ steps.formatversion.outputs.packageVersion }} |
| 63 | +
|
| 64 | + - name: Run tests with coverage |
| 65 | + working-directory: src |
| 66 | + run: >- |
| 67 | + dotnet test |
| 68 | + --configuration Release |
| 69 | + --collect:"XPlat Code Coverage" |
| 70 | + --results-directory ../coverage |
| 71 | +
|
| 72 | + - name: Generate coverage report |
| 73 | + uses: danielpalme/[email protected] |
| 74 | + with: |
| 75 | + reports: 'coverage/**/coverage.cobertura.xml' |
| 76 | + targetdir: 'coverage-report' |
| 77 | + reporttypes: 'Html;Cobertura;TextSummary' |
| 78 | + |
| 79 | + - name: Display Code Coverage Summary |
| 80 | + run: | |
| 81 | + echo "## 📊 Code Coverage Summary" >> $GITHUB_STEP_SUMMARY |
| 82 | + if [ -f "coverage-report/Summary.txt" ]; then |
| 83 | + echo '```' >> $GITHUB_STEP_SUMMARY |
| 84 | + cat coverage-report/Summary.txt >> $GITHUB_STEP_SUMMARY |
| 85 | + echo '```' >> $GITHUB_STEP_SUMMARY |
| 86 | + echo "### Build Log Coverage Summary:" |
| 87 | + cat coverage-report/Summary.txt |
| 88 | + else |
| 89 | + echo "⚠️ Coverage summary file not found" |
| 90 | + echo "⚠️ Coverage summary file not found" >> $GITHUB_STEP_SUMMARY |
| 91 | + fi |
| 92 | +
|
| 93 | + - name: Generate Rich Release Notes |
| 94 | + id: release_notes |
| 95 | + |
| 96 | + with: |
| 97 | + configuration: | |
| 98 | + { |
| 99 | + "template": "## 🚀 Release ${{ steps.gitversion.outputs.semVer }}\n\n### 📅 Release Information\n- **Version**: ${{ steps.gitversion.outputs.semVer }}\n- **NuGet Version**: ${{ steps.gitversion.outputs.fullSemVer }}\n- **Build Date**: $(date -u +'%Y-%m-%d %H:%M:%S UTC')\n- **Commit**: ${{ github.sha }}\n\n#{{CHANGELOG}}\n\n### 📊 Statistics\n- **Total Changes**: #{{UNCATEGORIZED_COUNT}} commits\n- **Contributors**: #{{CONTRIBUTORS}}\n\n---\n*Generated automatically by GitHub Actions*", |
| 100 | + "categories": [ |
| 101 | + { |
| 102 | + "title": "## 🚀 Features", |
| 103 | + "labels": ["feature", "enhancement", "feat"] |
| 104 | + }, |
| 105 | + { |
| 106 | + "title": "## 🐛 Bug Fixes", |
| 107 | + "labels": ["bug", "fix", "bugfix"] |
| 108 | + }, |
| 109 | + { |
| 110 | + "title": "## 📚 Documentation", |
| 111 | + "labels": ["documentation", "docs"] |
| 112 | + }, |
| 113 | + { |
| 114 | + "title": "## 🔧 Maintenance", |
| 115 | + "labels": ["maintenance", "chore", "refactor"] |
| 116 | + }, |
| 117 | + { |
| 118 | + "title": "## ⚠️ Breaking Changes", |
| 119 | + "labels": ["breaking", "breaking-change"] |
| 120 | + } |
| 121 | + ], |
| 122 | + "pr_template": "- #{{TITLE}} (#{{NUMBER}}) by @#{{AUTHOR}}", |
| 123 | + "empty_template": "- #{{TITLE}} (#{{HASH}}) by @#{{AUTHOR}}", |
| 124 | + "max_pull_requests": 200, |
| 125 | + "max_back_track_time_days": 365 |
| 126 | + } |
| 127 | + env: |
| 128 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 129 | + |
| 130 | + - name: Save Release Notes to File |
| 131 | + run: echo "${{ steps.release_notes.outputs.changelog }}" > release-notes.md |
| 132 | + |
| 133 | + - name: Upload Release Notes as artifact |
| 134 | + uses: actions/upload-artifact@v4 |
| 135 | + with: |
| 136 | + name: release-notes |
| 137 | + path: release-notes.md |
| 138 | + |
| 139 | + - name: Create NuGet package |
| 140 | + working-directory: src |
| 141 | + run: >- |
| 142 | + dotnet pack |
| 143 | + --configuration Release |
| 144 | + --no-build |
| 145 | + /p:PackageVersion=${{ steps.formatversion.outputs.packageVersion }} |
| 146 | + --output ../packages |
| 147 | +
|
| 148 | + - name: Upload NuGet package as artifact |
| 149 | + uses: actions/upload-artifact@v4 |
| 150 | + with: |
| 151 | + name: nuget-package |
| 152 | + path: packages/*.nupkg |
| 153 | + |
| 154 | + - name: Publish coverage report as artifact |
| 155 | + uses: actions/upload-artifact@v4 |
| 156 | + with: |
| 157 | + name: coverage-report |
| 158 | + path: coverage-report/ |
| 159 | + |
| 160 | + publish-nuget: |
| 161 | + runs-on: ubuntu-latest |
| 162 | + needs: build |
| 163 | + if: github.event.inputs.runPublish == 'true' && github.ref_name == github.event.repository.default_branch |
| 164 | + environment: |
| 165 | + name: Publish |
| 166 | + url: https://www.nuget.org/packages/TestStack.BDDfy/ |
| 167 | + |
| 168 | + steps: |
| 169 | + - name: Download NuGet package |
| 170 | + uses: actions/download-artifact@v5 |
| 171 | + with: |
| 172 | + name: nuget-package |
| 173 | + path: packages |
| 174 | + |
| 175 | + - name: Setup .NET |
| 176 | + uses: actions/setup-dotnet@v5 |
| 177 | + with: |
| 178 | + dotnet-version: '8.0.x' |
| 179 | + |
| 180 | + - name: Publish to NuGet |
| 181 | + run: dotnet nuget push packages/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate |
| 182 | + |
| 183 | + create-release-tag: |
| 184 | + runs-on: ubuntu-latest |
| 185 | + needs: [build, publish-nuget] |
| 186 | + if: github.event.inputs.runPublish == 'true' && github.ref_name == github.event.repository.default_branch |
| 187 | + permissions: |
| 188 | + contents: write |
| 189 | + |
| 190 | + steps: |
| 191 | + - name: Checkout code |
| 192 | + uses: actions/checkout@v5 |
| 193 | + with: |
| 194 | + fetch-depth: 0 |
| 195 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 196 | + |
| 197 | + - name: Check if tag already exists |
| 198 | + id: check_tag |
| 199 | + run: | |
| 200 | + TAG="v${{ needs.build.outputs.semver }}" |
| 201 | + if git rev-parse "$TAG" >/dev/null 2>&1; then |
| 202 | + echo "Tag $TAG already exists" |
| 203 | + echo "tag_exists=true" >> $GITHUB_OUTPUT |
| 204 | + else |
| 205 | + echo "Creating new tag: $TAG" |
| 206 | + echo "tag_exists=false" >> $GITHUB_OUTPUT |
| 207 | + echo "new_tag=$TAG" >> $GITHUB_OUTPUT |
| 208 | + fi |
| 209 | +
|
| 210 | + - name: Download Release Notes |
| 211 | + if: steps.check_tag.outputs.tag_exists == 'false' |
| 212 | + uses: actions/download-artifact@v5 |
| 213 | + with: |
| 214 | + name: release-notes |
| 215 | + path: . |
| 216 | + |
| 217 | + - name: Create Git Tag and GitHub Release |
| 218 | + if: steps.check_tag.outputs.tag_exists == 'false' |
| 219 | + run: | |
| 220 | + TAG="${{ steps.check_tag.outputs.new_tag }}" |
| 221 | + |
| 222 | + # Configure git |
| 223 | + git config user.name "github-actions[bot]" |
| 224 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 225 | + |
| 226 | + # Create and push tag |
| 227 | + git tag -a "$TAG" -m "Release $TAG" |
| 228 | + git push origin "$TAG" |
| 229 | + |
| 230 | + # Create GitHub release |
| 231 | + gh release create "$TAG" \ |
| 232 | + --title "Release $TAG" \ |
| 233 | + --notes-file release-notes.md \ |
| 234 | + --generate-notes |
| 235 | + env: |
| 236 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments