An IIS project that targets net10.0-windows builds and tests on Windows, so its pipeline asks for a Windows runner by tag instead of naming the Linux SDK image that builds the container images. A second job copies the published site onto IIS, and the running app is what makes that copy harder than it looks.
The job on the Windows runner
This is .gitlab-ci.yml in the IIS project:
build:
tags: [windows]
stage: build
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- dotnet test App.sln -c Release --logger "junit;LogFilePath=$env:CI_PROJECT_DIR/test-results/{assembly}.xml"
- dotnet publish Web/Web.csproj -c Release -o artifacts/web
artifacts:
reports:
junit: test-results/*.xml
paths: [artifacts/]
expire_in: 1 week
tags: [windows] sends the job only to a runner registered with that tag, and a Windows runner that uses the shell executor, with the .NET SDK installed, runs both commands on the host. The script tests the solution first, so a failing test stops the job before dotnet publish writes the web project into artifacts/web. The reports: junit line fills the merge request's tests panel the same way it does in Pipelines for Branches and Merge Requests. The script runs in PowerShell, so the log path reads the project directory as $env:CI_PROJECT_DIR; a bare $CI_PROJECT_DIR there is an unset PowerShell variable and expands to nothing. Under Windows PowerShell 5.1 that line wrote one file per test project into test-results.
Rules for a smaller runner pool
The rules differ from that article on purpose. The container pipeline runs on every branch push, while this job runs only for merge requests and for the default branch, because a Windows runner pool is often one or two machines and a push to a branch with no merge request would queue a build nobody reviews. Limiting branch pipelines to the default branch also means a push never produces both a branch pipeline and a merge request pipeline for the same commit, since merge requests come from other branches.
Two runner pools
The container pipeline's jobs set image: mcr.microsoft.com/dotnet/sdk:10.0 and no tag, because any runner with a Docker executor can start that image. This job sets a tag and no image, because the .NET SDK is installed on the Windows runner itself. Those are two separate runner pools, so a green container pipeline says nothing about whether a windows runner is online. When no registered runner carries the tag, the job stays pending instead of failing, and The Git Server and the Runner covers runner tags and what that pending state points at.
A published folder kept for a week
The artifact is the output of dotnet publish: the web project's compiled site in artifacts/web, ready to copy into an IIS site folder. GitLab keeps it for one week, which is long enough for a person to download it from the job page or for a later job to pick it up. Copying it onto a server is the work of a separate deploy job, and A Publish Job Is Not a Rollout is why that job is its own step.
A deploy job that copies onto the running site
The deploy job below assumes a second Windows runner installed on the IIS server itself and tagged iis, so the site folder is a local path. It takes the folder build kept and copies it into the site:
deploy:
stage: deploy
tags: [iis]
needs: [build]
environment:
name: production
url: https://app.example.com
resource_group: production
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: manual
script:
- .\deploy\Deploy-Site.ps1 -Source artifacts\web -SitePath $env:DEPLOY_PATH
needs: [build] downloads the build job's artifacts, so artifacts\web is there when the script starts. DEPLOY_PATH is a CI/CD variable scoped to the production environment, such as D:\sites\app. deploy is one of GitLab's default stages, so the file needs no stages: list. The environment, resource_group and manual rule do the same work as in A Publish Job Is Not a Rollout: a recorded deployment, one deploy at a time, and a button on default-branch pipelines only.
The copy lives in deploy/Deploy-Site.ps1 in the repository rather than in the YAML:
param(
[Parameter(Mandatory)] [string] $Source,
[Parameter(Mandatory)] [string] $SitePath
)
$offline = Join-Path $SitePath "app_offline.htm"
Set-Content -Path $offline -Value "<p>Updating. Back in a minute.</p>" -Encoding ASCII
robocopy $Source $SitePath /MIR /XF app_offline.htm /XD logs /R:15 /W:1 /NP /NFL /NDL
if ($LASTEXITCODE -ge 8) {
Write-Host "robocopy failed with exit code $LASTEXITCODE; app_offline.htm stays in place"
exit 1
}
Remove-Item $offline
exit 0
Each line answers something a plain copy got wrong.
The running app holds its DLL
The web.config that dotnet publish writes sets hostingModel="inprocess", so the app runs inside the IIS worker process, and that process keeps Web.dll open. Copying a new build over a running site with robocopy /MIR failed on that one file with ERROR 32 ... being used by another process, gave up after its retries and exited with 9. It had already copied the other changed files, so the site folder held the new web.config and Web.deps.json next to the old Web.dll, and the next request had its connection closed with no response. A failed copy onto a live site is half a deploy, not no deploy.
app_offline.htm stops the app first
The ASP.NET Core Module watches the site folder for a file named app_offline.htm. When it appears, the module shuts the app down and the DLLs are released. In the run, Web.dll could be opened for writing about 2.4 seconds after the file was written. The script does not sleep for a fixed time: robocopy starts at once, and /R:15 /W:1 retries a locked file once a second for up to 15 seconds, which covered that gap. Once the script removes the file, the next start of the app loads the new build, and it answered with version 2. Under full IIS that start is the next request; under IIS Express in the run, the process was started again by hand.
/MIR deletes what the artifact does not have
/MIR makes the site folder match the artifact, which removes files a previous build left behind, and also anything else in the folder. Without /XF app_offline.htm, robocopy listed the file as *EXTRA File and deleted it in the same run that copied the DLLs, so the site came back online before the script reached its own last line. /XF keeps it until the script removes it. /XD logs does the same for the folder that stdoutLogFile=".\logs\stdout" in the generated web.config writes to; add any other folder the site writes into, such as uploads.
robocopy exit codes are bit flags
robocopy exits with 0 when nothing needed copying, 1 when files were copied, 2 when extra files were found, 3 for both, and 8 or more when something failed. GitLab Runner's PowerShell shell checks $? after every script line, and Windows PowerShell 5.1 sets $? to false after any native command with a non-zero exit code. Run as a bare job line with that check after it, a successful copy exited with 3 and the check ended the job with 3. The script tests $LASTEXITCODE -ge 8 itself and ends with an explicit exit 0 or exit 1, so the line the runner sees passes or fails on what actually happened.
A failed copy leaves the site offline
When robocopy fails, the script leaves app_offline.htm in place and fails the job. Under full IIS, visitors get that page instead of the mix of old and new files from the first run, and the next run of the job, or a fix to the folder, brings the site back. With a source folder that did not exist, robocopy exited with 16, the job line exited with 1, and the file stayed.
The same job from a CI component
When more than one IIS project needs this job, it can move into a CI component that each project includes. A component lives in its own project, here example/ci-components, as templates/dotnet-windows.yml:
spec:
inputs:
web_project:
runner_tag:
default: windows
---
build:
tags:
- $[[ inputs.runner_tag ]]
stage: build
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- dotnet test App.sln -c Release --logger "junit;LogFilePath=$env:CI_PROJECT_DIR/test-results/{assembly}.xml"
- dotnet publish $[[ inputs.web_project ]] -c Release -o artifacts/web
artifacts:
reports:
junit: test-results/*.xml
paths: [artifacts/]
expire_in: 1 week
The spec: header declares two inputs, and the --- line separates it from the job. $[[ inputs.runner_tag ]] and $[[ inputs.web_project ]] are replaced when the pipeline is created, so the test logger and the rules block are written once while each project still supplies its own path and, if it needs one, its own runner tag. Each IIS project's .gitlab-ci.yml then includes the component by version:
include:
- component: $CI_SERVER_FQDN/example/ci-components/[email protected]
inputs:
web_project: Web/Web.csproj
Because inputs are fixed at pipeline creation, the resolved job still carries a windows tag and still waits for a Windows runner.
Shared or not, the build job ends with the web project built on a Windows runner and its output kept as an artifact for a week. The deploy job puts that folder on IIS behind app_offline.htm, and none of this runs on the Linux runner that publishes the container images.
What a run showed
The build used the .NET 10.0.15 runtime, and a small net10.0 web app was published twice, as version 1 and version 2. The site ran in-process under IIS Express with ASP.NET Core Module V2, from a folder on the same machine, on Windows 10 with Windows PowerShell 5.1. Each job line was run with the same if(!$?) check after it that GitLab Runner's PowerShell shell appends.
| Check | Result |
|---|---|
robocopy /MIR over the running site |
ERROR 32 on Web.dll, exit 9; the other changed files copied; the next request's connection closed |
app_offline.htm written, then Web.dll opened for writing |
Possible after about 2.4 s; the worker process had exited |
robocopy /MIR without /XF app_offline.htm |
app_offline.htm deleted as *EXTRA File during the copy |
Deploy-Site.ps1 against the running site |
Line passed with exit 0; app_offline.htm gone; the next start served version 2 |
A file in logs\ with /XD logs |
Kept |
A bare robocopy line that copied files |
Exit 3; the $? check ended the job with 3 |
Deploy-Site.ps1 with a missing source folder |
robocopy exit 16; job line exit 1; app_offline.htm stayed |
Not run: full IIS, where the worker process is w3wp.exe and visitors see the app_offline.htm page while the copy runs (IIS Express is itself the worker process, so it exited instead of serving the page), a real GitLab Windows runner, out-of-process hosting, and a copy to a UNC path on another server.