Automated Deployment of .NET Application to Windows IIS Server
Automated Deployment of .NET Application to Windows IIS Server
Automated Deployment of .NET Application to Windows IIS Server
2
Step 1: Install GitLab Runner on Windows IIS Server
3
Step 2: Configure GitLab Runner on the Windows IIS server
Enter the registration token -> type the token which you can find
here(Your GitLab repository -> Settings -> CI/CD -> Runners -> Three
dots -> Copy registration token)
Enter a description for the runner -> Type some description for the
runner
Enter tags for the runner -> Type some tags for the runner(Remember
these!)
Enter optional maintenance note for the runner -> This is optional
And at the end, you will see such output like this one:
Here you need to provide which executor you want to use. I suggest
typing “shell” here but you can modify this later.
4
Navigate to “C:\GitLab-Runner” and ensure the presence of the following
files:
executor = "shell"
shell = "powershell"
Define 3 stages:
stages:
- update
- build
- move_files
5
The third one will move our files to the corresponding folder in “C://Websites” to make sure IIS
can locate it.
update_repository:
stage: update
script:
- powershell -Command "if (-Not (Test-Path
'C:\\Users\\nikola.perisic\\Desktop\\infection-index')) { git clone
https://git.URL.git
C:\\Users\\nikola.perisic\\Desktop\\infection-index } else { cd
'C:\\Users\\nikola.perisic\\Desktop\\infection-index'; git fetch
--all; git reset --hard origin/development; git clean -fd }"
tags:
- ci
Explanation:
This script is part of a CI/CD pipeline that updates a local copy of a Git repository. Here's a
simplified breakdown:
Stage name: The stage is called update, indicating the purpose of this step in the pipeline.
Script: The script runs a PowerShell command that checks if a specific folder
(infection-index) exists on the user's desktop.
● If the folder doesn't exist, it clones the Git repository from the specified URL to that
location.
● If the folder already exists, it navigates to that folder, fetches all updates from the remote
repository, resets the local files to match the development branch, and cleans up any
untracked files.
Tags: The script is tagged with ci. As I said before, the tag is important to make the runner able
to detect which (continue)
6
build_application:
stage: build
script:
- powershell -Command "cd
'C:\\Users\\nikola.perisic\\Desktop\\infection-index'"
- powershell -Command "git checkout development"
- powershell -Command "dotnet publish
'C:\\Users\\nikola.perisic\\Desktop\\infection-index\\MSK.Infection
Index' -c Release -o
'C:\\Users\\nikola.perisic\\Desktop\\infection-index\\publish'"
tags:
- ci
Explanation:
This script is another part of a CI/CD pipeline, focused on building an application. Here’s a
beginner-friendly explanation:
Stage name: The stage is named build, indicating that this step handles the application's build
process.
Script: The script runs several PowerShell commands in sequence:
● It changes the directory to the local repository folder (infection-index) on the user's
desktop.
● It checks out the development branch in the Git repository, ensuring that the correct
branch is being used.
● It then runs the dotnet publish command to build the application located in the
MSK.InfectionIndex folder. The build is configured for the Release mode, and the
output is placed in the publish folder within the repository.
This code automates the process of building the application from a specific branch and prepares
the compiled files for deployment.
7
The third stage(job)
move_files:
stage: move_files
script:
- powershell -Command "if (Test-Path
'C:\\Websites\\galactic-resistance.com') { Remove-Item -Path
'C:\\Websites\\galactic-resistance.com\\*' -Recurse -Force }"
- powershell -Command "New-Item -Path
'C:\\Websites\\galactic-resistance.com' -ItemType Directory -Force"
- powershell -Command "Copy-Item -Path
'C:\\Users\\nikola.perisic\\Desktop\\infection-index\\publish\\*'
-Destination 'C:\\Websites\\galactic-resistance.com' -Recurse
-Force"
- powershell -Command "Copy-Item -Path
'C:\\Users\\nikola.perisic\\Desktop\\appsettings.Development.json'
-Destination
'C:\\Websites\\galactic-resistance.com\\appsettings.Development.jso
n' -Force"
- powershell -Command "Copy-Item -Path
'C:\\Users\\nikola.perisic\\Desktop\\appsettings.json' -Destination
'C:\\Websites\\galactic-resistance.com\\appsettings.json' -Force"
- powershell -Command "Copy-Item -Path
'C:\\Users\\nikola.perisic\\Desktop\\web.config' -Destination
'C:\\Websites\\galactic-resistance.com\\web.config' -Force"
tags:
- ci
This script is part of a CI/CD pipeline, focused on moving and setting up files for deployment.
Here’s what it does:
8
the desktop to the galactic-resistance.com folder, ensuring the deployment
includes the correct settings.
Note: I configured these files once on the server and saved them on the Desktop. Of course,
you can configure them locally, and therefore publish would generate those files well, but I had
another local environment and I didn't want to mix the configuration files or run the risk of
someone changing them and accidentally pushing the wrong ones, because that would
automatically affect the production environment.
This code automates the process of preparing a deployment directory by moving the necessary
application files and configurations into place.
9
Possible problem
____________________________________________________________________________
Privilege issue
To be able to move(copy) files and folders be sure to give all the necessary permissions.
10
A more complex example of pipeline code
1. Defining variables
variables:
USERNAME: "nikola.perisic"
BASE_PATH: "C:\\Users\\${USERNAME}\\Desktop\\infection-index"
PUBLISH_PATH: "${BASE_PATH}\\publish"
WEBSITE_PATH: "C:\\Websites\\galactic-resistance.com"
APPSETTINGS_DEV:
"C:\\Users\\${USERNAME}\\Desktop\\appsettings.Development.json"
APPSETTINGS: "C:\\Users\\${USERNAME}\\Desktop\\appsettings.json"
WEB_CONFIG: "C:\\Users\\${USERNAME}\\Desktop\\web.config"
2. Defining stages(jobs)
stages:
- update
- build
- move_files
3. Defining the first job - The script checks if a specified directory (${BASE_PATH}) exists.
If it doesn't, it clones a Git repository into that directory. If the directory exists, it updates
the repository by fetching, resetting to a specific branch (development), and cleaning
untracked files.
update_repository:
stage: update
script:
- powershell -Command "if (-Not (Test-Path '${BASE_PATH}')) { git clone
https://URL.git '${BASE_PATH}' } else { cd '${BASE_PATH}'; git fetch --all; git
reset --hard origin/development; git clean -fd }"
tags:
- ci
11
4. Defining the second job - The pipeline job checks out the development branch, then
publishes a .NET application from the specified directory (${BASE_PATH}) to a release
folder (${PUBLISH_PATH}).
build_application:
stage: build
script:
- powershell -Command "cd '${BASE_PATH}'"
- powershell -Command "git checkout development"
- powershell -Command "dotnet publish '${BASE_PATH}\\MSK.InfectionIndex' -c
Release -o '${PUBLISH_PATH}'"
tags:
- ci
5. Defining the third job - The job stops the web app pool, cleans and recreates the target
directory (${WEBSITE_PATH}), copies the published files and configuration files to this
directory, and then restarts the web app pool.
move_files:
stage: move_files
script:
- powershell -Command "Import-Module WebAdministration"
- powershell -Command "Stop-WebAppPool -Name
'galactic-resistance.sitesstage.com'"
- powershell -Command "if (Test-Path '${WEBSITE_PATH}') { Remove-Item -Path
'${WEBSITE_PATH}\\*' -Recurse -Force }"
- powershell -Command "New-Item -Path '${WEBSITE_PATH}' -ItemType Directory
-Force"
- powershell -Command "Copy-Item -Path '${PUBLISH_PATH}\\*' -Destination
'${WEBSITE_PATH}' -Recurse -Force"
- powershell -Command "Copy-Item -Path '${APPSETTINGS_DEV}' -Destination
'${WEBSITE_PATH}\\appsettings.Development.json' -Force"
- powershell -Command "Copy-Item -Path '${APPSETTINGS}' -Destination
'${WEBSITE_PATH}\\appsettings.json' -Force"
- powershell -Command "Copy-Item -Path '${WEB_CONFIG}' -Destination
'${WEBSITE_PATH}\\web.config' -Force"
- powershell -Command "Start-WebAppPool -Name
'galactic-resistance.sitesstage.com'"
tags:
- ci
12