In this lab, we will learn to create a build pipeline on OpenShift. This pipeline will be setup using Jenkins running as an application pod on OpenShift. The pipeline running on Jenkins will trigger builds and deployments on OpenShift.
Prerequisites: You need a github account if you want to make changes and test the pipeline kick-off.
Step 1: Create a new project Create a new project with name
pipeline-UserName
. Substitute UserName with your own user name.
Step 2: Deploy your CI/CD tool (Jenkins)
Click on Add to Project
on the Web console, and navigate to
Import YAML / JSON
tab.
In the text box with heading
Upload file by dragging & dropping, selecting it, or pasting from the clipboard
,
copy and paste the Build Configuration the following Build Configuration
and press on Create
.
apiVersion: v1 kind: BuildConfig metadata: name: myfirstpipeline labels: name: myfirstpipeline annotations: pipeline.alpha.openshift.io/uses: '[{"name": "myphp", "namespace": "", "kind": "DeploymentConfig"}]' spec: triggers: - type: GitHub github: secret: secret101 - type: Generic generic: secret: secret101 runPolicy: Serial source: type: None strategy: type: JenkinsPipeline jenkinsPipelineStrategy: jenkinsfile: "node() {\nstage 'build'\nopenshiftBuild(buildConfig: 'myphp', showBuildLogs: 'true')\nstage 'deploy'\nopenshiftDeploy(deploymentConfig: 'myphp')\nopenshiftScale(deploymentConfig: 'myphp',replicaCount: '2')\n}" output: resources: postCommit:
Note that the build configuration uses JenkinsPipelineStrategy
. Also
the spec.strategy.jenkinsPipelineStrategy.jenkinsfile
is the actual
pipeline that runs. We will discuss that in the next step.
This will spin up a Jenkins pod. Give it a few minutes as it takes time
to download and deploy jenkins. You will also see the URL assigned to
your Jenkins. It would be something like
https://jenkins-pipeline-UserName.{{APPS_ADDRESS}}
If you click on that URL, you will be taken to Jenkins Console. Note: The browser may complain that the URL is insecure. Click on advanced and proceed to url.
At the next step, choose Login with OpenShift
and enter your openshift
credentials to login.
On the next screen, allow all Requested Permissions
Now you will be taken to Jenkins console (that is running on OpenShift).
Step 3: Understand the pipeline
Using left menu on the Web Console, select Builds
→`Pipelines`. You
will see that myfirstpipeline
is created. Click on that.
Since we haven’t run the pipeline yet, the History
tab shows nothing.
If you click on the Configuration
tab, you will see the build
configuration details.
Note There are webhook urls on this page. We will come back to this and use after a couple of steps.
Review the Jenkinsfile
on this page.
* It has two stages build
and deploy
* In the build
stage the openshiftBuild
plugin is used to trigger a
build named myphp
* In the deploy
stage the openshiftDeploy
plugin invokes a
deployment configuration myphp
and then invokes openshiftScale
plugin to scale up the same deployment configuration to 2 replicas.
This means that we need a build configuration and deployment
configuration for our application, but we want these triggered by our
pipeline (not auto-triggered by default). So let us create an
application myphp
with the build configuration and deployment
configuration so that we can trigger it using this Jenkins pipeline.
Step 4: Create an application
Clone the following application into your github account https://github.com/VeerMuchandi/bluegreen if you haven’t already done it in a previous lab.
Click on Add to Project
on the project and select PHP
latest image.
We will use myphp
as the application name. Note same name will be
used for buildConfig and deployConfig by default.
Use your own github url if you want to test by pushing changes to code. Otherwise you can use https://github.com/VeerMuchandi/bluegreen.
Now click on the link advanced options
. It will open up a bunch of
options and we will edit those. Under the section Build Configuration
,
uncheck all the three check boxes for the following options
* Configure a webhook build trigger * Automatically build a new image
when the builder image changes * Launch the first build when the build
configuration is created
By unchecking these, we are disabling the start of the builds as we want to control those using pipeline.
In the section Deployment Configuration
, uncheck the two auto-deploy
options * New image is available * Deployment configuration changes
By unchecking these, we are disabling the start of the deployments as we want to control those using pipeline.
Now scroll down and click on the Create
button.
This will create the required objects (build configuration, deployment configuration, imagestream, service and route) but won’t start a build.
Step 5: Start pipeline
Now click on the Start Pipeline
button to start the pipeline.
Notice the pipeline starts and build
and deploy
stages are executed.
Click on the View log
link for the pipeline. It’ll take you to Jenkins
and show the logs.
In a couple of minutes, the build
and deploy
will complete, and your
applicaiton will be deployed and scaled to 2 replicas. Now use the
application to notice that the blue
box is displayed.
Step 6: Configure webhook and trigger with changes (optional)
Navigate back to the configuration
tab for the pipeline as explained
in Step 3. Copy the value for Github webhook
url.
Based on what you learn in the past, go to your github repository that you cloned and set up a webhook pointing to this URL.
Tips
-
Navigate to
Settings
→Webhooks
on your project in github -
Set the`Payload URL` to
Github Webhook
URL noted above -
Make sure the
Content Type
is set toapplication/json
-
Press on
Disable SSL
-
Press on
Add Webhook
Now edit the image.php
file to uncomment the green box and comment the
blue box as shown below
// Draw a filled rectangle //imagefilledrectangle($im, 0, 0, 199, 199, $blue); imagefilledrectangle($im, 0, 0, 199, 199, $green);
and Commit
changes.
Come back and watch the Web Console, you will notice that a new build has just started. Once the build completes, you will also see the rolling deployment of the pods.
Bonus Points: Watch the videos here https://blog.openshift.com/create-build-pipelines-openshift-3-3/ and understand how to create a pipeline that goes across multiple projects.
Congratulations!! In this lab, you have learnt how to set up and run your own CI/CD pipeline on OpenShift.