Build and Deploy Spring Boot Web Service using Azure DevOps

Build and Deploy Spring Boot Web Service using Azure DevOpsJulius CanuteBlockedUnblockFollowFollowingJun 21I experimented with the Azure DevOps pipeline recently and was amazed by how easy it is to set up and use.

I want to document what was done so that In future, I can reference it back when I want something of a similar sort to be done.

I hope someone finds this useful as well.

Before venturing on this adventure, I decided to set a goal that I wanted to achieve using this experiment.

Goal“I wanted to take a real-world spring boot web service and deploy it using Kubernetes in the Azure infrastructure.

”There are few things I assume you already have if you are following along:* You have an Azure account already set up.

* You have an Azure Dev Ops Account.

There are few things I assume you already know:* You know how to work on the Azure Web Console.

* You know how to use Terraform, and you have configured it for Azure.

In this article, I’ll show the steps to create the Infrastructure, followed by the steps to set up the Azure DevOps Pipeline.

Create Infrastructure using TerrafromFirst, we must prepare the infrastructure to hold the Docker Images pushed from the Azure DevOps pipeline.

For preparing this infrastructure, I have used Terraform, and I am saving my states remotely inside the Azure Storage Container.

Check out the Terraform code from the following repo: https://github.

com/juliuscanute/azure-infrastructure.

2.

Create ‘terraform.

tvars’ inside the state directory.

storage_account_name = "exampleaccount"container_name = "examplecontainer"location = "australiasoutheast"resource_group_name = "example"3.

Comment the following lines in ‘state/main.

tf’ for now.

# terraform {# backend “azurerm” {}# }4.

Execute the following terraform commands to plan and apply the changes to the infrastructure.

terraform initterraform plan –var-file="terraform.

tvars"terraform apply –var-file="terraform.

tvars"After apply step Terraform outputs variables, which we make use of in the subsequent step to upload the state file to the container.

Apply complete!.Resources: 0 added, 0 changed, 0 destroyed.

Outputs:container_name = ####resource_group_location = ####resource_group_name = ####storage_account_name = ####storage_id = ####storage_key = ####5.

We’ll use the information above to upload the state file to the container(remote).

export AZURE_STORAGE_ACCOUNT=<storage_account_name>export AZURE_STORAGE_KEY=<storage_key>az storage blob upload –container-name <container_name> –file terraform.

tfstate –name <example>.

tfstate6.

Create state/terraform.

cfg and uncomment the lines commented in Step 3.

storage_account_name = "<storage_account_name>"container_name = "<container_name>"key = "<example>.

tfstate"access_key = "<storage_key>"7.

Let us verify if Terraform can read and write the remote state file.

terraform init –backend-config="terraform.

cfg"terraform apply –var-file="terraform.

tvars"Apply complete!.Resources: 0 added, 0 changed, 0 destroyed.

8.

Let us add deploy/terraform.

tvars and make sure ‘<example>.

tfstate’ matches with the state file created in the previous steps.

registry_name = "examplerepo"storage_account_name = "<storage_account_name>"container_name = "<container_name>"key = "<example>.

tfstate"accesss_key = "<storage_key>"9.

Let us add deploy/terraform.

cfg and make sure you give a different name to the key.

storage_account_name = "<storage_account_name>"container_name = "<container_name>"key = "<different>.

tfstate"access_key = "<storage_key>"10.

Let us complete the infrastructure set up to hold our docker images by switching to deploy directory and executing the commands shown below.

Make a note of the username and password as we use it to set up the pipeline to push the docker image.

cd .

/deploy/terraform init –backend-config="terraform.

cfg"terraform apply –var-file="terraform.

tvars".

Apply complete!.Resources: 1 added, 0 changed, 0 destroyed.

Outputs:login_server = ####password = ####username = ####Build using Azure PipelineI’ll use the code in the following repository for this pipeline build:https://github.

com/juliuscanute/kotlin-spring-realworld-example-appSelect Pipelines from the Navigation Drawer and click on create New Pipeline as shown.

Create New Pipeline2.

Select where you have hosted your code; in my case, it resides in GitHub.

Source Code Hosting Provider3.

Pick the repository containing the Spring Code.

https://github.

com/juliuscanute/kotlin-spring-realworld-example-app4.

Make sure your repository has a Dockerfile that knows how to package the built JAR.

5.

Configure the Azure Pipeline with the following steps and complete the configuration, while this triggers a Pipeline Job automatically, you can stop it for now as it is going to fail without the credentials.

6.

Now it is time to add the secrets, click on the menu as shown and select Edit pipeline.

7.

Inside the ‘Edit Pipeline’ select the menu again, and select variables.

8.

Create a new variable group.

9.

Key in the username and password you have obtained from Terraform output and save the created variable group.

10.

Link the variable group with the existing pipeline.

11.

Trigger the build again, but this time, it should complete successfully.

12.

You can verify if the Docker Image was pushed to the repository successfully by logging and viewing from the Azure Web Portal.

In the next article, I’ll show you how to use the Docker Image from the repository and deploy it to the Kubernetes Cluster in Azure.

.

. More details

Leave a Reply