|
| 1 | +name: Test and Publish |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + - development |
| 8 | + - 'cicd/**' |
| 9 | + pull_request: |
| 10 | + branches: |
| 11 | + - master |
| 12 | + - development |
| 13 | + - 'cicd/**' |
| 14 | + |
| 15 | +concurrency: |
| 16 | + group: ${{github.workflow}}-${{github.event.pull_request.number || github.ref}} |
| 17 | + cancel-in-progress: true |
| 18 | + |
| 19 | +env: |
| 20 | + # Disable the .NET logo in the console output. |
| 21 | + DOTNET_NOLOGO: true |
| 22 | + # Disable the .NET first time experience to skip caching NuGet packages and speed up the build. |
| 23 | + DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true |
| 24 | + # Disable sending .NET CLI telemetry to Microsoft. |
| 25 | + DOTNET_CLI_TELEMETRY_OPTOUT: true |
| 26 | + |
| 27 | +jobs: |
| 28 | + upload-event-file: |
| 29 | + name: Upload Event File |
| 30 | + runs-on: ubuntu-24.04 |
| 31 | + steps: |
| 32 | + - name: Upload event file |
| 33 | + uses: actions/upload-artifact@v5 |
| 34 | + with: |
| 35 | + name: test-event-file |
| 36 | + path: ${{ github.event_path }} |
| 37 | + retention-days: 1 |
| 38 | + |
| 39 | + # Construct a version suffix including the branch name, build number and commit hash (e.g., "development.58+abcdef12"). |
| 40 | + get-version: |
| 41 | + name: Calculating Version Suffix |
| 42 | + runs-on: ubuntu-24.04 |
| 43 | + outputs: |
| 44 | + version_suffix: ${{ steps.set-vars.outputs.version_suffix }} |
| 45 | + |
| 46 | + steps: |
| 47 | + - uses: actions/checkout@v6 |
| 48 | + |
| 49 | + - id: git-vars |
| 50 | + name: Get git branch information |
| 51 | + shell: bash |
| 52 | + run: | |
| 53 | + echo "##[set-output name=git_branch;]$(echo $GITHUB_REF)" |
| 54 | + echo "::set-output name=git_hash::$(git rev-parse --short HEAD)" |
| 55 | +
|
| 56 | + - id: set-vars |
| 57 | + uses: actions/github-script@v8 |
| 58 | + with: |
| 59 | + script: | |
| 60 | + let runNumber = "${{ github.run_number }}"; |
| 61 | + let gitHash = "${{ steps.git-vars.outputs.git_hash }}"; |
| 62 | + let rawGitRef = "${{ steps.git-vars.outputs.git_branch }}"; |
| 63 | + console.log("rawGitRef: " + rawGitRef); |
| 64 | + let gitRef = rawGitRef.replace(/^refs\/heads\//, "").replace(/^refs\/heads\//, "").replace(/[_//!@#$%&]/g, "-"); |
| 65 | + if(gitRef.indexOf("refs/pull/") === 0) { |
| 66 | + gitRef = "pr-" + gitRef.substring(10, gitRef.lastIndexOf("/")); |
| 67 | + } |
| 68 | + var versSuffix = `${gitRef}.${runNumber}+${gitHash}`; |
| 69 | + console.log(versSuffix); |
| 70 | + core.setOutput("version_suffix", versSuffix); |
| 71 | +
|
| 72 | + # Main build job for compiling all csprojs. |
| 73 | + build: |
| 74 | + name: Build |
| 75 | + runs-on: ubuntu-24.04 |
| 76 | + needs: [get-version] |
| 77 | + timeout-minutes: 10 |
| 78 | + |
| 79 | + steps: |
| 80 | + - uses: actions/checkout@v6 |
| 81 | + |
| 82 | + - name: Setup .NET |
| 83 | + uses: actions/setup-dotnet@v5 |
| 84 | + with: |
| 85 | + dotnet-version: '10.0.x' |
| 86 | + |
| 87 | + # Master branch is just a normal build. |
| 88 | + - name: Build on master branch |
| 89 | + if: ${{github.ref == 'refs/heads/master'}} |
| 90 | + run: | |
| 91 | + dotnet restore AvaloniaHex.slnx |
| 92 | + dotnet build AvaloniaHex.slnx ` |
| 93 | + --configuration Release |
| 94 | +
|
| 95 | + # For non-master branches (e.g., development or feature branches) we apply a fixup on the version suffix. |
| 96 | + - name: Build on non-master branch |
| 97 | + if: ${{github.ref != 'refs/heads/master'}} |
| 98 | + run: | |
| 99 | + dotnet restore AvaloniaHex.slnx ` |
| 100 | + --configuration Release ` |
| 101 | + --property:VersionSuffix=${{needs.get-version.outputs.version_suffix}} |
| 102 | +
|
| 103 | + dotnet build AvaloniaHex.slnx ` |
| 104 | + --configuration Release ` |
| 105 | + --property:VersionSuffix=${{needs.get-version.outputs.version_suffix}} |
| 106 | +
|
| 107 | + - name: Upload Build Artifacts |
| 108 | + uses: actions/upload-artifact@v5 |
| 109 | + with: |
| 110 | + name: build-artifacts |
| 111 | + path: artifacts/ |
| 112 | + retention-days: 7 |
| 113 | + |
| 114 | + # Multiplexed test job running all tests on different architectures. |
| 115 | + test: |
| 116 | + name: Test |
| 117 | + needs: [get-version, build] |
| 118 | + runs-on: ubuntu-24.04 |
| 119 | + timeout-minutes: 20 |
| 120 | + |
| 121 | + steps: |
| 122 | + - uses: actions/checkout@v6 |
| 123 | + |
| 124 | + - name: Setup .NET |
| 125 | + uses: actions/setup-dotnet@v5 |
| 126 | + with: |
| 127 | + dotnet-version: '10.0.x' |
| 128 | + |
| 129 | + - name: Download Build Artifacts |
| 130 | + uses: actions/download-artifact@v6 |
| 131 | + with: |
| 132 | + name: build-artifacts |
| 133 | + path: artifacts/ |
| 134 | + |
| 135 | + - name: Run Tests |
| 136 | + shell: pwsh |
| 137 | + run: | |
| 138 | + & dotnet test AvaloniaHex.slnx ` |
| 139 | + --configuration Release ` |
| 140 | + --property:VersionSuffix=${{needs.get-version.outputs.version_suffix}} ` |
| 141 | + --logger "trx" ` |
| 142 | + --logger "console;verbosity=minimal" |
| 143 | +
|
| 144 | + # Collect all trx files and upload them. |
| 145 | + - name: Upload Test Results |
| 146 | + uses: actions/upload-artifact@v5 |
| 147 | + if: always() |
| 148 | + with: |
| 149 | + name: test-results-${{matrix.runner.image}}-${{matrix.runner.arch}} |
| 150 | + path: '**/*.trx' |
| 151 | + if-no-files-found: 'warn' |
| 152 | + retention-days: 1 |
| 153 | + |
| 154 | + |
| 155 | + # Job to publish to nuget feeds. |
| 156 | + publish: |
| 157 | + name: Publish NuGet Packages |
| 158 | + runs-on: ubuntu-24.04 |
| 159 | + needs: [test] |
| 160 | + if: ${{github.ref == 'refs/heads/master'}} |
| 161 | + |
| 162 | + steps: |
| 163 | + - uses: actions/checkout@v6 |
| 164 | + |
| 165 | + - name: Setup .NET |
| 166 | + uses: actions/setup-dotnet@v5 |
| 167 | + with: |
| 168 | + dotnet-version: 10.0.x |
| 169 | + |
| 170 | + - name: Download Build Artifacts |
| 171 | + uses: actions/download-artifact@v6 |
| 172 | + with: |
| 173 | + name: build-artifacts |
| 174 | + path: artifacts/ |
| 175 | + |
| 176 | + - name: Push to NuGet |
| 177 | + if: ${{github.ref == 'refs/heads/master'}} |
| 178 | + shell: pwsh |
| 179 | + env: |
| 180 | + NUGET_API_KEY: ${{secrets.NUGET_API_KEY}} |
| 181 | + run: dotnet nuget push "./artifacts/**/*.nupkg" -k "$env:NUGET_API_KEY" -s https://api.nuget.org/v3/index.json --skip-duplicate |
0 commit comments