DEV Community

Sumit Mukherjee
Sumit Mukherjee

Posted on

Creating a CI pipeline for a Java application with Tekton on Openshift - Part 1

CI/CD Pipelines with Tekton on Openshift

Creating a CI pipeline for a Java application with Tekton on Openshift - Part 1

In this article, we will enumerate the step-by-step process of creating a basic Java based application with fetch-build-deploy style pipeline. We follow a low-code approach and show how easy it is to create a pipeline visually in Openshift pipeline builder interface.

Prerequisite

On Openshift, Tekton is available as an operator called Openshift Pipelines operator. Ensure it is installed. Verify it is installed by selecting the “Administrator” view and then navigate to Operators -> Installed Operators -> Search for Openshift Pipelines.

Ensure Openshift Pipelines is installed
Tekton also requires a Persistent volume for every pipeline invocation. Hence ensure that a Persistent Volume Claim (PVC) of at least 1GB storage exists on the project. If it does not exist, create one by choosing “Administrator” view and then navigating to Storage -> Persistent Volume Claim -> fill the form to create the PVC and name it as “my-tekton-pvc”.

Create a 1 GB persistent volume (if not available)

Setup

To create a pipeline, choose the “Administrator” view and navigate to Pipelines -> Pipelines and then click on the dropdown button “Choose” and select “Pipeline”.

A pipeline builder form opens, configure the below :

  1. For “Configure via”, select the radio button “Pipeline builder”
  2. Provide a name of the pipeline (say “java-builder-pipeline”) in the “Name” field.
  3. In Parameters, click “Add Parameter” to add 4 parameters
    1. Name: APP_NAME, Description: Name of the application to be deployed, default value: my-java-app
    2. Name: APP_GIT_REPO, Description: Github repo URL for the application source code, Default value : https://github.com/< your GitHub user >/<your repo name>
    3. Name: GIT_REVISION, Description: Github repo branch name to deploy, Default value : (blank)
    4. Name: PROJECT_NAME, Description: Openshift project where the imagestream will be stored in internal registry, Default value : <Your current Namespace>
  4. For workspace, create a new workspace, by clicking “Add workspace” and provide the name “shared-workspace”

Creating the pipeline

Creating the pipeline (continued)
This completes the basic definition of a skeleton Tekton pipeline. Now we have to add pipeline tasks for the clone-build-deploy cycle.

Pipeline Tasks

Task 1 : Retrieve source code from github

On the Pipeline builder, click “Add Task”. Type “git clone”. Select task from Red Hat and click on add.

Adding tasks to pipeline

Configure it as follows:

  1. Display Name: fetch
  2. Parameters->url : $(params.GIT_REPO)
  3. Parameter->revision : $(params.GIT_REVISION)
  4. Workspaces->output : shared-workspace

Leave other values to default. Click on anywhere outside to exit configuration of this task.

configure git-clone task

Task 2 : Build source code

Hover on the “fetch” task and click on the blue “+” sign on the right side. This adds another task. Click on the new “Add Task” button and type “S2I Java” in the filter. Select the option from Red Hat and click on “Add”. Click on the task again to open the configuration of the task and then configure it as below :

  1. Display name: build
  2. Parameters->Image : image-registry.openshift-image-registry.svc:5000/$(params.PROJECT_NAME)/$(params.APP_NAME):dev
  3. Workspaces->source : shared-workspace

Leave other values to default. Click on anywhere outside to exit configuration on this task.

Configure S2I Java task

Task 3 : Remove existing deployment

Hover on the “build” task and click on the blue “+” sign on the right side. This adds another task. Click on the new “Add Task” button and type “Openshift Client” in the filter. Select the option from Red Hat and click on “Add”. Click on the task again to open the configuration of the task and then configure it as below :

  1. Display Name: cleanup-old
  2. Parameters->Script : oc delete all -l app=$(params.APP_NAME)

Configuring remove existing task

Leave other values to default. Click on anywhere outside to exit configuration on this task.

Task 4 : Deploy the built code

Hover on the “cleanup-old” task and click on the blue “+” sign on the right side. This adds another task. Click on the new “Add Task” button and type “Openshift Client” in the filter. Select the option from Red Hat and click on “Add”. Click on the task again to open the configuration of the task and then configure it as below :

  1. Display Name: deploy
  2. Parameters->SCRIPT : oc new-app --name $(params.APP_NAME) --as-deployment-config image-registry.openshift-image-registry.svc:5000/$(params.PROJECT_NAME)/$(params.APP_NAME):dev

Leave other values to default. Click on anywhere outside to exit configuration on this task.

Configure deploy pipeline task

Task 5 : Expose service over route

Hover on the “deploy” task and click on the blue “+” sign on the right side. This adds another task. Click on the new “Add Task” button and type “Openshift Client” in the filter. Select the option from Red Hat and click on “Add”. Click on the task again to open the configuration of the task and then configure it as below :

  1. Display Name: expose-service
  2. Parameters->SCRIPT : oc expose service $(params.APP_NAME)

Leave other values to default. Click on anywhere outside to exit configuration on this task.

Configure expose service task

Task 6 : Verify deployment

Hover on the “expose-service” task and click on the blue “+” sign on the right side. This adds another task. Click on the new “Add Task” button and type “Openshift Client” in the filter. Select the option from Red Hat and click on “Add”. Click on the task again to open the configuration of the task and then configure it as below :

  1. Display Name: verify-rollout
  2. Parameters->SCRIPT : oc rollout status dc/$(params.APP_NAME)

Leave other values to default. Click on anywhere outside to exit configuration on this task.

Configure the verify rollout task

Click on the blue button “Create” to create the pipeline.

Executing the pipeline

Open the “Developer” view, click on “Pipelines” and then select the created pipeline “java-builder-pipeline”. On the right hand side, from the dropdown named Actions, select “Start”. The start pipeline form is displayed for review. Ensure that the parameters populated are correct :

  1. APP_NAME : my-java-app
  2. GIT_REPO : https://github.com/< your Github user >/<your repo name>
  3. GIT_REVISION : master

Starting the pipeline manually

Gotta fillup the form :-)

For Workspaces, click on the dropdown and select “PersistentVolumeClaim”. Then select the PVC “my-tekton-pvc”, that was created in the prerequisite step. Click on the button called “Start” to start the pipeline.

Let the pipeline begin !!

Click on the Logs tab to view the logs of the build process.

Viewing logs of the pipeline execution

That concludes part 1. In the next part, I will document the steps to create a web hook and integrate with Github, that will automatically trigger this pipeline when any code change is pushed to this repo.

Till next time, Happy Coding !!

Learn more about Tekton : https://tekton.dev
Explore Tekton on Openshift Sandbox here : https://developers.redhat.com/developer-sandbox

Top comments (0)