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

We are going to learn here how to:

We continue to set up CD for Worker Services

Stop and Delete Service

Navigate to your Release and click on +

Add new PowerShell task and provide Display name f.ex. Stop and Delete service. Try to give meaningful and descriptive name for your tasks.

Stop and Delete service

Script path should be the folder as explained earlier where we expand our application on server. We have to provide one input parameter to the PowerShell script, the service name we want to stop and delete. In my case the name for my service is WorkerService.

Remember to tell Azure DevOps to use latest installed version of PowerShell.

Now log in to the server and create a folder where you want your component to be.

I've just created folder WorkerService.

Delete files

Add new Task to our release, click on + and start to write Delete files

Delete files task

Choose Delete files and click on Add.

Provide the source folder we just created.

Delete files
C:\WorkerService\

And Contents **/*  means delete all files and subfolders.

Run release to check.

Copy files

Add new task again by clicking on +

Find Copy files task and click on Add

Copy files

Source folder should be the expanded tmp drop folder where we are coping files from.

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

Contents ** means all files and subfolders

Target Folder will be

C:\WorkerService\

Create release to check everything.

Install and Start Service

Add new PowerShell task

Install and start service

Script path will be where we copied our files

C:\WorkerService\PowerShell\Install-Service-Function.ps1

Now we have to provide all these parameters we defined in the PowerShell script

-UserName "$(UserName)"
-UserPassword "$(ServiceAccountPassword)"

First, we provide name and password for service account we want our service to run as. Here you have two choices run as Local System account (not recommended) and as This account where you provide the username and the password.

Here I am going to use the account I have created in this blog post.

Azure DevOps Library

You probably notice the strange syntax $(). This is the way how to get values from the variables we are going to define here.

Click on this and here you have two choices: define Pipelines variables and Variable groups where you can link your groups defined in the library.

I recommend you always to use Variable groups defined in the Library

Click on + Variable group to create our first variable group

Provide Variable group name and create two variables. Remember to click on lock after you provided the password.

Click on Save.

Now Link this Variable group to the Release here

Release -> WorkerService -> Edit

Variables -> Variable groups -> Link variable group

Find your group

And click on Link.

Provide other parameters for the PowerShell script

-UserName "$(UserName)"
-UserPassword "$(ServiceAccountPassword)"
-ServiceName "WorkerService" 
-DisplayName "WorkerService"
-BinaryPathName "C:\WorkerService\DemoWorkerService.exe"
-Description "This is our demo worker service"
-StartupType "delayed-auto"

It should be like this

Remember to select to use latest PowerShell version.

Save and run the release.

Run Service as Service Account

Most probably if you use the service account for first time you will get the error

Start-Service : Service 'WorkerService (WorkerService)' cannot be started due to the following error: Cannot start service 'WorkerService' on computer '.'.
At C:\WorkerService\PowerShell\Install-Service-Function.ps1:30 char:1

Go to Services-> find your service and double click on it. Click on Log On tab

choose Local System account, click on OK.

Start Service

Start

Observe that Service started.

Check also Event Viewer. Windows Logs -> System and Application for Errors.

Now open Log On tab again and try to log in as user we are trying to run as

Click on OK. Observe that you get

Start the service and check that everything is working as expected.

Now we run Release pipeline again and observe that everything became green.

Check also the server and Event Viewer.

Clean Up

Now we have to remove all files we unzipped.

Add new task, Delete Files

Provide

Source Folder should be the path where you Azure DevOps copies all files and, in the Contents, provide the name of the folder you want to delete. If you follow along, we called it tmp.

Save everything and create the Release. Check the server, tmp folder doesn't exist anymore.

And now we are done setting up CD for the Worker Server.

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.


Add user to the Azure Windows Server