Recorded Demo : https://youtu.be/XmLg8cntPuk
For this setup purpose we are using Jenkins ver. 2.204.2 (on Mac) & OpenShift v4.3
Jenkins openshift client plugin : https://github.com/openshift/jenkins-client-plugin
Microservice application source code is hosted under v1.0 & v2.0 branch in this repository. Prebuilt application Jar files are hosted under appjars branch.
Step 1 : Download Jenkins
Step 2 : Install Jenkins
Step 3 : Jenkins - OpenShift Client Plugin Installation
Step 4 : Jenkins - Global Tool Configuration
Step 5 : Jenkins - Cluster Configuration
Step 6 : Jenkins openshift setup test - basic new ruby app pipeline
Step 7 : Jenkins (CI) & OpenShift (CD) Advanced Pipeline
Step 7a : Openshift configurations : build skeleton
Step 7b : Test configurations : manually using oc commands
Step 7c : Jenkins : Configure openshift cluster details and run pipeline
Download Jenkins from http://mirrors.jenkins-ci.org/osx-stable/?C=M;O=D
For other platform , download jenkins from https://jenkins.io/download/
Start the installer & follow on screen instructions to complete the Jenkins installation.
You will require "System Administrator" access to install Jenkins.
- Copy the string from /Users/Shared/Jenkins/Home/secrets/initialAdminPassword and paste as administrator password during the installation when asked.
- Choose option : "Install suggested plugins"
- Create an admin user as per your preference
Enable OpenShift Client Jenkins Plugin : https://plugins.jenkins.io/openshift-client/
OpenShift Client Jenkins Plugin Package Download Link : https://updates.jenkins-ci.org/download/plugins/openshift-client/
Github Documentation Link : https://github.com/openshift/jenkins-client-plugin
- Go to Manage Jenkins
- Choose Manage Plugins
- Click on Available Tab
- Use Filter for search "Openshift"
- Select "OpenShift Client", "OpenShift Login", "OpenShift Sync" Plugins
- Download now and install after restart
- Restart Jenkins
- Go to Installed Tab (under managed plugins) & confirm openshift plugins are checked
This step is to configure tools such as java, maven and openshift client (oc) to be used by pipelines. Tools name given under these settings will be called under pipeline tool section.
Go to Manage Jenkins -> Global Tool Configuration
- Click on "Add OpenShift Client Tools"
- Name : oc
- Install automatically : checked
- Click on "Add OpenShift Client Tools
- Choose "Extract *.zip/*.tar.gz"
- Label : Leave empty
- Download URL for binary archive : https://mirror.openshift.com/pub/openshift-v4/clients/oc/latest/macosx/oc.tar.gz ( !! Important - Use the oc url as per your architecture !!)
- Subdirectory of extracted archive : Leave empty
- Click Apply
- Click on "Add JDK"
- Name : jdk8
- Install Automatically : uncheck
- JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/ (!! Important - Change this to your installation directory location !!)
- Click Apply
- Click on "Add Maven"
- Name : maven-3.6.3
- Install automatically : checked
- Install from Apache Version : 3.6.3
- Click Apply & Save
Go to Manage Jenkins -> Configure System
- Cluster Configurations : Click on "Add OpenShift Cluster"
- Cluster Name : openshift-cluster
- API Server URL : https://api.cluster-8a3a.sandbox956.opentlc.com:6443 (!! Important - Change this to your url !!)
- Disable TLS Verify : checked
- Credentials : Click on "Add"
- Kind : "OpenShift Token for OpenShift Client Plugin"
- Token : Paste your token here (!! Hint - Get token from $oc whoami -t !!)
- ID : my-user-token
- Click "Add"
- Credentials : Choose from drop down "my-user-token"
- Click on Apply & Save
Let's create a new pipeline in Jenkins to test above done configurations. This pipeline will use parameters to capture namespace and openshift cluster name (as configured above earlier).
A new project/namespace will be created in openshift (so make sure it doesn't exist) and new ruby app will be created using S2I method (code pull from github).
Open Jenkins -> New Item
- Enter an item name : OpenShift Setup Test Pipeline
- Select "Pipeline" & Click "OK"
- Under General : Check / Select "This project is parameterized"
- Click on "Add Parameter" & Choose "String Parameter"
- Name : PROJECT_NAME
- Click on "Add Parameter" & Choose "String Parameter"
- Name : CLUSTER_NAME
- Default Value : openshift-cluster (!! Important - This should match with earlier defined cluster name !!)
- Pipeline
- Script : Paste content from https://github.com/vaibhavjain4/jenkins-demo-app/blob/master/JenkinsOpenShiftConfigurationTestPipeline.txt
- Click on Apply & Save
- Click on Build with Parameters
- PROJECT_NAME : jenkins-setup-test
- CLUSTER_NAME : openshift-cluster
- Click on "Build"
Once build has run successful, go and check openshift for new project and application to confirm.
For this pipeline we will use following usecase :
Use following commands to build openshift skeleton for Jenkins Pipeline to invoke objects. (Note : This can also be done using pipeline but we are using manual method to explain the concepts and artifacts required.
Download and configure oc utility (link given in references section below ) on your local machine/workstation/laptop to execute following command :
Login into OpenShift Cluster
oc login https://api.cluster-8a3a.sandbox956.opentlc.com:6443
Create Two Namespaces
oc new-project my-project-dev
oc new-project my-project-stage
Create empty build, application, service & route in development environment (project). Disable automatic triggers.
oc new-build --name=microservice-app --image-stream=java:8 --binary=true -n my-project-dev
oc new-app --name=microservice-app microservice-app:latest --allow-missing-images -l app=microservice-app -n my-project-dev
oc rollout cancel dc/microservice-app -n my-project-dev
oc set triggers dc/microservice-app --from-config --remove -n my-project-dev
oc set triggers dc -l app=microservice-app --containers=microservice-app --from-image=microservice-app:latest --manual -n my-project-dev
oc expose dc microservice-app --port=8080 -n my-project-dev
oc expose svc microservice-app -n my-project-dev
oc set triggers dc/microservice-app --all -n my-project-dev
Create empty application, service & route in stage environment (project). Disable automatic triggers.
oc new-app --name=microservice-app microservice-app:stage --allow-missing-images -l app=microservice-app -n my-project-stage
oc rollout cancel dc/microservice-app -n my-project-stage
oc set triggers dc/microservice-app --from-config --remove -n my-project-stage
oc set triggers dc -l app=microservice-app --containers=microservice-app --from-image=microservice-app:stage --manual -n my-project-stage
oc create service clusterip microservice-app --tcp=8080:8080 -n my-project-stage
oc expose svc microservice-app -n my-project-stage
oc set triggers dc/microservice-app --all -n my-project-stage
Create Service Account in Development Project. This account will be used from Jenkins to invoke build etc
oc create sa jenkins -n my-project-dev
Give permissions to service account for editing and deploy applications into dev and stage namespaces/projects.
oc policy add-role-to-user edit system:serviceaccount:my-project-dev:jenkins -n my-project-dev
oc policy add-role-to-user edit system:serviceaccount:my-project-dev:jenkins -n my-project-stage
oc policy add-role-to-group system:image-puller system:serviceaccounts:my-project-dev -n my-project-dev
oc policy add-role-to-group system:image-puller system:serviceaccounts:my-project-stage -n my-project-dev
Pre-requisites : Git, Java 8, Maven & oc should be installed on your local machine to execute following steps
Get Service Account 'Jenkins' Token & Login Into OpenShift Using Token
TOKEN=`oc sa get-token jenkins -n my-project-dev`
oc login --token=$TOKEN --server=https://api.cluster-8a3a.sandbox956.opentlc.com:6443
Clone Microservice App Source Code on your local machine
git clone https://github.com/vaibhavjain4/jenkins-demo-app.git -b v1.0
cd jenkins-demo-app/
mvn package
[ STOP Jenkins to start and test application ]
java -jar target/microservice-app-1.0.0-SNAPSHOT.jar
[ Open browser and test http://localhost:8080 ]
Deploy above build jar file into openshift using S2I binary method
oc start-build microservice-app --from-file=target/microservice-app-1.0.0-SNAPSHOT.jar --follow --wait -n my-project-dev
Deploy the application into Development Project
oc rollout latest microservice-app -n my-project-dev
Promote & Deploy the application into Stage Project
oc tag my-project-dev/microservice-app:latest my-project-stage/microservice-app:stage
oc rollout latest microservice-app -n my-project-stage
Following command can help to check the status
oc get bc -n my-project-dev
oc get is -n my-project-dev
oc get dc -n my-project-dev
oc get pods -n my-project-dev
oc get route -n my-project-dev
[ Copy the HOST/PORT url into browser to access the deployed application ]
oc get pods -n my-project-stage
oc get route -n my-project-stage
[ Copy the HOST/PORT url into browser to access the deployed application ]
!! IMPORTANT NOTE - Fork a copy of https://github.com/vaibhavjain4/jenkins-demo-app/tree/v1.0 into your git account. !!
Update pom.xml on line number 26 & 30 to your nexus server url. Update AdvancedJenkinsCIOpenShiftCDPipeline.txt file on line number 34 to your sonarqube server.
If Nexus & SonarQube are not available, comment line number 34 & 40 into AdvancedJenkinsCIOpenShiftCDPipeline.txt file.
Jenkins -> Manage Jenkins
- Open Configure System
- Scroll to "OpenShift Client Plugin" Section
- Under Cluster Configurations -> OpenShift Cluster : Click on "Add" & Select "Jenkins"
- Kind : OpenShift Token for OpenShift Client Plugin
- Token : Paste Jenkins Service Account Token Value (Hint : oc sa get-token jenkins -n my-project-dev)
- ID : jenkins-service-account
- Click on Add
- Credentials : Select "jenkins-service-account" from drop down list
- Click on "Apply" & "Save"
Open Jenkins -> New
- Name : "Jenkins CI OpenShift CD Advanced Pipeline"
- Select Pipeline & Click "OK"
- General : Select/Check "This project is parameterised"
- Click "Add Parameter" : Choose "String Parameter"
- Name: GIT_URL
- Default Value: https://github.com/vaibhavjain4/jenkins-demo-app.git [Replace with your forked repo url]
- Click "Add Parameter" : Choose "String Parameter"
- Name: GIT_BRANCH
- Default Value: v1.0
- Click "Add Parameter" : Choose "String Parameter"
- Name: CLUSTER_NAME
- Default Value: openshift-cluster
- Click "Add Parameter" : Choose "String Parameter"
- Name: DEV_ENV
- Default Value: my-project-dev
- Click "Add Parameter" : Choose "String Parameter"
- Name: STAGE_ENV
- Default Value: my-project-stage
- Pipeline : Paste content from https://github.com/vaibhavjain4/jenkins-demo-app/blob/master/AdvancedJenkinsCIOpenShiftCDPipeline.txt
- Click Apply & Save
- Run Pipeline : Click on "Build with Parameters
- Github Documentation Link : https://github.com/openshift/jenkins-client-plugin
- Enable OpenShift Client Jenkins Plugin : https://plugins.jenkins.io/openshift-client/
- OpenShift Client Jenkins Plugin Package Download Link : https://updates.jenkins-ci.org/download/plugins/openshift-client/
- oc tool command download link : https://mirror.openshift.com/pub/openshift-v4/clients/oc/
User approval is required to promote application from development to stage environment. Please click on approve when prompted. Failure of approval will lead to failure of pipeline.
Access the application via routes of development & stage environment route url's.
Run the pipeline again with GIT_BRANCH = v2.0 and notice the change in application version via accessing it browser.