Simple project in Azure DevOps
We are going to build and deploy a java web app by creating a pipeline in Azure DevOps service
Prerequisites
- Azure subscription is needed. That's it.
What is DevOps
DevOps is a combination of Development and Operations. Coders build the project and Operators deploy and maintain the project.
Azure DevOps has different softwares for both development and operations. For development, we have Azure Boards (like Kanbam board), Azure Repos (like github) etc. For operations, we have Azure pipelines for CI/CD.
In this article, we will mainly focus on operations side. We will be deploying a java webapp to Azure app services via Azure DevOps
Using a Basic Java Web application for our project
I am using a basic java web app for our project. The link to the repository is
This project is maven based web app, created using spring initializer. If you want to know more about how I built the app, you can Click Here.
Azure DevOps Organization
Azure DevOps is a part of Azure but cannot be fully operated from Azure Portal. It has its own interface. So first, visit dev.azure.com. Login with your outlook email, preferably use email that is registered with Azure. Then create an organization. In this organization, you can create multiple projects.
So, let's create a project. I have created a private project called javawebapp in devopsprojectsKVS organization.
Once it's created, open it. You will get a dashboard as shown below
We can see that there are 6 sections on the left menu. As said earlier, Boards and Repos are used for the development process. Now, our main focus is to create a pipeline, so we will be using Pipelines.
But before all of that, first we will create a webapp in Azure because we will be using it in creating the pipeline.
For that, go to the Azure Portal > App Services > Create Web App
Please use the above configuration settings if you are going to use my github code. If you have your own project, you can go with the relevant configuration. After creating the webapp, let's get back to our DevOps dashboard.
Creating a pipeline
There are two types of pipelines: Build pipeline and Release pipeline. Build pipeline is mainly used to create an artifact(package) and the Release pipeline is used to deploy the artifact to a service.
But here, I am going to use a single pipeline for creating the package and deploying it to azure webapp. Although there are many benefits of using Release pipeline for artifact deployment like quality gates etc., as our project is simple I am going to stick with a single pipeline.
Go to Pipelines > Create Pipeline
As our code is in GitHub, we will select it. Then I connected my github account. You can connect your account here or go to your github, install azure pipelines app and connect your DevOps organization.
Now select the repository of your project. If you are using my code, please fork it and select it from your github account.
After selecting the repo, azure analyzes our repo and comes with suggestions.
As you can see, I got 4 suggestions in which the second one is very relevant to our goal. So I am selecting it. It will prompt you to select webapp
I selected my webapp which I just created. After that, you will be displayed a yml file. This is the file which creates the pipeline itself. If you followed my steps, you would get the below code.
Let's analyze our pipeline. We have trigger, variables and stages. trigger and variables are self-explanatory. The important part here is stages.
A pipeline contains stages. Here we have two stages. One for building the artifact and the other one is for deploying.
Firts let’s take a look on Build stage. Each stage comprises of jobs and each jobs comprises of steps and finally, each step comprises of tasks. Here we have one job MavenPackageAndPublishArtifacts. Here we have 3 steps. First one is to build the maven package.
To build a maven package locally, we run the command mvn clean install. But here, we just specify the task name and input file. The rest of the thing will be taken care of by Azure. Now, as a beginner we don't know the task name and its parameters. Here, we can take the help of assistant.
The icon on the right(arrow in a box) opens the assistant. Search maven and select Maven
As you can see, we selected pom.xml file and specified our Goal as package. We can also configure other things like JUnit Tests, Code coverage, Code Analysis as shown above. After configuring everything, we can click on Add and it will appear in the yml file.
After creating the artifact, the next task is to copy the artifact to another folder. Here we copied our war file from the source folder to the target folder. Next we upload our artifact. As said earlier, you can search for copy task using the assistant.
Now our next stage is to deploy the artifact that we just built to azure webapp. We have specified our app name, app type, subscription etc.
In both stages, we have a property called pool. To run any pipeline, we need an agent (server). This agent should have all the required dependencies. For example, here we are building a maven package. So the agent needs maven and also java preinstalled. Otherwise, it can’t create a maven package. Here, in our code, we are using a pool with an agent provided by Azure itself.
Now, save and run the pipeline. A job will get queued. Normally, you get the below error.
It is asking to fill a form. But why?
Go to the project settings which is present on left below and then go to Agent pools in pipeline section. Project Settings -> Pipelines -> Agent Pools
Click on Azure Pipelines and go to Agents section.
This is a microsoft hosted agent. We can use this to run our jobs. But you need access to it.
Go to, Project Settings -> Pipelines -> Parallel jobs.
As you can see, Microsoft-hosted has 0 parallel jobs and Self-Hosted has 1 free Parallel jobs. You can create an agent pool and create a self hosted agent or you can set-up billing for Microsoft-hosted jobs. To set it up, click on change in the Microsoft-hosted section.
To set up billing, you need an Azure Subscription. If you have Free or Pay-as-you-go, you can select it and use the microsoft hosted agent of the default pool. After linking your subscription, see Paid Parallel jobs.
For MS Hosted CI/CD we have free and paid one. For Free, we have up to 1800 minutes. This is not enabled by default. We have to fill a form that was given above and request for it. You can fill it and wait until you get access. Or else, you can change the 0 to 1 in Paid parallel jobs. Now I have done it. And then I forgot it for a week.
As you can see, I got a bill of 840 rupees which is huge for a week. So please monitor the cost, setup budgets and alerts and delete the project if not needed. Click Here for more info.
There is also another way. You can create your own agent(self-hosted). Spin up a free tier VM and then you can configure it as an agent. But as I said earlier, an agent should require the necessary software to run the pipeline. So you need to install java, maven etc before using it. I tried but got errors. So I stuck with the default MS Hosted agent.
Now go to Pipelines and rerun our pipeline. Go inside the pipeline and click on the Build Stage.
At first, your job gets queued. After some time, it runs all the jobs present.
As you can see, our web app is ready with our code. Go to Azure portal and open the web app URL. To vie our application, use /webapp-0.0.1-SNAPSHOT. This is the name of the package we specified in our code and we need to go to that path to view our application.
And that’s t. We created a java package and deployed it to Azure WebApp using Azure DevOps.
Remember to delete all resources when you are done. Or else your billing may continue. You can find your DevOps organization under a Resource Group in your Azure account. Delete it once you are finished.
Finally, thanks for sticking to the end of the article and have a great day!!!