building a simple .net build server - home | anexinet

77
Building a Simple .NET Build Server Contents I. Create a new Windows 2016 Server VM in Windows Azure ................................................................ 4 A. Create a new MSDN account and activate the Azure Free Trial ....................................................... 4 B. Create a new virtual machine in Azure ............................................................................................. 4 C. Set up the following network security rules in the network security group resource for the virtual machine (optional) .................................................................................................................................... 5 II. Install SQL Server 2016 Express ............................................................................................................ 6 A. https://www.microsoft.com/en-us/sql-server/sql-server-downloads............................................. 6 III. Install SQL Server Management Studio ............................................................................................ 8 A. https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms ...... 8 IV. Configure SQL Server ........................................................................................................................ 9 A. Enable mixed authentication (SQL and Windows authentication) ................................................... 9 B. Create SQL login for TeamCity ........................................................................................................ 11 C. Create empty database for TeamCity, set the TeamCity user as the Owner ................................. 12 D. Create SQL login for Octopus .......................................................................................................... 13 E. Create empty database for Octopus, set the Octopus user as the Owner ..................................... 13 F. Enable TCP/IP in SQL Server Configuration Manager ..................................................................... 13 G. Set the TCP Port to 1433 in the IP Address tab of the Properties window for all IP’s .................... 14 H. Start the SQL Server Browser service ............................................................................................. 14 I. Add Firewall rules to expose connections to SQL Server (optional) ............................................... 15 V. Install TeamCity................................................................................................................................... 16 A. https://www.jetbrains.com/teamcity/download/#section=windows ........................................... 16 B. Configure TeamCity to use MS SQL Server ..................................................................................... 18 C. Use the TeamCity SQL login setup earlier ....................................................................................... 19 D. Accept the Terms and create the administrator account ............................................................... 21 E. TeamCity is now installed with no projects .................................................................................... 22 F. Install the Octopus Deploy plugin for TeamCity ............................................................................. 22 G. Add Firewall rule to expose TeamCity Web UI externally (optional).............................................. 23 VI. Install Build Tools ............................................................................................................................ 23 A. Install .NET Framework 3.5 and ASP.NET 4.6.................................................................................. 23 B. Install Build Tools for Visual Studio 2017 ........................................................................................ 24

Upload: others

Post on 11-Feb-2022

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Building a Simple .NET Build Server - Home | Anexinet

Building a Simple .NET Build Server Contents I. Create a new Windows 2016 Server VM in Windows Azure ................................................................ 4

A. Create a new MSDN account and activate the Azure Free Trial ....................................................... 4

B. Create a new virtual machine in Azure ............................................................................................. 4

C. Set up the following network security rules in the network security group resource for the virtual

machine (optional) .................................................................................................................................... 5

II. Install SQL Server 2016 Express ............................................................................................................ 6

A. https://www.microsoft.com/en-us/sql-server/sql-server-downloads ............................................. 6

III. Install SQL Server Management Studio ............................................................................................ 8

A. https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms ...... 8

IV. Configure SQL Server ........................................................................................................................ 9

A. Enable mixed authentication (SQL and Windows authentication) ................................................... 9

B. Create SQL login for TeamCity ........................................................................................................ 11

C. Create empty database for TeamCity, set the TeamCity user as the Owner ................................. 12

D. Create SQL login for Octopus .......................................................................................................... 13

E. Create empty database for Octopus, set the Octopus user as the Owner ..................................... 13

F. Enable TCP/IP in SQL Server Configuration Manager ..................................................................... 13

G. Set the TCP Port to 1433 in the IP Address tab of the Properties window for all IP’s .................... 14

H. Start the SQL Server Browser service ............................................................................................. 14

I. Add Firewall rules to expose connections to SQL Server (optional) ............................................... 15

V. Install TeamCity ................................................................................................................................... 16

A. https://www.jetbrains.com/teamcity/download/#section=windows ........................................... 16

B. Configure TeamCity to use MS SQL Server ..................................................................................... 18

C. Use the TeamCity SQL login setup earlier ....................................................................................... 19

D. Accept the Terms and create the administrator account ............................................................... 21

E. TeamCity is now installed with no projects .................................................................................... 22

F. Install the Octopus Deploy plugin for TeamCity ............................................................................. 22

G. Add Firewall rule to expose TeamCity Web UI externally (optional) .............................................. 23

VI. Install Build Tools ............................................................................................................................ 23

A. Install .NET Framework 3.5 and ASP.NET 4.6 .................................................................................. 23

B. Install Build Tools for Visual Studio 2017 ........................................................................................ 24

Page 2: Building a Simple .NET Build Server - Home | Anexinet

C. Install .NET Framework Developer Pack ......................................................................................... 25

VII. Install Octopus Deploy .................................................................................................................... 28

A. https://octopus.com/downloads .................................................................................................... 28

B. Configure Octopus to use the local SQL Server instance with the Octopus database and SQL

authentication ......................................................................................................................................... 28

C. Configure the Web Portal ............................................................................................................... 28

D. Create an environment ................................................................................................................... 32

E. Install the tentacle and configure the first deployment target ...................................................... 33

VIII. Create a sample project in a GitHub repository ............................................................................. 41

A. Set up a new GitHub repository ...................................................................................................... 41

B. Create a new Console Application project using Visual Studio ...................................................... 41

C. Add a Unit Test project to the solution ........................................................................................... 42

IX. Create a project in TeamCity........................................................................................................... 43

A. Create the project in TeamCity ....................................................................................................... 43

B. Register a new OAuth application in GitHub .................................................................................. 44

C. Connect to GitHub in TeamCity to create the project .................................................................... 45

D. Add the MSBuild build step to the Build configuration .................................................................. 46

E. Update the build number format for the build configuration ........................................................ 47

F. Add the Nuget tool to TeamCity ..................................................................................................... 48

G. Add the Nuget Installer build step to the build configuration ........................................................ 49

X. Add Test configuration to the TeamCity project ................................................................................ 50

A. Add the Test build configuration .................................................................................................... 50

B. Install NUnit on the build server ..................................................................................................... 51

C. Add the NUnit build step to the Test build configuration .............................................................. 52

D. Add a trigger to the build configuration ......................................................................................... 53

E. Add a dependency to the first build configuration ......................................................................... 54

F. Update the build number format ................................................................................................... 55

G. Run the build ................................................................................................................................... 56

XI. Nuget Feed ...................................................................................................................................... 57

A. Enable the Nuget feed in TeamCity ................................................................................................ 57

B. Add a new user in TeamCity for Octopus ....................................................................................... 57

C. Add the Nuget feed in Octopus ...................................................................................................... 58

D. Test the Nuget feed connection ..................................................................................................... 60

Page 3: Building a Simple .NET Build Server - Home | Anexinet

XII. Create a project in Octopus ............................................................................................................ 60

A. Create the project ........................................................................................................................... 60

B. Add the deployment step ............................................................................................................... 62

C. Create a release and deploy it ........................................................................................................ 63

XIII. Add the Deploy configuration to the TeamCity project .................................................................. 66

A. Create a new build configuration in the TeamCity project called Deploy ...................................... 66

B. Add the dependency and trigger and update the build number .................................................... 67

C. Get an API key from Octopus .......................................................................................................... 69

D. Add the OctopusDeploy: Create release build step ........................................................................ 71

E. Add the OctopusDeploy: Deploy release build step ....................................................................... 72

XIV. Run the build ................................................................................................................................... 75

A. Commit and push new code ........................................................................................................... 75

B. Watch TeamCity and Octopus build, test and deploy the solution ................................................ 75

C. Verify and test deployment ............................................................................................................ 76

Page 4: Building a Simple .NET Build Server - Home | Anexinet

I. Create a new Windows 2016 Server VM in Windows Azure

A. Create a new MSDN account and activate the Azure Free Trial

B. Create a new virtual machine in Azure

With the Free Trial, you are limited to only 4 processing cores. Choose a size that has 4 processing cores,

I prefer the Standard A4_v2 size, as it has enough memory and processing power for a build server and

to host small applications

Page 5: Building a Simple .NET Build Server - Home | Anexinet

C. Set up the following network security rules in the network security group

resource for the virtual machine (optional)

This optional step allows you to browse to the TeamCity, Octopus, and IIS websites and connect to the

SQL Server from a remote machine. Go to Resource Groups, click your resource group name, click the

Network Security Group resource, click Inbound Security Rules, add the following rules:

Description Protocol Port

SQL Server – TCP TCP 1433

SQL Server – UDP UDP 1434

TeamCity Web UI TCP 8080 (port chosen for TeamCity)

Octopus Web UI TCP 8081 (port chosen for Octopus)

HTTP TCP 80

Page 6: Building a Simple .NET Build Server - Home | Anexinet

II. Install SQL Server 2016 Express

A. https://www.microsoft.com/en-us/sql-server/sql-server-downloads With the Express version, you are limited to a 10 GB database size and are not able to use the SQL

Server Agent. This is sufficient for running a build server and hosting simple applications.

Page 7: Building a Simple .NET Build Server - Home | Anexinet
Page 8: Building a Simple .NET Build Server - Home | Anexinet

III. Install SQL Server Management Studio

A. https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-

management-studio-ssms

Page 9: Building a Simple .NET Build Server - Home | Anexinet

IV. Configure SQL Server

A. Enable mixed authentication (SQL and Windows authentication) Right click on the server in the Object Explorer, click Properties, click the Security page, select SQL Server

and Windows Authentication Mode under the Server authentication heading, then click OK.

Page 10: Building a Simple .NET Build Server - Home | Anexinet
Page 11: Building a Simple .NET Build Server - Home | Anexinet

B. Create SQL login for TeamCity

Page 12: Building a Simple .NET Build Server - Home | Anexinet

C. Create empty database for TeamCity, set the TeamCity user as the Owner

Page 13: Building a Simple .NET Build Server - Home | Anexinet

D. Create SQL login for Octopus

E. Create empty database for Octopus, set the Octopus user as the Owner

F. Enable TCP/IP in SQL Server Configuration Manager

Page 14: Building a Simple .NET Build Server - Home | Anexinet

G. Set the TCP Port to 1433 in the IP Address tab of the Properties window for

all IP’s

H. Start the SQL Server Browser service If the Start service button is greyed out, the service is disabled. Right click the service, click Properties,

go to the Service tab, set the Start Mode to Automatic

Page 15: Building a Simple .NET Build Server - Home | Anexinet

I. Add Firewall rules to expose connections to SQL Server (optional) Open Windows Firewall, click Inbound Rules, click Action, then New Rule… and add the following rules:

Description Rule Type Program/Protocol Port

SQL Server program Program C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\Binn\sqlservr.exe

TCP port Port TCP 1433

UDP port Port UDP 1434

Page 16: Building a Simple .NET Build Server - Home | Anexinet

V. Install TeamCity

A. https://www.jetbrains.com/teamcity/download/#section=windows The default settings work fine, but I prefer to use a different port, such as 8080, so I can have an IIS web

application running on port 80

Page 17: Building a Simple .NET Build Server - Home | Anexinet
Page 18: Building a Simple .NET Build Server - Home | Anexinet

B. Configure TeamCity to use MS SQL Server

Page 19: Building a Simple .NET Build Server - Home | Anexinet

Download the JDBC driver from the instructions provided by TeamCity and copy it to the TeamCity Data

directory:

C. Use the TeamCity SQL login setup earlier

Page 20: Building a Simple .NET Build Server - Home | Anexinet
Page 21: Building a Simple .NET Build Server - Home | Anexinet

D. Accept the Terms and create the administrator account

Page 22: Building a Simple .NET Build Server - Home | Anexinet

E. TeamCity is now installed with no projects

F. Install the Octopus Deploy plugin for TeamCity Simply copy the Octopus.TeamCity.zip file from https://octopus.com/downloads into the [TeamCity

Data Directory]\plugins folder then restart the TeamCity server.

Page 23: Building a Simple .NET Build Server - Home | Anexinet

G. Add Firewall rule to expose TeamCity Web UI externally (optional)

VI. Install Build Tools

A. Install .NET Framework 3.5 and ASP.NET 4.6 Open the Server Manager, click Manager, then click Add Roles and Features

Page 24: Building a Simple .NET Build Server - Home | Anexinet

B. Install Build Tools for Visual Studio 2017 https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2017

Page 25: Building a Simple .NET Build Server - Home | Anexinet

C. Install .NET Framework Developer Pack https://www.microsoft.com/en-us/download/confirmation.aspx?id=49978

Page 26: Building a Simple .NET Build Server - Home | Anexinet
Page 27: Building a Simple .NET Build Server - Home | Anexinet
Page 28: Building a Simple .NET Build Server - Home | Anexinet

VII. Install Octopus Deploy

A. https://octopus.com/downloads

B. Configure Octopus to use the local SQL Server instance with the Octopus

database and SQL authentication

C. Configure the Web Portal The default settings work fine, but again, I prefer to use a different port, such as 8081.

Page 29: Building a Simple .NET Build Server - Home | Anexinet
Page 30: Building a Simple .NET Build Server - Home | Anexinet
Page 31: Building a Simple .NET Build Server - Home | Anexinet
Page 32: Building a Simple .NET Build Server - Home | Anexinet

D. Create an environment You can have multiple environments for the different stages of the software development lifecycle, but

typically you’ll have at least two: one for dev/staging/test and the live production environment. Start

out by creating a DEV environment.

Page 33: Building a Simple .NET Build Server - Home | Anexinet

E. Install the tentacle and configure the first deployment target In the Octopus web UI, go to the newly created environment and click the “Add deployment target”

button to set up a new Listening Tentacle. The same server can host Octopus, as well as be tentacle to

have applications deployed to.

Page 34: Building a Simple .NET Build Server - Home | Anexinet

Download and run the Octopus Tentacle installer: https://octopus.com/downloads. Walk through the

installer using the default settings.

Page 35: Building a Simple .NET Build Server - Home | Anexinet
Page 36: Building a Simple .NET Build Server - Home | Anexinet

Use the thumbprint from “New deployment target” screen in Octopus

Page 37: Building a Simple .NET Build Server - Home | Anexinet
Page 38: Building a Simple .NET Build Server - Home | Anexinet

After the Tentacle service is installed, go back to Octopus to finish configuring the deployment target.

Page 39: Building a Simple .NET Build Server - Home | Anexinet

The tentacle is on the same server, so we can use localhost as the hostname. Click Discover and the

tentacle will automatically resolve. Create a new role for this server, any application with this new role

will be able to be deployed to this deployment target. Copy the tentacle thumbprint and save it.

Page 40: Building a Simple .NET Build Server - Home | Anexinet
Page 41: Building a Simple .NET Build Server - Home | Anexinet

VIII. Create a sample project in a GitHub repository

A. Set up a new GitHub repository The code for this exists in my GitHub repo: https://github.com/mriedel90/DevOpsDemo

I like to organize the repository with a few top-level folders so it follows this folder structure:

• Root o Source

▪ Projects ▪ .sln file

o Test ▪ Projects

o Build

B. Create a new Console Application project using Visual Studio Clone the repository to your machine then create the new solution in the Source folder. The project will

have a simple Main method, that calls a Worker class to do work.

Page 42: Building a Simple .NET Build Server - Home | Anexinet

C. Add a Unit Test project to the solution Keep all test projects in a different location as the main source projects, when adding the project to the

solution, point the folder to the Test folder in the repository.

Use NUnit, remove references to the standard Microsoft testing framework and add the NUnit Nuget

package and write unit tests using NUnit

Page 43: Building a Simple .NET Build Server - Home | Anexinet

IX. Create a project in TeamCity

A. Create the project in TeamCity

Page 44: Building a Simple .NET Build Server - Home | Anexinet

B. Register a new OAuth application in GitHub Go to your GitHub account settings, under Developer settings, select OAuth applications. Register the

application

Page 45: Building a Simple .NET Build Server - Home | Anexinet

C. Connect to GitHub in TeamCity to create the project

When creating the project, the first Build Configuration will be added

Page 46: Building a Simple .NET Build Server - Home | Anexinet

D. Add the MSBuild build step to the Build configuration Choose to add build steps manually

Choose MSBuild as the Runner Type

Set the path to the .sln file in the Build File Path

Use Build Tools 2017, since we installed Build Tools for Visual Studio 2017 in step VI.

Check the box to run Octopack, this shows up because we installed the TeamCity.Octopus plugin in step

V. F. and use the build number for the Octopack version

Page 47: Building a Simple .NET Build Server - Home | Anexinet

E. Update the build number format for the build configuration Under General Settings for the build configuration, under advanced settings, set the Build Number

Format to 1.0.%build.counter%

Page 48: Building a Simple .NET Build Server - Home | Anexinet

F. Add the Nuget tool to TeamCity Under Administration, then Tools, install the Nugest.exe tool

Page 49: Building a Simple .NET Build Server - Home | Anexinet

G. Add the Nuget Installer build step to the build configuration Go to the Build Steps for the build configuration and click Add build step

Select the Nuget Installer runner type and point it to your solution file

Before the solution will build, all Nuget packages need to be downloaded. The Nuget Installer step does

this and needs to occur before the build. Go back to the Build Steps for this build configuration and

reorder them

Page 50: Building a Simple .NET Build Server - Home | Anexinet

X. Add Test configuration to the TeamCity project

A. Add the Test build configuration At the Root project level, add the build configuration named Test

Choose to create it Manually

Page 51: Building a Simple .NET Build Server - Home | Anexinet

B. Install NUnit on the build server Download and install the NUnit Console from http://nunit.org/download/

Page 52: Building a Simple .NET Build Server - Home | Anexinet

C. Add the NUnit build step to the Test build configuration Select the version of NUnit installed as the NUnit runner

Enter the path to the NUnit console tool, it should be similar to: C:\Program Files (x86)\NUnit.org\nunit-

console\nunit3-console.exe

Add the path to the assemblies to run tests for: Test\**\bin\**\DevOpsDemo*.dll

This is why we store tests in a separate folder in the repository. Here, ** indicates any folder path,

including subfolders, and * indicates a simple wild card

Page 53: Building a Simple .NET Build Server - Home | Anexinet

D. Add a trigger to the build configuration Select the Finish Build Trigger and ensure it only runs after a successful build

Page 54: Building a Simple .NET Build Server - Home | Anexinet

E. Add a dependency to the first build configuration This is so we can use the same build number for this build configuration

Page 55: Building a Simple .NET Build Server - Home | Anexinet

F. Update the build number format In the General Settings for the build configuration, use the build number for the first build configuration

as the build number

Page 56: Building a Simple .NET Build Server - Home | Anexinet

G. Run the build

Click the Run button next to both Build and Test configurations

Page 57: Building a Simple .NET Build Server - Home | Anexinet

Both builds were successful and the Nuget package was add to the artifacts

XI. Nuget Feed

A. Enable the Nuget feed in TeamCity Under Administration, enable the Nuget feed

B. Add a new user in TeamCity for Octopus Optionally enable the Guest user in the public feed, or add a new user for Octopus

Page 58: Building a Simple .NET Build Server - Home | Anexinet

C. Add the Nuget feed in Octopus Under Library, select External Feeds, and add the feed

Page 59: Building a Simple .NET Build Server - Home | Anexinet

Use the TeamCity feed URL and newly created Octopus user credentials

Page 60: Building a Simple .NET Build Server - Home | Anexinet

D. Test the Nuget feed connection Search for the DevOpsDemo Nugest package to ensure everything is linked up correctly

XII. Create a project in Octopus

A. Create the project Under projects, click the Add Project button

Page 61: Building a Simple .NET Build Server - Home | Anexinet

Give it a name

Page 62: Building a Simple .NET Build Server - Home | Anexinet

B. Add the deployment step Under Process on the new project, add a new step called Deploy based off the Deploy a Package step

template

Configure it to use the TeamCity Nuget feed and default options

Page 63: Building a Simple .NET Build Server - Home | Anexinet

C. Create a release and deploy it Create the first release

Page 64: Building a Simple .NET Build Server - Home | Anexinet

Now deploy it to DEV

Page 65: Building a Simple .NET Build Server - Home | Anexinet
Page 66: Building a Simple .NET Build Server - Home | Anexinet

XIII. Add the Deploy configuration to the TeamCity project

A. Create a new build configuration in the TeamCity project called Deploy

Page 67: Building a Simple .NET Build Server - Home | Anexinet

B. Add the dependency and trigger and update the build number This configuration will depend on the Test configuration

Page 68: Building a Simple .NET Build Server - Home | Anexinet

It will run after a successful build of the Test configuration

Update the build number to use the Test build number

Page 69: Building a Simple .NET Build Server - Home | Anexinet

C. Get an API key from Octopus Under your profile, select the API keys tab and create a new key for TeamCity

Page 70: Building a Simple .NET Build Server - Home | Anexinet

Save the key somewhere for use later on as you cannot retrieve this key from Octopus

Page 71: Building a Simple .NET Build Server - Home | Anexinet

D. Add the OctopusDeploy: Create release build step

Page 72: Building a Simple .NET Build Server - Home | Anexinet

Enter the URL for the Octopus server, the API key, project name, and use the build number as the

release number

E. Add the OctopusDeploy: Deploy release build step Enter the same information as the Create release step, but this time an Environment is required

Page 73: Building a Simple .NET Build Server - Home | Anexinet

View all build configurations

Page 74: Building a Simple .NET Build Server - Home | Anexinet
Page 75: Building a Simple .NET Build Server - Home | Anexinet

XIV. Run the build

A. Commit and push new code

B. Watch TeamCity and Octopus build, test and deploy the solution

Page 76: Building a Simple .NET Build Server - Home | Anexinet

C. Verify and test deployment Log on to the DEV server and browse to the deployment folder, each version released will be listed

Page 77: Building a Simple .NET Build Server - Home | Anexinet

Verify the contents of the latest version, only the necessary files are deployed

Test the application