Skip to main content

Use Java templates to deploy a service

๐ŸŽฏcontext

You want to deploy a Java service to OpenShift.

Descriptionโ€‹

This How-To shows you how you can use one of the provided Java templates to deploy a service to OpenShift.

Overviewโ€‹

The IBM DevOps Solution Workbench provides three different templates where everything you need to deploy your Java service to OpenShift is already configured. These templates include complete CI/CD pipelines and Kubernetes deployment configurations.

Content of the templatesโ€‹

Each Java template comes pre-configured with the following components:

  • GitLab CI/CD Pipeline (.gitlab-ci.yml): Automated pipeline with four stages:
    • build.app: Compiles the Java application using Maven
    • build.helm: Packages Helm charts for Kubernetes deployment
    • build.and.push.containerimage: Builds Docker images using Buildah and pushes to registry
    • deploy.helm: Deploys the application to OpenShift using Helm
  • Maven Build Configuration (pom.xml): Spring Boot 3.5.7 with Java 21, including all necessary dependencies
  • Dockerfile: Multi-stage Docker build
  • Helm Charts (src/main/helm/): Complete Kubernetes/OpenShift deployment configurations

Different types of templatesโ€‹

The templates are based on a project baseline, each with its own purpose:

  • JAVATEMPLATEDD (Domain-Driven Design): Domain service with Java/Spring Boot based on domain driven design terminology
  • JAVATEMPLATEINTSVC (Integration Service): Integration service with Java/Spring Boot for providing and consuming REST APIs
  • JAVATEMPLATEBASIC (Basic Service): General purpose service with Java/Spring Boot

How to work with a templateโ€‹

In this example we will use the "JAVATEMPLATEINTSVC" asset to create a Hello World API that gets deployed to the OpenShift cluster.

Step 1: Create a new project from the templateโ€‹

  1. In the IBM DevOps Solution Workbench navigate to "Assets" and select "JAVATEMPLATEINTSVC" from the asset catalog.
  2. Create a new project from the asset.

Step 2: Design your serviceโ€‹

Create a "Hello World API" and one "SayHello" method, that returns a hello world message.

โ„น๏ธnote

If the main branch of your project is protected, you need to first create a new branch before making changes.

Step 3: Generate code with AIโ€‹

You can now make use of the integrated project baseline and use the skills provided to generate the code.

  1. Open the project in your IDE and have your coding assistant (GitHub Copilot, Cursor, etc.) ready.
  2. Trigger the code generation by executing the following prompt:
    k5-generate-rest-api
โ„น๏ธnote

As we have only designed a REST API, we are not using the k5-generate prompt. This prompt would include generating the REST API as well as eventing, but because we didn't design events we will just use the k5-generate-rest-api prompt.

  1. The assistant will automatically:
    • Read your design files from /src-design
    • Generate the REST API based on the design
    • Set up Swagger UI
    • Update application configuration files
    • Validate the build with Maven

Optional: Generate additional components separately:

k5-generate-unit-tests  # Generate comprehensive test suite
k5-generate-readme # Generate detailed documentation
โ„น๏ธnote

Always review the AI-generated code carefully before proceeding. The generated code may contain errors or require adjustments. Make sure to test and validate the implementation before deploying.

Step 4: Check CI/CD variablesโ€‹

All deployment variables are pre-configured in your GitLab group. To view them, navigate to Settings -> CI/CD -> Variables in your GitLab group.

Step 5: Commit and push your changesโ€‹

  1. Review the generated code and make any necessary adjustments
  2. Commit your changes:
    git add .
    git commit -m "Implement Hello World API"
    git push
  3. Create a merge request for review

Step 6: Deploy to OpenShiftโ€‹

After pushing your changes, the GitLab CI/CD pipeline will automatically start:

  1. build.app stage: Compiles the application using Maven
  2. build.helm stage: Packages Helm charts
  3. build.and.push.containerimage stage: Creates and pushes Docker image
  4. deploy.helm stage (manual trigger): Deploys to OpenShift

Once the pipeline succeeds, deploy your service:

  1. Navigate to your pipeline in GitLab
  2. Locate the deploy.helm stage
  3. Click the Play button to trigger manual deployment
  4. Monitor the deployment logs for any issues
  5. Verify deployment in OpenShift, by checking the pods in your deployment namespace

Step 8: Test the APIโ€‹

If the deployment is completed, you can test the api by using the Swagger UI of the service:

๐ŸŒŸCongratulations!

You have successfully implemented and deployed a service to OpenShift using an asset provided by the IBM DevOps Solution Workbench.