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.
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”.
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 :
- For “Configure via”, select the radio button “Pipeline builder”
- Provide a name of the pipeline (say “java-builder-pipeline”) in the “Name” field.
- In Parameters, click “Add Parameter” to add 4 parameters
- Name: APP_NAME, Description: Name of the application to be deployed, default value: my-java-app
- Name: APP_GIT_REPO, Description: Github repo URL for the application source code, Default value : https://github.com/< your GitHub user >/<your repo name>
- Name: GIT_REVISION, Description: Github repo branch name to deploy, Default value : (blank)
- Name: PROJECT_NAME, Description: Openshift project where the imagestream will be stored in internal registry, Default value : <Your current Namespace>
- For workspace, create a new workspace, by clicking “Add workspace” and provide the name “shared-workspace”
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.
Configure it as follows:
- Display Name: fetch
- Parameters->url : $(params.GIT_REPO)
- Parameter->revision : $(params.GIT_REVISION)
- Workspaces->output : shared-workspace
Leave other values to default. Click on anywhere outside to exit configuration of this 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 :
- Display name: build
- Parameters->Image : image-registry.openshift-image-registry.svc:5000/$(params.PROJECT_NAME)/$(params.APP_NAME):dev
- Workspaces->source : shared-workspace
Leave other values to default. Click on anywhere outside to exit configuration on this 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 :
- Display Name: cleanup-old
- Parameters->Script : oc delete all -l app=$(params.APP_NAME)
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 :
- Display Name: deploy
- 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.
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 :
- Display Name: expose-service
- Parameters->SCRIPT : oc expose service $(params.APP_NAME)
Leave other values to default. Click on anywhere outside to exit configuration on this 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 :
- Display Name: verify-rollout
- Parameters->SCRIPT : oc rollout status dc/$(params.APP_NAME)
Leave other values to default. Click on anywhere outside to exit configuration on this 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 :
- APP_NAME : my-java-app
- GIT_REPO : https://github.com/< your Github user >/<your repo name>
- GIT_REVISION : master
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.
Click on the Logs tab to view the logs of the build process.
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)