Skip to content

Instantly share code, notes, and snippets.

@luuksommers
Created May 31, 2022 18:50
Show Gist options
  • Save luuksommers/7dd10db8435d7d3624343021abecd53d to your computer and use it in GitHub Desktop.
Save luuksommers/7dd10db8435d7d3624343021abecd53d to your computer and use it in GitHub Desktop.
Gitlab CI dotnet iis
# This file shows how to setup the template for your projects
include:
- project: 'templates'
file: 'gitlab-templates/dotnet-framework.v1.gitlab-ci.yml'
variables:
ENABLE_UNITTESTS: 'true'
DEFAULT_IIS_SITENAME: "my-awesome-deployment.nl" # For dev deploys this gets prefixed with 'development.', for prod it gets prefixed with 'www.'
DEFAULT_DEVELOPMENT_URL: "https://my-awesome-deployment.devserver.nl/"
DEFAULT_PRODUCTION_URL: "https://www.my-awesome-deployment.nl/"
# If you want to do some additional things during the default steps,
# for example build node stuff, you can do that in the 'before_script'
build_debug:
cache:
key: npm-cache
paths:
- '$env:CI_PROJECT_DIR\\.npm\\'
before_script:
- 'npm ci --cache $env:CI_PROJECT_DIR\\.npm --prefer-offline'
- 'npx lerna bootstrap -- --cache $env:CI_PROJECT_DIR\\.npm --prefer-offline'
- 'npx lerna run test'
- 'npx lerna run build'
# Or you can overrule the variables for the seperate steps
deploy_production:
variables:
IIS_SERVER: 'overruled-prodserver.nl'
include:
- project: 'templates' # refer to the base template of dotnet-framework.v1.gitlab-ci.yml
file: 'gitlab-templates/dotnet-framework.v1.gitlab-ci.yml'
build_debug:
stage: build
script:
- 'dotnet restore'
- 'dotnet build --nologo --no-restore --verbosity quiet /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:PackageLocation="$CI_PROJECT_DIR\$PACKAGE_TO_DEPLOY" /p:Configuration=Debug /p:EnvironmentName=Staging'
# INSTALL https://www.nuget.org/packages/JUnitXml.TestLogger/ into the test projects
test:
script:
- 'dotnet restore'
- 'dotnet test --no-restore --test-adapter-path:. --logger:"junit;LogFilePath=..\..\artifacts\{assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"'
build_release:
stage: build
script:
- 'dotnet restore'
- 'dotnet build --nologo --no-restore --verbosity quiet /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:PackageLocation="$CI_PROJECT_DIR\$PACKAGE_TO_DEPLOY" /p:Configuration=Release /p:EnvironmentName=Production'
# This is de default build script for .net solutions
# To implement this, override the variables with [OVERRIDE THIS VARIABLE]
# Or overrule parts of the script that dont fit your needs
# See https://docs.gitlab.com/ee/ci/yaml/includes.html
# The deployment phase expects a MSDEPLOY package as a zip file in artifacts\website.zip
# To overrule the file location update the PACKAGE_TO_DEPLOY variable.
include:
- template: "Workflows/MergeRequest-Pipelines.gitlab-ci.yml"
stages:
- build
- test
- deploy
variables:
ENABLE_UNITTESTS: 'false'
DEFAULT_IIS_SITENAME: "[OVERRIDE THIS VARIABLE]"
DEFAULT_DEVELOPMENT_URL: "[OVERRIDE THIS VARIABLE]"
DEFAULT_PRODUCTION_URL: "[OVERRIDE THIS VARIABLE]"
PACKAGE_TO_DEPLOY: 'artifacts\website.zip'
NUGET_PATH: 'C:\tools\build\nuget.exe'
MSBUILD_PATH: 'C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\MSBuild.exe'
MSDEPLOY_PATH: 'C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe'
validate_tag:
stage: .pre
script:
- echo Release tags should follow semver numbering
- echo For example "1.0.3"
- exit 1
tags:
- windows
rules:
- if: $CI_COMMIT_TAG && $CI_COMMIT_TAG !~ /^[0-9]+[.][0-9]+[.][0-9]+$/
build_debug:
stage: build
script:
- '& "$env:NUGET_PATH" restore -Verbosity quiet'
- '& "$env:MSBUILD_PATH" /p:DeployOnBuild=true /p:DeployTarget=Package /p:PackageLocation="$env:CI_PROJECT_DIR\$env:PACKAGE_TO_DEPLOY" /p:Configuration=Debug -consoleloggerparameters:"ErrorsOnly;Summary" -verbosity:quiet -m'
artifacts:
expire_in: 5 days
paths:
- '$env:PACKAGE_TO_DEPLOY'
tags:
- windows
rules:
- if: $CI_COMMIT_TAG
when: never
- if: $CI_MERGE_REQUEST_IID
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
# INSTALL https://www.nuget.org/packages/JUnitXml.TestLogger/ into the test projects
test:
stage: test
script:
- '& "$env:NUGET_PATH" restore -Verbosity quiet'
- '& "$env:MSBUILD_PATH" /p:DeployOnBuild=true /p:DeployTarget=Package /p:PackageLocation="$env:CI_PROJECT_DIR\$env:PACKAGE_TO_DEPLOY" /p:Configuration=Debug -consoleloggerparameters:"ErrorsOnly;Summary" -verbosity:quiet -m'
- 'dotnet test tests\**\bin\**\*.Tests.dll --test-adapter-path:. --logger:"junit;LogFilePath=.\artifacts\{assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"'
artifacts:
when: always # save test results even when the task fails
expire_in: 1 week
paths:
- artifacts/*test-result.xml
reports:
junit:
- artifacts/*test-result.xml
tags:
- windows
rules:
- if: $ENABLE_UNITTESTS == 'false'
when: never
- if: $CI_COMMIT_TAG
when: never
- if: $CI_MERGE_REQUEST_IID
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
deploy_development:
environment:
name: development
url: $DEFAULT_DEVELOPMENT_URL
stage: deploy
variables:
IIS_SERVER: 'devserver.nl'
IIS_SITENAME: development.$DEFAULT_IIS_SITENAME
IIS_WEBDEPLOY_USERNAME: $DEVSERVER_WEBDEPLOY_USERNAME # SET BY CI/CD Protected variables
IIS_WEBDEPLOY_PASSWORD: $DEVSERVER_WEBDEPLOY_PASSWORD # SET BY CI/CD Protected variables
script:
- |
$arguments = [string[]]@(
"-verb:sync",
"-source:package='$PACKAGE_TO_DEPLOY'",
"-dest:auto,computerName='https://${IIS_SERVER}:8172/msdeploy.axd?site=$IIS_SITENAME',userName='$IIS_WEBDEPLOY_USERNAME',password='$IIS_WEBDEPLOY_PASSWORD',authtype='Basic'",
"-setParam:name='IIS Web Application Name',value='$IIS_SITENAME'",
"-allowUntrusted",
"-skip:Directory='App_Data'",
"-skip:skipaction='Delete',objectname='dirPath',absolutepath='\\media'")
- Start-Process "$env:MSDEPLOY_PATH" -ArgumentList $arguments -NoNewWindow -Wait
tags:
- windows
rules:
- if: $CI_COMMIT_TAG
when: never
- if: $CI_MERGE_REQUEST_IID
when: manual
allow_failure: true
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
dependencies:
- build_debug
build_release:
stage: build
script:
- '& "$env:NUGET_PATH" restore -Verbosity quiet'
- '& "$env:MSBUILD_PATH" /p:DeployOnBuild=true /p:DeployTarget=Package /p:PackageLocation="$env:CI_PROJECT_DIR\$env:PACKAGE_TO_DEPLOY" /p:Configuration=Release -consoleloggerparameters:"ErrorsOnly;Summary" -verbosity:quiet -m'
artifacts:
paths:
- '$PACKAGE_TO_DEPLOY'
tags:
- windows
rules:
- if: $CI_COMMIT_TAG
deploy_production:
environment:
name: production
url: $DEFAULT_PRODUCTION_URL
stage: deploy
variables:
IIS_SERVER: 'prodserver.nl'
IIS_SITENAME: www.$DEFAULT_IIS_SITENAME
IIS_WEBDEPLOY_USERNAME: $PRODSERVER_WEBDEPLOY_USERNAME # SET BY CI/CD Protected variables
IIS_WEBDEPLOY_PASSWORD: $PRODSERVER_WEBDEPLOY_PASSWORD # SET BY CI/CD Protected variables
script:
- |
$arguments = [string[]]@(
"-verb:sync",
"-source:package='$PACKAGE_TO_DEPLOY'",
"-dest:auto,computerName='https://${IIS_SERVER}:8172/msdeploy.axd?site=$IIS_SITENAME',userName='$IIS_WEBDEPLOY_USERNAME',password='$IIS_WEBDEPLOY_PASSWORD',authtype='Basic'",
"-setParam:name='IIS Web Application Name',value='$IIS_SITENAME'",
"-allowUntrusted",
"-skip:Directory='App_Data'",
"-skip:skipaction='Delete',objectname='dirPath',absolutepath='\\media'")
- Start-Process "$env:MSDEPLOY_PATH" -ArgumentList $arguments -NoNewWindow -Wait
tags:
- windows
rules:
- if: $CI_COMMIT_TAG
when: manual
dependencies:
- build_release
@ravurivivek
Copy link

It shows job success but I didn't see any artifact that has been sent to the deployment server (IIS).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment