permalink |
---|
/guides/jenkins-integration-with-kabanero/ |
The product delivers a modern DevOps toolchain with pre-built pipelines. Kubernetes-style resources are provided for declaring CI/CD-style pipelines. While it is advantageous to run both CI and CD style pipelines to leverage the power of Kubernetes, some might want to reuse Jenkins assets or skills.
With the product, you can customize the deployment section of the pipeline to use Jenkins.
In this guide, you will learn how to build an image and how to use Jenkins to deploy an application that uses that image. The kabanero-pipelines repository contains a collection of tasks and pipelines that are intended to work out of the box with application stacks to illustrate a CI/CD workflow. The pipelines and tasks can be run manually or by using a webhook. This guide shows you how to use an automated webhook to create and run pipelines.
The following diagram depicts the relationship and flow between the pipeline and Jenkins:
The purpose of the guide is to show an existing Jenkins user or administrator how to build an image with the pipeline and how to deploy an image with Jenkins. Users should have experience with Jenkins and be familiar with the fundamental concepts of microservices, Docker, Kubernetes, and pipelines.
You need the following prerequisites to complete the guide:
-
Kabanero Foundation (cloned and installed)
-
Pipelines (cloned)
-
Appsody (installed)
See each project’s documentation for necessary system requirements.
-
Log in as cluster-admin in the master node of your Kubernetes cluster. The following steps assume that you are logged in as cluster-admin, and the commands are run on the master node of your Kubernetes cluster.
-
While you are logged in as a cluster-admin, run the installation script from the kabanero-operator release:
curl -s -L https://github.com/kabanero-io/kabanero-operator/releases/download/0.3.2/install.sh | bash
-
Create an instance of Kabanero CR by running the command that you see as output near the end of the previous installation script. For example, run the following command:
oc apply -n kabanero -f https://raw.githubusercontent.com/kabanero-io/kabanero-operator/0.3.2/config/samples/default.yaml
-
Run the following command to get the pipelines dashboard URL:
kubectl get routes -n tekton-pipelines
The output looks like the following example:
NAME HOST/PORT PATH SERVICES PORT tekton-dashboard tekton-dashboard-tekton-pipelines.apps.kabanero.os.fyre.ibm.com tekton-dashboard <all>
-
From the pipelines dashboard, go to Webhooks and click on the
+
next to Add Webhook. -
From Webhook Settings, give your webhook a name, repository URL, and access token. The webhook name can be anything. This example uses a Java MicroProfile pipeline, so your codebase should be Java MicroProfile-based. If you don’t have a Java MicroProfile codebase, you can fork this repo. To learn how to create a personal access token for GitHub, see GitHub’s documentation.
-
From Target Pipeline Settings, select
Kabanero
namespace,java-microprofile-build-push-jk-pl
Pipeline,kabanero-pipeline
Service Account, and your OpenShift Registry URL inimage-registry.openshift-image-registry.svc:5000/<projectname>
form. -
Go to the PipelineRuns tab. If you didn’t already create PipelineRuns, the list is empty.
-
In GitHub, go to Settings → Webhooks. You can see your new webhook, which was created by the the pipelines dashboard.
-
Now make any code change in your repository and push the change. Wait a few moments and you can see a PipelineRun start under the PipelineRuns tab on your pipelines dashboard.
-
After the PipelineRun finishes successfully, log in to your OpenShift Registry and find the new image. The image resides under a repository with the same name as your GitHub repository. Note: Kabanero Pipelines will use Project Name as the image name and latest git commit id as the tag; by design.
-
Note: If you use an external image registry, you’ll have to configure the secrets in the pipelines dashboard and you’ll find the image under that external image registry.
-
-
Create a Jenkinsfile: Use the Jenkinsfile template from the reference section of this guide, and change the Docker Hub source to your own image repository. Push this Jenkinsfile to your GitHub repository in the root location.
-
Create a deploy file: On your local environment where your development application is installed, go to your project folder and run the following command to generate an
app-deploy.yaml
file:appsody build
Other tools, including Jenkins, can use this file for development application deployment. Commit and push this file to the root of your project. Jenkins creates the
AppsodyApplication
resource in the OpenShift cluster and uses your application to deploy the application by using theapp-deploy.yaml
file. Updateapp-deploy.yaml
file with your desired image name to match theIMAGENAME
envVar in the Jenkinsfile you will create below.metadata: name: java-microprofile spec:
-
Create a Jenkins project and pipeline: Create a Jenkins (Ephemeral) instance by running the following command:
oc new-app jenkins-ephemeral -n kabanero
Next, in the OpenShift UI go to Networking → Routes, select the Kabanero project, and find the Jenkins URL in the Location column. From Jenkins, create a project and specify GitHub as the source. Provide your GitHub account and repository details and choose multi-branch pipeline creation. Jenkins automatically detects the Jenkinsfile in your GitHub repository and starts the deployment process.
-
Verify deployment: Go to Networking → Routes and select the Kabanero project to see the successful deployment. You can also see the URL of the running application by going to Networking → Routes.
-
The following file is a sample Jenkinsfile that you can use to set up your initial Jenkinsfile for the guide. The user might need to adjust the following values: envVars: [ envVar(key: 'TAG', value: 'latest'), envVar(key: 'IMAGENAME', value: 'java-microprofile'), envVar(key: 'PROJECT', value: 'kabanero')])
podTemplate(label: 'label', cloud: 'openshift', serviceAccount: 'kabanero-pipeline', containers: [ containerTemplate(name: 'kubectl', image: 'lachlanevenson/k8s-kubectl', ttyEnabled: true, command: 'cat', envVars: [ envVar(key: 'TAG', value: 'latest'), envVar(key: 'IMAGENAME', value: 'java-microprofile'), envVar(key: 'PROJECT', value: 'kabanero')]) ]){ node('label') { stage('Deploy') { container('kubectl') { checkout scm sh 'sed -i -e \'s#applicationImage: .*$#applicationImage: image-registry.openshift-image-registry.svc:5000/\'$PROJECT\'/\'$IMAGENAME\':\'$TAG\'#g\' app-deploy.yaml' sh 'cat app-deploy.yaml' sh 'find . -name app-deploy.yaml -type f|xargs kubectl apply -f' } } } }
-
This guide was tested on the following product versions: OpenShift 4.2 Platform, Kabanero Foundation 0.3.1, Tekton 0.7, Jenkins ver. 2.176.3