Home / Deploying a Java-based Tomcat application to Azure

Overview

In this lab, you will learn how you can use Azure Pipelines to deploy a Java web application to Apache Tomcat with a MySQL database on Azure. Apache Tomcat is an open-source Java Servlet Container developed by the Apache Software Foundation (ASF). MySQL is a very popular open-source relational database management system.

For this lab, you will use Azure App Service and Azure Database for MySQL, a relational database service based on the open source MySQL Server engine. It is a fully managed database as a service, capable of handing mission-critical workload with predictable performance and dynamic scalability.

MS teamsWant additional learning? Check out the Automate Java container deployments module on Microsoft Learn.

What’s covered in this lab

This lab will show how you will

  • Create a new Azure App Service with a MySQL database server and configure the web app to use Apache Tomcat
  • Use Azure App Service Task to deploy a WAR file

Before you begin

  1. Refer the Getting Started page before you follow the exercises.

  2. Use MyShuttle as a template to provision the new Azure DevOps project using the Azure DevOps Demo Generator.

Exercise 1: Creating Azure Web App and MySQL database

  1. Launch the Azure Cloud Shell from the portal. To deploy to a resource group, enter the following command

    az group create --name MyResourceGroup --location westus

  2. To create an App service plan

    az appservice plan create --resource-group MyResourceGroup --name MyPlan --sku S1

  3. Create the web app with a unique app name

    az webapp create --resource-group MyResourceGroup --plan MyPlan --name MyUniqueAppName

  4. Finally, create the MySQL server with a unique server name.

    az mysql server create --resource-group MyResourceGroup --name mysqldbserver --admin-user mysqldbuser --admin-password P2ssw0rd@123 --sku-name GP_Gen5_2

  5. Navigate to the resource group that you have created. You should see a Azure Database for MySQL server provisioned. Select the database server.

    Resource Group

  6. Select Properties. Save the Server name and Server admin login name to a notepad.

    Database properties

    In this example, the server name is myshuttle-1-mysqldbserver.mysql.database.azure.com and the admin user name is mysqldbuser@myshuttle-1-mysqldbserver.

  7. Select Connection security. Enable Allow access to Azure services toggle and Save the changes. This provides access to Azure services for all the databases in your MySQL server.

Exercise 2: Updating the App Settings for the Web App

Next, navigate to the Web app that you have created. As you are deploying a Java application, you need to change the web app’s web container to Apache Tomcat.

  1. Select Configuration. Set the Stack settings as shown in below image and click Save.

    Setting Web container to Tomcat

  2. Select Overview and click Browse.

    Default Java App

    The web page will look like the below image.

    Default Java App

    Next, you need to update the connection strings for the web app to connect to the database correctly. There are multiple ways you can do this - but for the purpose of this lab, you will take a simple approach by updating it directly on the Azure portal.

  3. From the Azure portal, select the Web app you provisioned. Go to Configuration | Application settings | Connection strings and click on + New connection string.

  4. In Add/Edit connection string window, add a new MySQL connection string with MyShuttleDb as the name, paste the following string for the value and replace MySQL Server Name, your user name and your password with the appropriate values. Click Update.

    jdbc:mysql://{MySQL Server Name}:3306/alm?useSSL=true&requireSSL=false&autoReconnect=true&user={your user name}&password={your password}

    DB Connection

    • MySQL Server Name : Value that you copied previously from the MySQL server Properties.
    • your user name : Value that you copied previously from the MySQL server Properties.
    • your password : Value that you provided during the creation of MySQL database server.
  5. Click on Save to save the connection string.

    String conStr = System.getenv("MYSQLCONNSTR_MyShuttleDb");

You have now setup and configured all the resources that is needed to deploy and run the MyShuttle application.

Exercise 3: Deploy the changes to Web App

  1. Select Pipelines | Pipelines. Choose the pipeline MyShuttleBuild and click Edit Pipeline to view the build definition.

    Builds

    The lab uses the standard Maven build template to compile the code, copy and publish the resulting artifacts for deployment. An additional file which is copied here is the CreateMYSQLDB.sql file which creates a MySQL database and inserts a few records into it during the deployment.

  2. Click Queue to trigger the build and wait for the build to complete.

    Queue Build Queue Build 2

  3. Once the build succeeds, select Releases under Pipelines.

  4. Select MyShuttle Release and click Edit to open the release definition.

    Edit MyShuttle Release Definition

  5. Make sure the artifact is pointing to the Build artifact as shown below. If you are following this lab from Jenkins hands-on-lab, make sure the artifact is pointing to Jenkins.

    Team Build Artifact

  6. Click Tasks. Select Execute Azure MySQL : SqlTaskFile task and provide the following details.

    • Azure Subscription Details : Select the appropriate subscription. Click Authorize and login to your Azure subscription in the pop-up window.
    • Host Name : Select the MySQL Database server host name that was created.
    • Server Admin Login : Provide the Server admin login name that you noted down previously.
    • Password : Provide the password that you created during the creation of MySQL server in the Azure portal.

    Execute Azure MySQL Task

  7. Select the Deploy Azure App Service task, choose the Azure subscription details and select App Service name from the dropdown.

  8. Click on Save and then +Release | Create Release to start a new release

    MyShuttle Release Definition

  9. Wait for the release to complete. Then navigate to the Web App and select the URL from the overview blade. Add /myshuttledev context to the URL. For instance - http://myshuttle1.azurewebsites.net/myshuttledev

  10. Select Login and try logging in to the site with any one of the following credentials.

    Username Password
    barney barneypassword
    fred fredpassword
  11. If your database was setup correctly and the connection parameters are valid, you should be able to login to the portal.

    MyShuttle page after login

Summary

In this lab, you have learnt how to deploy a Tomcat based Java application with MySQL database on Azure with Azure Pipelines.