|
| 1 | +# This workflow will build a .NET project |
| 2 | +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net |
| 3 | + |
| 4 | +name: Build .NET lib |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: [ "develop" ] |
| 9 | + pull_request: |
| 10 | + branches: [ "main" ] |
| 11 | + |
| 12 | +env: |
| 13 | + PRODUCT_NAME: TeamTools.Linter.CommandLine |
| 14 | + OUTPUT_PATH: ${{ github.workspace }}/.bin/ |
| 15 | + |
| 16 | +jobs: |
| 17 | + semver: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + outputs: |
| 20 | + next-version: ${{ steps.next-ver.outputs.VERSION }} |
| 21 | + |
| 22 | + steps: |
| 23 | + - uses: actions/checkout@v4 |
| 24 | + |
| 25 | + - name: Prepare next version |
| 26 | + id: next-ver |
| 27 | + run: | |
| 28 | + VERSION=$(date +%Y.%m.${{ github.run_number }}) |
| 29 | + echo Next version: $VERSION |
| 30 | + echo "VERSION=$VERSION" >> $GITHUB_OUTPUT |
| 31 | + |
| 32 | + build: |
| 33 | + name: Build and test |
| 34 | + needs: semver |
| 35 | + runs-on: ${{ matrix.os }} |
| 36 | + |
| 37 | + defaults: |
| 38 | + run: |
| 39 | + shell: bash |
| 40 | + |
| 41 | + strategy: |
| 42 | + fail-fast: false |
| 43 | + matrix: |
| 44 | + os: [windows-latest, ubuntu-latest] |
| 45 | + configuration: [Debug, Release] |
| 46 | + include: |
| 47 | + - os: windows-latest |
| 48 | + configuration: Debug |
| 49 | + coverage: true |
| 50 | + |
| 51 | + steps: |
| 52 | + - uses: actions/checkout@v4 |
| 53 | + |
| 54 | + - name: Setup .NET |
| 55 | + uses: actions/setup-dotnet@v4 |
| 56 | + with: |
| 57 | + dotnet-version: | |
| 58 | + 3.1.x |
| 59 | + 6.0.x |
| 60 | + 8.0.x |
| 61 | +
|
| 62 | + - name: Display dotnet version |
| 63 | + run: dotnet --info |
| 64 | + |
| 65 | + - name: Cache NuGet packages |
| 66 | + id: cache-nugets |
| 67 | + uses: actions/cache@v4 |
| 68 | + with: |
| 69 | + path: ${{ github.workspace }}/packages |
| 70 | + key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json', '**/*.csproj', 'Directory.Build.props') }} # Unique key for the cache |
| 71 | + restore-keys: | |
| 72 | + ${{ runner.os }}-nuget- |
| 73 | + |
| 74 | + - name: Install coverlet |
| 75 | + run: | |
| 76 | + dotnet add package coverlet.msbuild --version 6.0.4 && |
| 77 | + dotnet add package coverlet.collector --version 6.0.4 |
| 78 | + working-directory: TeamTools.Linter.CommandLineTests |
| 79 | + |
| 80 | + - name: Restore dependencies |
| 81 | + # if: ${{ steps.cache-nugets.outputs.cache-hit != 'true' }} |
| 82 | + run: dotnet restore --p:ContinuousIntegrationBuild=true |
| 83 | + |
| 84 | + - name: Build |
| 85 | + id: build |
| 86 | + run: > |
| 87 | + dotnet build --no-restore |
| 88 | + --configuration ${{ matrix.configuration }} |
| 89 | + -p:VersionPrefix=${{ needs.semver.outputs.next-version }} |
| 90 | + -p:ContinuousIntegrationBuild=true |
| 91 | + -p:SourceRevisionId=${{ github.sha }} |
| 92 | + -p:RepositoryUrl="${{ github.repositoryUrl }}" |
| 93 | + -p:RepositoryType=git |
| 94 | + -p:RepositoryBranch="${{ github.ref_name }}" |
| 95 | + -p:BaseOutputPath="${{ env.OUTPUT_PATH }}" |
| 96 | +
|
| 97 | + - name: Test |
| 98 | + if: ${{ !matrix.coverage }} |
| 99 | + run: > |
| 100 | + dotnet test --no-build --verbosity normal |
| 101 | + -p:BaseOutputPath="${{ env.OUTPUT_PATH }}" |
| 102 | + --configuration ${{ matrix.configuration }} |
| 103 | + --logger "trx;LogFileName=${{ github.workspace }}/TestResults/test-results.trx" |
| 104 | +
|
| 105 | + - name: Test with coverage |
| 106 | + if: ${{ matrix.coverage }} |
| 107 | + run: > |
| 108 | + dotnet test --no-build --verbosity normal |
| 109 | + -p:CollectCoverage=true -p:CoverletOutput="${{ github.workspace }}/TestResults/" -p:CoverletOutputFormat=opencover /p:ExcludeByFile="**/TeamTools.TSQL.Common/**/*.cs" |
| 110 | + -p:BaseOutputPath="${{ env.OUTPUT_PATH }}" |
| 111 | + --configuration ${{ matrix.configuration }} |
| 112 | + --logger "trx;LogFileName=${{ github.workspace }}/TestResults/test-results.trx" |
| 113 | +
|
| 114 | + - name: Upload test artifacts |
| 115 | + uses: actions/upload-artifact@v4 |
| 116 | + if: ${{ matrix.coverage }} |
| 117 | + with: |
| 118 | + name: test-results |
| 119 | + path: TestResults |
| 120 | + |
| 121 | + - name: Test Report |
| 122 | + uses: dorny/test-reporter@v2 |
| 123 | + if: ${{ !cancelled() }} # run this step even if previous step failed |
| 124 | + with: |
| 125 | + name: NUnit testing ${{ matrix.configuration }} build on ${{ matrix.os }} |
| 126 | + path: '**/TestResults/*.trx,*.trx' |
| 127 | + reporter: dotnet-trx |
| 128 | + |
| 129 | + - name: Upload build artifacts 3.1 |
| 130 | + uses: actions/upload-artifact@v4 |
| 131 | + if: ${{ matrix.configuration == 'Release' && matrix.os == 'windows-latest' && steps.build.conclusion == 'success' }} |
| 132 | + with: |
| 133 | + name: ${{ env.PRODUCT_NAME }}-${{ needs.semver.outputs.next-version }}-${{ matrix.configuration }}-netcoreapp3.1 |
| 134 | + path: ${{ env.OUTPUT_PATH }}/${{ matrix.configuration }}/netcoreapp3.1 |
| 135 | + |
| 136 | + - name: Upload build artifacts 6.0 |
| 137 | + uses: actions/upload-artifact@v4 |
| 138 | + if: ${{ matrix.configuration == 'Release' && matrix.os == 'windows-latest' && steps.build.conclusion == 'success' }} |
| 139 | + with: |
| 140 | + name: ${{ env.PRODUCT_NAME }}-${{ needs.semver.outputs.next-version }}-${{ matrix.configuration }}-net6.0 |
| 141 | + path: ${{ env.OUTPUT_PATH }}/${{ matrix.configuration }}/net6.0 |
| 142 | + |
| 143 | + - name: Upload build artifacts 8.0 |
| 144 | + uses: actions/upload-artifact@v4 |
| 145 | + if: ${{ matrix.configuration == 'Release' && matrix.os == 'windows-latest' && steps.build.conclusion == 'success' }} |
| 146 | + with: |
| 147 | + name: ${{ env.PRODUCT_NAME }}-${{ needs.semver.outputs.next-version }}-${{ matrix.configuration }}-net8.0 |
| 148 | + path: ${{ env.OUTPUT_PATH }}/${{ matrix.configuration }}/net8.0 |
| 149 | + |
| 150 | + report: |
| 151 | + needs: build |
| 152 | + if: always() |
| 153 | + runs-on: ubuntu-latest |
| 154 | + |
| 155 | + steps: |
| 156 | + - uses: actions/checkout@v4 |
| 157 | + |
| 158 | + - name: Download test results |
| 159 | + uses: actions/download-artifact@v4 |
| 160 | + with: |
| 161 | + name: test-results |
| 162 | + path: TestResults |
| 163 | + |
| 164 | + - name: Extract coverage info |
| 165 | + uses: simon-k/dotnet-code-coverage-badge@v1.0.0 |
| 166 | + id: create_coverage_badge |
| 167 | + if: ${{ !cancelled() && hashFiles('TestResults/coverage.opencover.xml') != '' }} |
| 168 | + with: |
| 169 | + label: Unit Test Coverage |
| 170 | + color: brightgreen |
| 171 | + path: TestResults/coverage.opencover.xml |
| 172 | + gist-filename: code-coverage.json |
| 173 | + gist-id: ${{ vars.GIST_COVERAGE_ID }} |
| 174 | + gist-auth-token: ${{ secrets.GIST_AUTH_TOKEN }} |
| 175 | + |
| 176 | + - name: Create Coverage Badge |
| 177 | + uses: schneegans/dynamic-badges-action@v1.7.0 |
| 178 | + if: ${{ !cancelled() && hashFiles('TestResults/coverage.opencover.xml') != '' && steps.create_coverage_badge.outputs.percentage != '' }} |
| 179 | + with: |
| 180 | + auth: ${{ secrets.GIST_AUTH_TOKEN }} |
| 181 | + gistID: ${{ vars.GIST_COVERAGE_ID }} |
| 182 | + filename: code-coverage.svg |
| 183 | + label: Coverage |
| 184 | + message: ${{ steps.create_coverage_badge.outputs.percentage }}% |
| 185 | + valColorRange: ${{ steps.create_coverage_badge.outputs.percentage }} |
| 186 | + maxColorRange: 100 |
| 187 | + minColorRange: 0 |
| 188 | + |
| 189 | + - name: Print code coverage |
| 190 | + if: steps.create_coverage_badge.outcome == 'success' |
| 191 | + run: echo "Code coverage percentage ${{steps.create_coverage_badge.outputs.percentage}}%" |
0 commit comments