Building a Simple .NET Build Server - Home | Anexinet

Post on 11-Feb-2022

4 views 0 download

Transcript of 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

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

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

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

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

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.

III. Install SQL Server Management Studio

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

management-studio-ssms

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.

B. Create SQL login for TeamCity

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

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

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

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

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

B. Configure TeamCity to use MS SQL Server

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

D. Accept the Terms and create the administrator account

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.

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

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

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

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.

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.

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.

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

installer using the default settings.

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

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

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.

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.

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

IX. Create a project in TeamCity

A. Create the project in TeamCity

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

application

C. Connect to GitHub in TeamCity to create the project

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

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

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%

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

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

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

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

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

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

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

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

G. Run the build

Click the Run button next to both Build and Test configurations

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

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

Use the TeamCity feed URL and newly created Octopus user credentials

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

Give it a name

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

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

Now deploy it to DEV

XIII. Add the Deploy configuration to the TeamCity project

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

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

It will run after a successful build of the Test configuration

Update the build number to use the Test build number

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

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

D. Add the OctopusDeploy: Create release build step

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

View all build configurations

XIV. Run the build

A. Commit and push new code

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

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

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

Test the application