Manage your Java application

Though this tutorial focuses on managing the lifecycle of a Java application with Score and the Humanitec Platform Orchestrator; however, the principles discussed are universally applicable.

Score allows you to describe any workload - regardless of language or framework - in an environment-agnostic way.

The only variations may lie in the build process and the container image that gets generated.

This tutorial is derived from the GitHub repository Deploying a Springboot application with Score.

Introduction

To deploy a Java application using Humanitec and Score, you’ll need to build, package, and deploy your application. This guide will walk you through the entire lifecycle: from code to deployment.

Prerequisites

Ensure you have the following prerequisites in place:

Getting started

This tutorial will demonstrate the steps to deploy a Java application using Score on an environment managed by Humanitec.

Clone the repository

To get started, clone the demo-backstage repository:

# create a directory
mkdir demo-score && cd demo-score
gh repo clone htc-demo-17/demo-backstage
cd demo-backstage/templates/microservice-java-template

Now that you’re in the microservice-java-template directory, you can see something similar to the following file structure:

.github/workflows
src/main
Dockerfile
README.md
build.gradle
humanitec-apps.yaml
pom.xml
score.yaml
settings.gradle

Build the Java application

For Maven-based Java projects, execute the following command to build the artifact:

mvn package

The artifact will be available in the target directory.

Build the Docker image

Building and deploying a cloud-based application typically requires containerization, commonly done with Docker. Follow these steps:

Configure Your Dockerfile

Add the following configuriation to your Dockerfile to build your container:

FROM openjdk:17-alpine

COPY target/my-app.jar my-app.jar

ENTRYPOINT ["java", "-jar", "/my-app.jar"]

The Dockerfile contains the following sections:

  • FROM openjdk:17-alpine: Specifies the base image, which includes Alpine Linux and OpenJDK 17.
  • COPY target/my-app.jar my-app.jar: Copies the artifact into the container and renames it.
  • ENTRYPOINT ["java", "-jar", "/my-app.jar"]: Defines the startup command for the container.

Docker build

Execute the docker build command to build the Docker image:

docker build -t $MY_REGISTRY/$IMAGE:$TAG .

The flags represent:

  • -t $MY_REGISTRY/$IMAGE: Image name, prefixed by the registry URL if not using Docker Hub.
  • :$TAG: Image tag, usually representing the release version.
  • .: Directory containing the Dockerfile (current directory here).

Docker push

Authentication is required before pushing to a registry. Execute the docker login command to authenticate:

docker login -u $USERNAME -p $SECRET_PASSWORD

Push the image using the docker push command:

docker push $MY_REGISTRY/$IMAGE:$TAG

The variables represent:

  • $USERNAME: Username for the registry.
  • $SECRET_PASSWORD: Password for the registry.
  • $MY_REGISTRY/$IMAGE:$TAG: Image name and tag.

Now that your image is built and pushed to the registry, you can deploy it to your environment with Score. Choose to either deploy manually or automate the process with a CI pipeline.

Deploy with your CI pipeline (Optional)

Integrate your CI pipeline with Humanitec to automatically notify it of new image versions. This allows deployments to be triggered automatically. For more information, refer to the Humanitec documentation.

Deploy with Score

Before deploying, generate an API token in Humanitec.

Create the Score Specification file

Define your deployment configuration in a score.yaml file:

apiVersion: score.dev/v1b1

metadata:
  name: # Set by command line

# Define the ports that this service exposes
service:
  ports:
    www:
      port: 80 # The port that the service will be exposed on
      targetPort: 8080 # The default port of tomcat is 8080

# Define the containers that make up this service
containers:
  frontend:
    image: # Set by command line
    variables:
      DB_DATABASE: ${resources.db.name}
      DB_USER: ${resources.db.username}
      DB_PASSWORD: ${resources.db.password}
      DB_HOST: ${resources.db.host}
      DB_PORT: ${resources.db.port}

# Define the resources that this service needs
resources:
  dns: # We need a DNS record to point to the service
    type: dns
  db: # We need a database to store data
    type: mysql

The score.yaml file contains the following sections:

  • metadata: Defines the name of the application.
  • service: Defines the ports that the service exposes.
  • containers: Defines the containers that make up the service.
    • frontend: Defines the name of the container that will be deployed.
      • image: Defines the image that will be deployed.
      • variables: Defines the environment variables that the container needs.
  • resources: Defines the resources that the service needs.
    • dns: Defines the DNS record that will be created.
    • db: Defines the database that will be created.

Now that your score.yaml file is ready, you can deploy your application.

Deploy using Score

To deploy your application, run the following command:

humctl score deploy --org $HUMANITEC_ORG --app $APP_ID --env development --token $HUMANITEC_TOKEN --file score.yaml -p metadata.name=$APP_ID -p containers.frontend.image=$MY_REGISTRY/$IMAGE:$TAG

This command performs the following actions:

  • delta: Applies incremental changes to the application environment.
  • --org $HUMANITEC_ORG: Specifies your Humanitec organization.
  • --app $APP_ID: Names the application within Humanitec’s UI.
  • --env development: Sets the environment as development.
  • --token $HUMANITEC_TOKEN: Uses the API token for authentication.
  • --file score.yaml: Indicates the path to the score.yaml configuration file.
  • --deploy: Initiates the deployment process.
  • -p metadata.name: Overrides the Score file property metadata.name with your application ID.
  • -p containers.frontend.image: Overrides the Score file property containers.frontend.image with the reference to your container image source.

Now that your application is deployed, you can access it by navigating to the URL provided by the DNS resource in the Humanitec UI. As you make code changes, simply rebuild the Docker image with the new tag, push it to the registry, and run humctl score deploy again to deploy the latest version. Humanitec will automatically roll out the new image and restart containers. This allows for fully automated, continuous deployment of your application.

Conclusion

In this tutorial, you learned how to:

  • Build a Java application
  • Build a Docker image
  • Deploy the application using Score and Humanitec Platform Orchestrator
  • Automate deployments using a CI pipeline

Some key advantages of this approach include:

  • Faster feedback loops. Changes are deployed automatically after a successful build, allowing developers to get feedback quicker.
  • Reduced risk. Deployments are automated, reducing the risk of human error.
  • Increased productivity. Developers can focus on writing code instead of managing deployments.
Top