Skip to content

Latest commit

 

History

History
125 lines (92 loc) · 6.36 KB

200.md

File metadata and controls

125 lines (92 loc) · 6.36 KB

Hands on Lab 200 - Deploy using Jenkins Build sysDeploy using Jenkins Build

In this Hands on Lab (HOL) you will learn how to:

  1. Deploy a Jenkins Master from an Azure Market place image
  2. Install components to the Jenkins Master
  3. Create an Azure Service Principal to deploy resources on your behalf
  4. Create a "Web App for Containers" App Service
  5. Create an Azure Container Registry (ACR)
  6. Setup a Jenkins Job for Docker
  7. Create a Jenkins Pipeline
  8. Profit

Prerequisites

  • Must have a github account or create a new one
  • You must provide your own laptop computer for this hands-on lab.
  • Laptop must be connected to internet via WiFi
  • Azure Pass/Azure Free trial Subscription and Login to Azure portal (Cloud Shell)  or the Azure CLI 2.0

What will you learn:

  • Configure Jenkins to deploy Web Apps for Containers.

Create a Jenkins Master

Use this template from marketplace to create it: https://azuremarketplace.microsoft.com/en-us/marketplace/apps/azure-oss.jenkins?tab=Overview
 
See doc for details: https://docs.microsoft.com/en-us/azure/jenkins/install-jenkins-solution-template 
 
It includes Java Development Kit (JDK) version 8 and the following required Jenkins plugins:

- [Jenkins Git client plugin](https://plugins.jenkins.io/git-client) version 2.4.6
- [Docker Commons plugin](https://plugins.jenkins.io/docker-commons) version 1.4.0
- [Azure Credentials](https://plugins.jenkins.io/azure-credentials) version 1.2

Fork the Java app

Fork the java app https://github.com/azure-devops/javawebappsample 
Locally clone the app from your forked repo 

Add Java SDK and Maven to build the Java sample project

To install the components, log in to the Jenkins instance with SSH and run the following commands:
 
sudo apt-get install -y openjdk-7-jdk
sudo apt-get install -y maven

Install docker on Jenkins instance

https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/

See also: [Hands on Lab 100 - Using Docker Hub + Django Poll Application](100.md#Install-Docker-on-the-linux-machine)

Add an Azure service principal to the Jenkins credentials

You need an Azure service principal to deploy to Azure.
1. To create an Azure service principal, use the [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/create-an-azure-service-principal-azure-cli?toc=%2fazure%2fazure-resource-manager%2ftoc.json) or the [Azure portal](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal).
2. On the Jenkins dashboard, select Credentials > System. Then, select Global credentials(unrestricted).
3. To add a Microsoft Azure service principal, select Add Credentials. Supply values for the Subscription ID, Client ID, Client Secret, and OAuth 2.0 Token Endpoint fields. Set the IDfield to mySp. We use this ID in subsequent steps in this article.

 

Create web app for containers

Make a note of the resource group of the Jenkins VM
Create a app service plan 
az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku S1 --is-linux
 
Create a web app 
az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <app name> --runtime "DOTNETCORE|1.1" --deployment-local-git
 
Add app setting to enable GIT deploy 
az webapp config appsettings set -g MyResourceGroup -n mywebapp --settings WEBSITE_ENABLE_APP_SERVICE_STORAGE=true
 
Browse site to check if it was created successfully 
http://<app name>.azurewebsites.net

Create a Azure Container registry

Create an ACR 

az acr create --name myContainerRegistry007 --resource-group myResourceGroup --admin-enabled --sku Basic
 
Before pulling or pushing images, you need to login to the ACR 
 
az acr login --name <acrname>

Set up the Jenkins job for Docker

Create a new freestyle project on the Jenkins Dashboard.

1. Configure the Source Code Management field to use your local fork of the [simple Java web app for Azure](https://github.com/azure-devops/javawebappsample). Provide the Repository URL value. For example: http://github.com/<your_ID>/javawebappsample.
2. Add a step to build the project by using Maven by adding an Execute shell command. Include the following line in the command: mvn clean package
3. Add a post-build action by selecting Publish an Azure Web App.
4. Supply mySp as the Azure service principal. This principal was stored as the Azure Credentials in a previous step.
5. In the App Configuration section, choose the resource group and a Linux web app in your subscription.
6. Choose Publish via Docker.
7. Fill in the Dockerfile path value. You can keep the default value /Dockerfile. For the Docker registry URL value, supply the URL by using the format https://<yourRegistry>.azurecr.io if you use Azure Container Registry. If you use Docker Hub, leave the value blank.
8. For the Registry credentials value, add the credential for the container registry. You can get the userid and password by running the following commands in the Azure CLI. The first command enables the administrator account:

az acr update -n <yourRegistry> --admin-enabled true
az acr credential show -n <yourRegistry>

Create a Jenkins pipeline

1. Open Jenkins in a web browser. Select New Item.
1. Provide a name for the job and select Pipeline. Select OK.
2. Select the Pipeline tab.
3. For the Definition value, select Pipeline script from SCM.
4. For the SCM value, select Git. Enter the GitHub URL for your forked repo. For example: https://<your_forked_repo>.git.
5. Update the Script Path value to Jenkinsfile_container_plugin.
6. Select Save and run the job.

Verify your web app

1. o verify your web app, run the following command in the Azure CLI:

az acr repository list -n <myRegistry> -o json

The following message is displayed:

["calculator"]

2. Go to http://<your_app_name>.azurewebsites.net/api/calculator/ping. Replace <your_app_name> with the name of your web app. You see the message:

Welcome to Java Web App!!! This is updated!
Sun Jul 09 16:39:10 UTC 2017

3. Go to http://<your_app_name>.azurewebsites.net/api/calculator/add?x=<x>&y=<y>. Replace <x> and <y> with any numbers to get the sum of x + y.