# Deploying to IIS from GitHub Actions without opening WinRM to the internet

Source: https://www.jawsdeploy.net/blog/iis-deploy-github-actions

Build in GitHub Actions, then let an agent on the Windows server pull the release and deploy it to IIS. No inbound ports, a folder per release, and rollback by re-deploying.

GitHub Actions is good at building .NET. The awkward part is the last step: getting the build onto a Windows server running IIS. GitHub's hosted runners live on the public internet, and your web server probably shouldn't accept connections from it.

This post walks through a setup that keeps the server closed to inbound traffic. GitHub Actions builds and packages the app, and an agent on the server pulls the package and deploys it to IIS. We make [Jaws Deploy](https://www.jawsdeploy.net/go/iis-windows-deployments), so the examples use it, but the pattern works with any tool that uses an outbound agent.

## The usual options, and what they cost

Most guides for deploying to IIS from GitHub Actions use one of three approaches.

- **Web Deploy or WinRM from a hosted runner.** The runner connects to the server, so the server has to accept connections on port 8172 (Web Deploy) or 5985/5986 (WinRM). GitHub's runner IP ranges are large and change, so the firewall rule usually ends up wide. The repository secrets then hold credentials for a server administrator.
- **A self-hosted runner on the web server.** The runner connects outbound, so no inbound ports are needed. The catch is that the production server now runs whatever the workflow file says. Anyone who can change a workflow on a branch that triggers it can run code on that machine, and GitHub's own documentation advises against self-hosted runners on public repositories.
- **Copying files over FTP or a file share.** This is simple until you need an app pool, bindings, a different connection string per environment, or a deployment that doesn't fail on a locked DLL.

For one server and a small team that trusts everyone with write access, a self-hosted runner can be a reasonable choice. Past that, the problems tend to show up around the second environment or the third server.

## Split building from deploying

The setup below separates the two jobs:

1. GitHub Actions builds, tests and packages the app, as it does now.
2. It uploads the package and tells the deployment tool that a new release exists.
3. An agent on the IIS server, which keeps an outbound connection open, receives the deployment, pulls the package and runs the deployment steps.

The server needs outbound HTTPS and nothing else. It runs only the steps defined in the deployment project, not code from the repository's workflow files. And the decision to promote a release to production is made in the deployment tool, where you can see what is running where.

## The workflow

This workflow runs on every push to `main`. It publishes the site, zips it, pushes the zip to the [built-in package store](https://www.jawsdeploy.net/guides/built-in-package-store) and creates a release.

```yaml
name: build-and-release

on:
  push:
    branches: [main]

jobs:
  build:
    runs-on: windows-latest
    env:
      VERSION: 1.0.${{ github.run_number }}
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '10.0.x'

      - run: dotnet test --configuration Release

      - run: dotnet publish src/Shop.Web -c Release -o out/Shop.Web

      - name: Package
        shell: pwsh
        run: Compress-Archive -Path out/Shop.Web/* -DestinationPath "Shop.Web.$env:VERSION.zip"

      - name: Push package and create release
        shell: pwsh
        env:
          JAWS_API_LOGIN: ${{ secrets.JAWS_SERVICE_ACCOUNT_ID }}
          JAWS_API_PASSWORD: ${{ secrets.JAWS_API_KEY }}
        run: |
          $credential = [pscredential]::new($env:JAWS_API_LOGIN,
            (ConvertTo-SecureString $env:JAWS_API_PASSWORD -AsPlainText -Force))

          Invoke-RestMethod -Method Post `
            -Uri "https://app.jawsdeploy.net/api/packagestore/package" `
            -Authentication Basic -Credential $credential `
            -Form @{
              WorkspaceId = "${{ vars.JAWS_WORKSPACE_ID }}"
              PackageFile = Get-Item "Shop.Web.$env:VERSION.zip"
            }

          $release = @{
            projectId       = "${{ vars.JAWS_PROJECT_ID }}"
            version         = $env:VERSION
            packageVersions = @{ "Shop.Web" = $env:VERSION }
          } | ConvertTo-Json

          Invoke-RestMethod -Method Post `
            -Uri "https://app.jawsdeploy.net/api/release" `
            -Authentication Basic -Credential $credential `
            -ContentType "application/json" -Body $release
```

A few details worth knowing:

- The package ID and version come from the file name, so `Shop.Web.1.0.42.zip` becomes package `Shop.Web`, version `1.0.42`.
- The credentials belong to a service account, not a person. Give it the right to push packages and create releases, and if the workflow should also deploy, the right to deploy to the development environment only. Deploy permission can be granted per environment, so a leaked key can't push to production.
- To deploy to the first environment straight away, `Invoke-ReleaseAndDeployProject` in the [PowerShell SDK](https://www.jawsdeploy.net/guides/integrating-github-actions) creates the release, deploys it and prints the deployment log in the workflow.

## The deployment project

On the Jaws Deploy side, the project has two steps, plus an optional third.

**1. Deploy package.** Set the installation directory to a folder per release:

```text
D:\Sites\Shop\#{CONTEXT.ReleaseVersion}
```

Each release gets a new, empty folder. Nothing overwrites the files the site is serving, so a locked DLL can't fail the deployment, and the previous version is still on disk. The same step replaces `#{...}` tokens in your config files with values for the environment being deployed, so one package serves every environment ([variable replacement in files](https://www.jawsdeploy.net/guides/variable-replacement-in-files) has the details):

```json
{
  "ConnectionStrings": {
    "Shop": "#{ShopDbConnection}"
  },
  "Payments": {
    "BaseUrl": "#{PaymentsBaseUrl}"
  }
}
```

**2. Deploy IIS website.** Fill in the site name, the app pool, the bindings and the HTTPS certificate, and set the physical path to the same folder as step 1. The step creates the site and its app pool if they don't exist, and updates them if they do. It retries when IIS's configuration file is locked, and two deployments on the same machine take turns changing IIS rather than corrupting each other's changes.

When the physical path changes, IIS starts the app from the new folder. Expect the usual warm-up on the first request.

**3. Clean up old folders (optional).** A folder per release adds up. A short PowerShell step at the end keeps the last few and never touches the live one:

```powershell
$root = 'D:\Sites\Shop'
$live = Join-Path $root '#{CONTEXT.ReleaseVersion}'

Get-ChildItem $root -Directory |
  Where-Object FullName -ne $live |
  Sort-Object CreationTime -Descending |
  Select-Object -Skip 4 |
  Remove-Item -Recurse -Force
```

Values that differ between environments, such as host names, connection strings and the certificate thumbprint, go into project variables scoped to Staging and Production. Store passwords and keys as secret variables, which are masked in the deployment log.

## More than one server

Behind a load balancer, you don't want every server switching at the same moment. Put the steps in a [rolling group](https://www.jawsdeploy.net/guides/rolling-deployments) with a window of 1. Jaws Deploy then runs the whole group on one machine, then moves to the next, and stops if a machine fails. You can also put a canary machine first. If your load balancer needs the server taken out of rotation first, add a script step for that at the start of the group.

## Rolling back

To roll back, re-deploy the previous release. The same steps run: the package goes back into its own folder and the IIS step points the site at it. There is no separate rollback procedure to keep working.

One caveat: this rolls back files and IIS settings, not your database. If a release ran a migration that the old version can't work with, re-deploying won't undo it. Keep migrations backwards compatible for at least one release, or plan that rollback separately.

## What the server needs

[Install the agent](https://www.jawsdeploy.net/guides/installing-jaws-deploy-agent) with the script from the Add machine page. It runs as a Windows service and needs outbound HTTPS (port 443) to `app.jawsdeploy.net` and the hub it connects to. Allowing `*.jawsdeploy.net` covers both. No inbound ports, no WinRM, no Web Deploy service. Machines without internet access can install it offline, and teams that can't use a cloud service at all can run [Jaws Deploy Stack](https://www.jawsdeploy.net/platform/stack) on their own servers.

## Try it on one server

The free plan covers two machines with no time limit, so you can try this on a staging server before changing anything in production. The first IIS deployment usually takes about half an hour, most of it filling in the IIS step.

