Jaa


Implement a CI/CD pipeline to build and deploy your Java app to Azure

TFS 2015

Azure Pipelines provides a highly customizable continuous integration (CI) and deployment (CD) automation system for your Java apps. This quickstart shows how to set up CI and CD to deploy a Java app. to an Azure Web App. You create the web app using the Azure CLI, then set up CI/CD in Azure Pipelines. In the CI pipeline, you build the Java app and run JUnit tests using Maven. In the CD pipeline, you deploy the build to an Azure Web App.

java web app

Get ready to go

Before you begin, make sure that you are:

  • A co-administrator of the Azure subscription. If you don't have an Azure subscription, you can get one free through Visual Studio Dev Essentials.

  • An administrator in Azure DevOps. If you don't yet have an organization, you can create one for free.

  • Using the New Release pipeline Editor that is currently in preview.

    preview features action in profile menu

    new release pipeline editor preview on

Create an Azure Web App using the CLI

Note

If you already have a web app that you want to use, you can skip this and move to the next section.

The Azure Cloud Shell is a free Bash shell that you can run directly within the Azure portal. It has the Azure CLI preinstalled and configured to use with your Azure subscription. Click the Cloud Shell button on the menu in the upper-right of the Azure portal.

Cloud Shell

The button launches an interactive shell that you can use to run all of the following steps:

![Screenshot showing the Cloud Shell window in the portal]((https://docs.microsoft.com/azure/includes/media/cloud-shell-try-it/cloud-shell-safari.png)

Create a resource group with the az group create command. The following example creates a resource group named myResourceGroup in the eastus location.

az group create --name myResourceGroup --location eastus

Create an App Service plan with the az appservice plan create command. The following example creates an App Service plan named myAppServicePlan in the Free pricing tier:

az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku FREE

Create a web app in the myAppServicePlan App Service plan with the az webapp create command. In the following command, replace <app_name> with a unique name (valid characters are a-z, 0-9, and -). If <app_name> is not unique, you get the error message "Website with given name <app_name> already exists." The default URL of the web app is https://<app_name>.azurewebsites.net.

az webapp create --name <app_name> --resource-group myResourceGroup --plan myAppServicePlan

Set up the Java runtime configuration that your app needs with the az appservice web config update command. The following command configures the web app to run on a recent Java 8 JDK and Apache Tomcat 8.0

az webapp config set --name <app_name> --resource-group myResourceGroup --java-version 1.8 --java-container Tomcat --java-container-version 8.0

Import sample code into your Azure Repos Git repo

In the Azure Repos portal, navigate to the Code hub for your new project. Click the default repository and then click Import repository.

Screenshot showing menu item to import a repository

On the Import a Git repository dialog box, paste the following for Clone URL.

https://github.com/Adventworks/java-sample

Click Import to copy the sample app code into your Azure Repos Git repository.

Set up continuous integration

Under the Build and Release menu, select Builds then New to create a new build definition.

Create a new build definition for a repository

Select a source and chose your team project, repository, and default branch then select Continue.

Select a template or start with an Empty process to create your build pipeline.

In the right panel, search for java, select Maven, and then click Apply.

You now see all the tasks that were automatically added to the build pipeline by the template. These are the tasks that will automatically run every time you push code changes.

For the Agent queue, select Hosted VS2017.

Set up continuous deployment

Once the build succeeds, click the Release action on the build summary page.

On the Create release pipeline dialog box, click Yes.

In the Select a Template panel, click the Azure App Service Deployment template, and then click Apply.

apply azure app service deployment template

Click Tasks, and then the Deploy Azure App Service task.

Configure the inputs for the Deploy Azure App Service task in the release pipeline. First, select the name of the Azure subscription to which you would like to deploy the application. If there is an Authorize button next to the input, click on it to authorize Azure Pipelines to connect to your Azure subscription.

authorize azure subscription in new release pipeline

Note

If you don't see the Azure subscription in the menu, make sure you are a co-administrator of the Azure subscription you want to use. You can create your own Azure subscription for free.

Next, select the name of the Azure Web App you created for the App service name.

Select the artifact trigger and make sure the Continuous deployment trigger is enabled.

build artifact trigger in release pipeline

Click Save. In the Save dialog box, click OK.

To test the release pipeline, click Release and then Create Release.

create release

On the Create new release dialog box, click Queue.

You will notice a new release being created. Select the link to navigate to the release.

new release created message

You can watch the live logs for the deployment as it happens. Wait for the release to be deployed to the Azure Web App.

Update to redeploy the code

Navigate to the Code hub in the Azure DevOps portal. Navigate to src/main/webapp/index.jsp file. Make the following simple change to that file by selecting the edit action.

Screenshot showing update to code

Change the following line of text:

<h2>Hello World!</h2>

to the following:

<h2>Demo of CI/CD!!</h2>

Commit your changes in Git. This change triggers a CI build, and when the build completes, it triggers an automatic deployment to Azure Web App.

Browse to the app

Once deployment has completed, open the browser and test your web app.

http://<app_name>.azurewebsites.net

Congratulations! You've deployed changes to your application using CI/CD.

Clean up resources

When no longer needed, you can use the az group delete command to remove the resource group and all related resources.

az group delete --name myResourceGroup