Jenkins as Continuous Integration and Continuous Deployment

From Logic Wiki
Revision as of 14:27, 9 May 2016 by Dt1nh6 (Talk | contribs) (1 revision imported)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Setting Up Everything

General Settings

1 - Download relevant setup file from https://jenkins-ci.org/

2 - If you're installing Jenkins on a server which has TFS on it, change TFS port to 8090 before installing Jenkins.

3 - Download TFS plugin from https://wiki.jenkins-ci.org/display/JENKINS/Team+Foundation+Server+Plugin

4 - Install TFS plugin

  • Go to jenkins page http://localhost:8080
  • Click Manage Jenkins
  • Click Manage Plugins
  • Click Advanced
  • Upload Plugin file (.hpi) that you downloaded in step 10

5 - Restart Jenkins

6 - Create a TFS job

  • New Item
  • Give a name
  • Select Freestyle Project
  • Under Source Code Management select Team Foundation Server
  • Fill the fields like this

7 - Run a build

8 - If it's successful go to the configuration of the job and in Poll SCM field type H/5 * * * * This means check server on every 5 minutes and build if necessary

Building

9 - Now we need to configure Building

10 - Download MSBuild plugin from https://wiki.jenkins-ci.org/display/JENKINS/MSBuild+Plugin

10 a - to download MS Build form MS site follow this link http://www.microsoft.com/en-gb/download/details.aspx?id=40760

11 - Install MSBuild plugin

  • Go to jenkins page http://localhost:8080
  • Click Manage Jenkins
  • Click Manage Plugins
  • Click Advanced
  • Upload Plugin file (.hpi) that you downloaded in step 3


12 - After you are back in Jenkins, return to the Manage Jenkins screen, and this time click Configure System. On this screen, click the Add MSBuild button. After this section has expanded, you should fill in “MS Build 2013” for the Name value, and “c:\Program files (x86)\MSBuild\12.0\bin\msbuild.exe” for the Path to MSBuild value. You will get a Jenkins warning, but it’s safe to ignore. Finally, click the Save button at the bottom of the page.

13 - Go back to the Jenkins Test App Dashboard, click Configure and scroll down to the Build section. Click Add Build Step and choose Build a Visual Studio project or solution using MSBuild. For the MSBuild Build File, choose your solution file, for this example, I’m using “JenkinsTestApp.sln”.

You can always verify the solution name and path by viewing the Workspace of the Jenkins project.

14 - Modify Jenkins to Build Web Packages and Deploy Them to the IIS Server


Go back to the Jenkins App Dashboard and click Configure and scroll down to the Build section. Click Add Build Step and choose Execute Windows batch command and enter the following text to create the web packages:

"C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe" "WebForms1\WebForms1.csproj" /T:Build;Package /p:Configuration=DEBUG /p:OutputPath="obj\DEBUG" /p:DeployIisAppPath="web" /p:VisualStudioVersion=12.0

Check Setting Team Foundation Server For Deployment if there is a "package not found error"

15 - Make sure to change the DeployIisAppPath to the actual IIS deploy path for your application.

Deployment

Do this step one more time, but this time paste the following text to deploy the web packages:

 "C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\MsDeploy.exe" -verb=sync -source:package="WebForms1\obj\Debug\_PublishedWebsites\WebForms1_Package\WebForms1.zip" -dest:auto,wmsvc=https://r1d-tfs-web01:8172/msdeploy.axd?site=web,userName=ROCHDALE_MBC\AIAdmin,password=C1pralex6+ -allowUntrusted=true

16 - Make sure to change the above computerName value to the actual IIS computer name that you want your web packages to be deployed to.

Click Save. Now click the Build button. Take a look at the Console, and you will see your application being packaged and then deployed to your local IIS’s Virtual Directory of your app.

Deployment with build numbers

17 - Install these plugins

https://wiki.jenkins-ci.org/display/JENKINS/Promoted+Builds+Plugin

https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Trigger+Plugin

https://wiki.jenkins-ci.org/display/JENKINS/Copy+Artifact+Plugin

https://wiki.jenkins-ci.org/display/JENKINS/Clone+Workspace+SCM+Plugin

https://wiki.jenkins-ci.org/display/JENKINS/Build+Pipeline+Plugin

18 - You will also need to setup default Archive the Artifacts post-build action on this build job.

WebForms1\obj\Debug\_PublishedWebsites\WebForms1_Package\*.zip

19 - Check mark Promote builds when

20 - Define Name "Deploy to DEV"

21 - Under Criteria check mark Only when manually approved

22 - Under Actions use Trigger/call builds on other projects

23 - Check mark Block until the triggered projects finish their builds

24 - Mark this build as failure if the triggered build is worse or equal to: FAILURE (adjust according to statuses of your deploy job)

25 - Predefined parameters

Server=IP_of_my_dev_server`  
Job=$PROMOTED_JOB_NAME`  
BuildSelection=<SpecificBuildSelector><buildNumber>$PROMOTED_NUMBER</buildNumber></SpecificBuildSelector>

Above, in the Predefined parameters section, the name to the left of = are the parameters that are defined in your deploy job. And to the right of = are the values that will be assigned to those parameters when this promotion executes. Defines three parameters Server, Job and BuildSelection.

The parameter Server= is my own, as my deploy job can deploy to multiple servers. However if your deploy job is hardcoded to always deploy to a specific location, you won't need that.

The Job= parameter is required, but the name of the param depends on what you've setup in your deploy job (I will explain configuration there). The value $PROMOTED_JOB_NAME has to remain as is. This is an environment variable that the promotion process is aware of and refers back to the name of your build job (the one where promotion process is configured)

The BuildSelection= parameter is required. This whole line has to remain as is. The value passed is $PROMOTED_NUMBER, which once again the promotion is aware of. In your example, it would be #39.

The Block until the triggered projects finish their builds check mark will make the promotion process wait until the deploy job finished. If not, the promotion process will trigger the deployment job and quit with success. Waiting for the deploy job to finish has the benefit that if the deploy job fails, the promotion star will be marked with failure too.

(One little note here: the promotion star will appear successful while the deploy job is running. If there is a deploy failure, it will only change to failure after the deploy job finished. Logical... but can be a bit confusing if you look at the promotion star before the deployment completed)

Setup deploy job

26 - Under This build is parameterized

27 - Configure a parameter of type Choice (or Text) with name Server (this name has to match with configuration in promotion's Predefined Parameters in previous section)

Choices: Enter list of possible server IPs that would be used by the promotion's Predefined Parameters in previous section (see update note below)

28 - Configure a parameter of type Choice (or Text) with name Job (this name has to match with configuration in promotion's Predefined Parameters in previous section) Choices: Enter the name of your build job as default. This is only needed if you trigger the deploy job manually. When the deploy job is triggered from promotion, the promotion will supply the value (the Job= from Predefined parameters that we configured). Also, if there is no value passed from promotion's Predefined parameters, the first choice value will be used. If you have a 1-to-1 relationship between the build and deploy jobs, you can omit the Job= parameter in promotion's configuration.

29 - Update: since version 2.23 of Parameterized Trigger, the available choices in the deploy job configuration have to have all possible values coming from the promotion's predefined parameters. If you don't want that limit, use "Text" instead of "Choice"

30 - Configure a parameter of type Build selector for Copy Artifact with name: BuildSelection

Default Selector: Latest successful build

31 - Under Build steps

  • Configure Copy artifacts from another project
  • In Project name enter ${Job}
  • At Which build choose Specified by a build parameter
  • In Parameter Name enter BuildSelection (without ${...}!)

Configure the rest accordingly for your artifacts that will be copied from build job to deploy job's workspace

Use the copied artifacts inside the deploy job as you need in order to deploy

So now, with the above deploy job, you can run it manually and select which build number from build job you want to deploy (last build, last successful, by build number, etc). You probably already have it configured very similarly. The promotion on the build job will basically execute the same thing, and supply the build number, based on what promotion was executed.


Problems & Solutions

ERROR_USER_UNAUTHORIZED

  • Open Regedit
  • Open the path HKEY_LOCAL_MACHINE\Software\Microsoft\WebManagement\Server
  • add a DWORD named WindowsAuthenticationEnabled with a value of 1
  • Restart WMsvc and IIS

.NET Framework 4.5 is not located

Install .NET framework 4.5 SDK https://msdn.microsoft.com/en-us/windows/desktop/hh852363.aspx


All .NET Frameworks and SDKs Download page

https://msdn.microsoft.com/en-us/vstudio/aa496123.aspx

Jenkins Plugins URL

http://updates.jenkins-ci.org/download/plugins/


Helpful Links

http://www.frictionlesscode.com/have-continuous-integration-with-jenkins-in-30-mins/

http://stackoverflow.com/questions/15126059/how-to-promote-a-specific-build-number-from-another-job-in-jenkins

http://justinramel.com/2013/01/15/5-minute-setup/


ms build - > /p:Configuration=Release

Screenshots

Jenkins1.png

Jenkins2.png