Creating a CI pipeline for a Java application with Tekton on Openshift - Part 1
Tags: java, tekton, openshift, cicd
Published: 2023-09-04 09:55:46


![CI/CD Pipelines with Tekton on Openshift](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2b1ra7t23rdp045zhg2.png)
## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eplv2v5psg2seetv5yi3.png)
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)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wgpsqs2jgot4tik7rbyk.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0ht1q7yt8jgp1uioy3x.png)


![Creating the pipeline (continued)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4bb03wy7pw950x50treh.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhvayb1a329m3a4t40fy.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7nogp0n7mimqhfofccap.png)

#### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cgbzjpwi257xjfwrrdi4.png)

#### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bythnv8dzkfzl7wvp8lo.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gglc08a54jobni62wtdq.png)

#### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7i8sv01qsjy90uwcxw7i.png)

#### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tab8vzib1jljpyv5sfk.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kii1hotk8a0ymh0z29qn.png)


![Gotta fillup the form :-)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0mfo4somc2afm2jqofak.png)

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 !!](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20hsx8z1a2qdjn2gpioc.png)

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


![Viewing logs of the pipeline execution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6tkv6cx9tahrmrjuei69.png)

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