Here is the link to the "home page" for this blog series.

We are going to learn here how to:

Prerequisites

Create a new Release

When we have created well functional Continuous Integration (CI), read previous blog post here, it is time to set up Continuous Delivery (CD). Login to your Azure DevOps account and Navigate to Pipelines -> Releases

Releases

Click on + New to create a new Release

New release pipeline

Choose + New release pipeline

Create IIS website and SQL database deployment job

In the Search box start to write IIS

And choose IIS website and SQL database deployment by clicking on Apply

Provide Stage name

Add an artifact

Now we are going to add an artifact. Click on Add an artifact area.

Artifacts

Here we are selecting published artifacts drop folder we created in the previous blog post here. And to remind you it looked like this

Artifacts

Source type should be Build

Choose right Project, mine called WorkerService.

Source (build pipeline) should be artifacts from which pipeline you want to use.

Default version should be always Latest.

Source alias can be just used default one.

And click on Add.

Now click on Continuous deployment trigger to activate CD

Continuous deployement trigger

Enable

This means each time CI pipeline goes green it will trigger deployment.

Create Stage tasks

Now we are done with Artifacts and will define stage tasks. Click on 2 jobs, 3 tasks

Delete SQL Deployment group by selecting it and click on Remove.

And remove IIS Web App Deploy and IIS Web App Manage tasks.

Choose IIS Deployment and select Deployment group.

This is the deployment group we created here.

Create File transform task

Now we are going to add task to the IIS Deployment. Click on + and start to write File trans

Choose File transform task and click on Add. This task will do transformation of our appsettings.json file when we are going to use multiple environments. More about this later.

Click on ...

Choose zip package from artifacts we added previously.

Click on OK.

Choose File format JSON

File format

Provide meaningful name for the Release and click on Save.

Now we can test the Release. Click on Create release.

Click on Create and observe that the Release is In progress.

After a while observe that release went green.

Click on it and check the logs.

Here we see task we created, File Transform and some task Azure DevOps created automatically. Spend some time and check all this logs.

Azure DevOps Folder structure on server

Click on task called Download artifact - _WorkerService - drop. And find where Azure DevOps downloaded the artifacts on server.

2020-08-05T10:36:22.7070059Z Downloading drop/TheBestWorkerService/TheBestWorkerService.zip
to
C:\azagent\A1\_work\r2\a\_WorkerService\drop\TheBestWorkerService\TheBestWorkerService.zip

It is very important to understand the folder structure where Azure DevOps copies files on server.

Log to your server. And check that you can find your zip package.

Create Expand Archive Powershell task

Now we are ready to create next Task, navigate to your release in Azure DevOps and click on IIS Deployment and on +

Start writing in the search box powershell

Find PowerShell task and click on Add

We are going to use PowerShell to expand zip package.

Provide meaningful Display name f.ex. Expand Archive and Choose Type Inline, this is just one PowerShell command Expand-Archive

We have to provide the path where zip package is located on the server.

-Path $(System.DefaultWorkingDirectory)/_WorkerService/
drop/TheBestWorkerService/TheBestWorkerService.zip 

Here we are using predefined variables in this case $(System.DefaultWorkingDirectory) means C:\azagent\A1_work\r2\a\

Destination path should be

-DestinationPath $(System.DefaultWorkingDirectory)/
_WorkerService/drop/TheBestWorkerService/tmp

means that we are going to place unzipped files to the folder called tmp.

Provide meaninful Display name and run the release again. Check that we are getting expected result.

tmp folder is created with all our unzipped files

tmp folder

Remove/Install worker service

Now it is time to Remove/Install worker service as Windows Service in Services. For this we are going to use PowerShell as well. Make sure to install the latest PowerShell version on the server. Download the msi installer for PowerShell from here. Install and restart the server.

Open the code in the Visual Studio.

Right click on DemoWorkerService -> Add -> New Folder

New Folder

Call it PowerShell and add to PowerShell files. Delete-Service-Function.ps1 and Install-Service-Function.ps1. This is as you can guess two PowerShell scripts for install and delete the service.

Delete-Service-Function.ps1

[CmdletBinding()]
param (
	[string] $ServiceName
)

if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {     
   Stop-Service -Name $ServiceName
   Write-Host "Service $ServiceName stopped"
   
   Start-Sleep 5

   Remove-Service -Name $ServiceName
   Write-Host "Service $ServiceName removed"
   
    Start-Sleep 10

}else {
      Write-Host "Service $ServiceName is not installed."        
}

Here we are first stopping the service if exists, sleep for 5 seconds then removing it and sleep again for 10 seconds. It takes a little bit of time for OS to stop and remove the service. The function takes one parameter service name

Install-Service-Function.ps1

[CmdletBinding()]
param (

    [string] $UserName,
    
    [string] $UserPassword,

    [string] $ServiceName,

    [string] $DisplayName,

    [string] $BinaryPathName,

    [string] $Description
)

$PWord = ConvertTo-SecureString $UserPassword -AsPlainText -Force

$params = @{
  Name = $ServiceName
  BinaryPathName = $BinaryPathName
  DisplayName = $DisplayName      
  Description = $Description
  Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName,  $PWord
  StartupType = "AutomaticDelayedStart"
}

New-Service @params
Write-Host "Service $ServiceName installed" 

Start-Service -Name $ServiceName
Write-Host "Service $ServiceName started" 

I will explain parameters later but here we just install service and start it.

Remember to right click on these files -> Properties

Copy always.

Commit and push your changes.

The Part 1 becoming too big and that's why we continue with Part 2 here (Coming soon...).

Here is the link to the "home page" for this blog series.

If you like my post image, you can free download it from here.

Let me know in the comments if you experienced any troubles or if you have any feedbacks. Don't forget to subscribe to get news from Sergey .NET directly to your mailbox.


My Azure DevOps public project Worker Service

Create Azure Windows Server

Download latest PowerShell