Skip to content

Latest commit

 

History

History
224 lines (149 loc) · 10.7 KB

200.md

File metadata and controls

224 lines (149 loc) · 10.7 KB

Hands on Lab 200 - Deploy Java App using Jenkins for Web App for Containers

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

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

Prerequisites

  • You must have a github account, or you can create a new one at github.com
  • If you want to use DockerHub for container image, you
  • You need an Azure Subscription so you can login Azure portal or Azure CLI 2.0

What will you learn

  • Deploy a Jenkins server to Azure and configure Jenkins for deploying a Java app to Web Apps for Containers.

Section #1: Prepare your dev machine and install Jenkins server

Prepare your dev machine and Git repo

Generate SSH Key

*Note*: this lab machine includes a pre-generated SSH key pair, you can skip the following stpes if you choose to use the existing keys to save time.

Open a terminal on your lab Ubuntu machine.

Create your SSH key pair on the command-line: 
 	
mkdir ~/.ssh
chmod 700 ~/.ssh
ssh-keygen -t rsa -b 2048

You can leave the passphrase as blank to save time.

Make note: the location for the public key is at ~/.ssh/id_ras.pub. You will need the public key during Jenkins server installation. You can cat ~/.ssh/id_ras.pub then copy it from the terminal.

Fork the Java app repo on github.com

Fork the Java app https://github.com/azure-devops/javawebappsample.
In browser, go to the github repo page, sign in using your github.com account, then click "Fork" on the repo page.

Once forked, clone the app from your forked git repo to dev machine, on the command-line:

cd /src
git clone https://github.com/[your-github-account]/javawebappsample.git

Create a Jenkins Instance and install necessary packages

Installl Jenkins Server

Use the Jenkins template from [Azure marketplace](https://azuremarketplace.microsoft.com/en-us/marketplace/apps/azure-oss.jenkins?tab=Overview) to create a Jenkins server.

*Note* - used D2S_v3 or D4S_v3 and they worked. Make note for the "User name" and "Domain name lable", you will need them to later to SSH to Jenkins server.
**Note** - on the second step in Jenkins template, you can choose "Azure Verified" for Jenkins release type, as it create a Service Principal for you automatically and registeres it with Jenkins. It can save lots of time for later steps.   
 
See Jenkins 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

This prcoess takes about 6-9 minutes depends on the size of VM you choose. 

Test if you can SSH to Jenikins server using the following command:

ssh -L 127.0.0.1:8080:localhost:8080 [email protected]

Note: the username is the "User name" you typed when created the Jenkins server, the serverdnsname is the Jenkins VM dns name (e.g. "Domain name label") and rengion is the AZ region where you created the VM. You can find the info on the portal.    

Test if you can browse the Jenkins dashboard at: http://localhost:8080/

Note: make sure you use *sudo* when you retrive the initial admin password from Jenkins server.

Add Java SDK and Maven on Jenkins instance

To install the components, connect 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/

# Run the following commands to set up Docker engine and CLI tools

# Update your repository manager
sudo apt-get update 

# Install package dependencies
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common 
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - 

# Add the docker gpg keys
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" 

# Install Docker CE
sudo apt-get update 
sudo apt-get install docker-ce 

# Verify Docker Installation 
sudo docker run hello-world 

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 portal](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal)
   or, 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).
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.

Section #2 Create a Web App and configure Jenkins to deploy to Web App for Containers

Prepare Azure CLI on dev machine

Az cli 2.0 is already installed on the machine
# clear previous cached account
az account clear

# login in using Azure CLI
az login

# set the default subscription if you have multiple subscriptions
az account list
az account set --subscription <sub-name>

 

Create a web app using Web App for Containers

Complete the following steps in command-line    

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 using Nginx as placeholder
az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <app name> --deployment-container-image-name nginx

  Browse site to check if it was created successfully http://.azurewebsites.net

Create a Azure Container registry

Complete the following steps in command-line

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 build job for Docker image

On the Jenkins Dashboard, create a new freestyle project  

1. Configure the Source Code Management field to use your 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>

9. Verify if the docker image is built and pushed to the container registry. And verify the app following "Verify your web app" section at the end of the workshop. 

Note: if the build job failed due to Docker permission issue, you can run chmod 777 /var/run/docker.sock in command-line as a quick workaround. 

Deploy Web App for Containers by using Jenkins Pipeline

Change the Java app github repo for Jenkins Pipeline

1. In the GitHub interface, open the Jenkinsfile_container_plugin file. To edit the file, select the pencil icon. Update the resourceGroup and webAppName definitions for your web app on lines 11 and 12, respectively:

Java 

def resourceGroup = '<myResourceGroup>'
def webAppName = '<myAppName>'

2. Change line 13 to your container registry server:

Java

def registryServer = '<registryURL>'

3. Change line 16 to use the credential ID in your Jenkins instance:

Java

azureWebAppPublish azureCredentialsId: '<mySp>', publishType: 'docker', resourceGroup: resourceGroup, appName: webAppName, dockerImageName: imageName, dockerImageTag: imageTag, dockerRegistryEndpoint: [credentialsId: 'acr', url: "http://$registryServer"]

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: http://github.com/<your_ID>/javawebappsample.
5. Update the Script Path value to Jenkinsfile_container_plugin.
6. Select Save and run the job.

Verify your web app

1. 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.