Skip to content

Latest commit

 

History

History
219 lines (154 loc) · 6.92 KB

19_Creating_a_Pipeline.adoc

File metadata and controls

219 lines (154 loc) · 6.92 KB

Creating a Pipeline

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: From a Web Browser Create a new project pipeline-UserName.

Step 2: Deploy your CI/CD tool (Jenkins)

Click on Add to Project→Import YAML / JSON.

Copy and paste the text below into box, then click "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-<your_project>.apps.rhpds.openshift.opentlc.com/

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

If you haven’t already done it in a previous lab Login to your github repository and clone the following application into your github account

From within the OpenShift Console, click on Add to 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.

image

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

Using the menu on the left, click on "Builds→Pipelines". There should be one pipeline visible. Click on the "Start Pipeline" button.

image

Notice the pipeline starts and build and deploy stages are executed.

image

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.

Builds→Pipeline → Click on Pipeline and then click on Configuration tab.

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 SettingsWebhooks on your project in github

  • Set the`Payload URL` to Github Webhook URL noted above

  • Make sure the Content Type is set to application/json

  • Press on Disable SSL

  • Press on Add Webhook

Now edit the image.php file. Go to line 6 and make a copy of the text, and paste it below line 6. Then comment out line 6, and modify line 7 to convey the color green. The code would look like the text below.

//$deployment = getenv("COLOR");
$deployment = 'green';

Commit the 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.

Let’s clean up your project prior to continueing.

$ oc delete project pipeline-UserName