|
| 1 | +--- |
| 2 | +description: .NET Guide |
| 3 | +sidebar_position: 9 |
| 4 | +--- |
| 5 | + |
| 6 | +# .NET |
| 7 | + |
| 8 | +Semaphore Ubuntu images include the .NET SDK and PowerShell. |
| 9 | + |
| 10 | +This page shows how to inspect installed versions, build .NET projects, run tests, and speed up pipelines with caching. |
| 11 | + |
| 12 | +## Check installed versions |
| 13 | + |
| 14 | +Use the following commands to inspect available .NET and PowerShell versions: |
| 15 | + |
| 16 | +```bash |
| 17 | +dotnet --info |
| 18 | +dotnet --list-sdks |
| 19 | +dotnet --list-runtimes |
| 20 | +pwsh --version |
| 21 | +``` |
| 22 | + |
| 23 | +## Build a .NET project |
| 24 | + |
| 25 | +The following example restores dependencies and builds a project in Release mode: |
| 26 | + |
| 27 | +```yaml |
| 28 | +version: v1.0 |
| 29 | +name: .NET pipeline |
| 30 | +agent: |
| 31 | + machine: |
| 32 | + type: f1-standard-2 |
| 33 | + os_image: ubuntu2404 |
| 34 | +blocks: |
| 35 | + - name: Build |
| 36 | + task: |
| 37 | + jobs: |
| 38 | + - name: Build application |
| 39 | + commands: |
| 40 | + - checkout |
| 41 | + - dotnet --info |
| 42 | + - dotnet restore |
| 43 | + - dotnet build --configuration Release --no-restore |
| 44 | +``` |
| 45 | +
|
| 46 | +## Run tests |
| 47 | +
|
| 48 | +Use `dotnet test` to execute your test suite: |
| 49 | + |
| 50 | +```yaml |
| 51 | +version: v1.0 |
| 52 | +name: .NET tests |
| 53 | +agent: |
| 54 | + machine: |
| 55 | + type: f1-standard-2 |
| 56 | + os_image: ubuntu2404 |
| 57 | +blocks: |
| 58 | + - name: Test |
| 59 | + task: |
| 60 | + jobs: |
| 61 | + - name: Run tests |
| 62 | + commands: |
| 63 | + - checkout |
| 64 | + - dotnet restore |
| 65 | + - dotnet test --configuration Release --no-restore |
| 66 | +``` |
| 67 | + |
| 68 | +## Cache NuGet packages |
| 69 | + |
| 70 | +Caching NuGet packages can make pipelines faster. |
| 71 | + |
| 72 | +Set `NUGET_PACKAGES` to a project-local directory, then restore and store the cache: |
| 73 | + |
| 74 | +```bash |
| 75 | +export NUGET_PACKAGES=.nuget/packages |
| 76 | +checkout |
| 77 | +cache restore |
| 78 | +dotnet restore |
| 79 | +cache store |
| 80 | +``` |
| 81 | + |
| 82 | +In subsequent jobs, restore the cache before building or testing: |
| 83 | + |
| 84 | +```bash |
| 85 | +export NUGET_PACKAGES=.nuget/packages |
| 86 | +checkout |
| 87 | +cache restore |
| 88 | +dotnet build --configuration Release --no-restore |
| 89 | +``` |
| 90 | + |
| 91 | +## Use PowerShell |
| 92 | + |
| 93 | +PowerShell is available as `pwsh`. |
| 94 | + |
| 95 | +Example: |
| 96 | + |
| 97 | +```yaml |
| 98 | +version: v1.0 |
| 99 | +name: .NET with PowerShell |
| 100 | +agent: |
| 101 | + machine: |
| 102 | + type: f1-standard-2 |
| 103 | + os_image: ubuntu2404 |
| 104 | +blocks: |
| 105 | + - name: Run PowerShell |
| 106 | + task: |
| 107 | + jobs: |
| 108 | + - name: PowerShell script |
| 109 | + commands: |
| 110 | + - checkout |
| 111 | + - pwsh -Command "Get-ChildItem" |
| 112 | + - pwsh -File ./scripts/build.ps1 |
| 113 | +``` |
| 114 | + |
| 115 | +## Build and test in one pipeline |
| 116 | + |
| 117 | +The following pipeline restores dependencies, builds the application, and runs tests: |
| 118 | + |
| 119 | +```yaml |
| 120 | +version: v1.0 |
| 121 | +name: .NET example |
| 122 | +agent: |
| 123 | + machine: |
| 124 | + type: f1-standard-2 |
| 125 | + os_image: ubuntu2404 |
| 126 | +blocks: |
| 127 | + - name: Build and test |
| 128 | + task: |
| 129 | + jobs: |
| 130 | + - name: .NET |
| 131 | + commands: |
| 132 | + - checkout |
| 133 | + - dotnet --info |
| 134 | + - dotnet restore |
| 135 | + - dotnet build --configuration Release --no-restore |
| 136 | + - dotnet test --configuration Release --no-build |
| 137 | +``` |
| 138 | + |
| 139 | +## Example using a solution file |
| 140 | + |
| 141 | +If your repository contains a solution file, you can target it explicitly: |
| 142 | + |
| 143 | +```bash |
| 144 | +dotnet restore MyApp.sln |
| 145 | +dotnet build MyApp.sln --configuration Release --no-restore |
| 146 | +dotnet test MyApp.sln --configuration Release --no-build |
| 147 | +``` |
0 commit comments