20 MINUTE EXERCISE
In this lab you will learn about deployment pipelines and you will create a pipeline to automate build and deployment of the Inventory service.
So far you have been building and deploying each service manually to OpenShift. Although it’s convenient for local development, it’s an error-prone way of delivering software if extended to test and production environments.
Continuous Delivery (CD) refers to a set of practices with the intention of automating various aspects of delivery software. One of these practices is called Delivery Pipeline which is an automated process to define the steps a change in code or configuration has to go through in order to reach upper environments and eventually to production.
OpenShift simplifies building CI/CD Pipelines by integrating the popular Jenkins Pipelines into the platform and enables defining truly complex workflows directly from within OpenShift.
The first step for any deployment pipeline is to store all code and configurations in a source code repository. You can use any Git server (e.g. GitHub, BitBucket, etc) for this lab but we have prepared a Gogs git server which you can access here:
{{ GIT_URL }}[{{ GIT_URL }}^]
Click on 'Register'
to register a new user with the following details and then click on
Create New Account:
-
Username: {{OPENSHIFT_USER}}
-
Email: your email (Don’t worry! Gogs won’t send you any emails)
-
Password: {{OPENSHIFT_PASSWORD}}
-
Re-Type: {{OPENSHIFT_PASSWORD}}
You will be redirected to the sign in page. Sign in using the above username and password.
Click on the plus icon
on the top navigation bar and then on New Repository.
Give inventory-thorntail as Repository Name and click on 'Create Repository'
button, leaving the rest with default values.
The Git repository is created now.
Click on the copy-to-clipboard icon
to near the
HTTP Git url to copy it to the clipboard which you will need in a few minutes.
Now that you have a Git repository for the Inventory service, you should push the source code into this Git repository.
Go the inventory-thorntail folder, initialize it as a Git working copy and add the GitHub repository as the remote repository for your working copy.
Important
|
Replace GIT-REPO-URL with the Git repository url copied in the previous steps |
$ cd /projects/labs/inventory-thorntail $ git init $ git remote add origin GIT-REPO-URL
Before you commit the source code to the Git repository, configure your name and email so that the commit owner can be seen on the repository. If you want, you can replace the name and the email with your own in the following commands:
$ git config --global user.name "Developer" $ git config --global user.email "[email protected]"
Commit and push the existing code to the GitHub repository.
$ git add . --all $ git commit -m "initial add" $ git push -u origin master
Enter your Git repository credentials
if you get asked to.
Refresh the page of your 'inventory-thorntail' repository
. You should
see the project files in the repository.
OpenShift has built-in support for CI/CD pipelines by allowing developers to define a Jenkins pipeline for execution by a Jenkins automation engine, which is automatically provisioned on-demand by OpenShift when needed.
The build can get started, monitored, and managed by OpenShift in the same way as any other build types e.g. S2I. Pipeline workflows are defined in a Jenkinsfile, either embedded directly in the build configuration, or supplied in a Git repository and referenced by the build configuration.
Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is created using a scripted or declarative syntax.
In the project explorer in CodeReady Workspaces, right-click 'inventory-thorntail' project and then
on 'New > File' and name it 'Jenkinsfile'
.
Copy the following pipeline definition
into Jenkinsfile.
pipeline {
agent {
label 'maven'
}
stages {
stage('Build JAR') { #(1)
steps {
sh "mvn package"
stash name:"jar", includes:"target/inventory-1.0-SNAPSHOT-thorntail.jar"
}
}
stage('Build Image') { #(2)
steps {
unstash name:"jar"
script {
openshift.withCluster() {
openshift.startBuild("inventory-s2i", "--from-file=target/inventory-1.0-SNAPSHOT-thorntail.jar", "--wait")
}
}
}
}
stage('Deploy') { #(3)
steps {
script {
openshift.withCluster() {
def dc = openshift.selector("dc", "inventory")
dc.rollout().latest()
dc.rollout().status()
}
}
}
}
}
}
This pipeline has three stages <1> Build JAR: to build and test the jar file using Maven <2> Build Image: to build a container image from the Inventory JAR archive using OpenShift S2I <3> Deploy: to deploy the Inventory container image in the current project
Tip
|
The pipeline definition is fully integrated with OpenShift and you can perform operations like image build, image deploy, etc directly from within the Jenkinsfile. |
Tip
|
When building deployment pipelines, it’s important to treat your infrastructure and everything else that needs to be configured (including the pipeline definition) as code and store them in a source repository for version control. |
Commit and push
the Jenkinsfile to the Git repository.
$ git add Jenkinsfile $ git commit -m "pipeline added" $ git push origin master
The pipeline definition is ready and now you can create a deployment pipeline using this Jenkinsfile.
Like mentioned, {{OPENSHIFT_DOCS_BASE}}/architecture/core_concepts/builds_and_image_streams.html#pipeline-build[OpenShift Pipelines^] enable creating deployment pipelines using the widely popular Jenkinsfile format.
OpenShift automates deployments using {{OPENSHIFT_DOCS_BASE}}/dev_guide/deployments/basic_deployment_operations.html#triggers[deployment triggers^] that react to changes to the container image or configuration. Since you want to control the deployments instead from the pipeline, you should remove the Inventory deploy triggers so that building a new Inventory container image wouldn’t automatically result in a new deployment. That would allow the pipeline to decide when a deployment should occur.
Set triggers of Inventory Deployment to manual
and switch off Prometheus Agent
:
$ oc set triggers dc/inventory --manual $ oc set env dc/inventory AB_PROMETHEUS_OFF=true
Deploy a Jenkins server
using the provided template and container image that
comes out-of-the-box with OpenShift:
$ oc new-app jenkins-ephemeral --param=MEMORY_LIMIT="2Gi"
Caution
|
Please modify the 'Resource Limits: jenkins' from the {{OPENSHIFT_CONSOLE_URL}}[OpenShift Web Console^] to define 'CPU Limit = 2' |
After Jenkins is deployed and is running (verify in web console), then create a
deployment pipeline
by running the following command within the inventory-thorntail folder:
$ oc new-app . --name=inventory-pipeline --strategy=pipeline
The above command creates a new build config of type pipeline which is automatically configured to fetch the Jenkinsfile from the Git repository of the current folder (inventory-thorntail Git repository) and execute it on Jenkins.
Go to the {{OPENSHIFT_CONSOLE_URL}}[OpenShift Web Console^]
inside the {{COOLSTORE_PROJECT}} project and from the left sidebar
click on 'Builds >> Pipelines'
Tip
|
Pipeline syntax allows creating complex deployment scenarios with the possibility of defining checkpoint for manual interaction and approval process using the large set of steps and plugins that Jenkins provide in order to adapt the pipeline to the process used in your team. You can see a few examples of advanced pipelines in the OpenShift GitHub Repository. |
Tip
|
In order to update the deployment pipeline, all you need to do is to update the Jenkinsfile in the inventory-thorntail Git repository. OpenShift pipeline automatically executes the updated pipeline next time it runs. |
Manually triggering the deployment pipeline to run is useful but the real goal is to be able to build and deploy every change in code or configuration at least to lower environments (e.g. dev and test) and ideally all the way to production with some manual approvals in-place.
In order to automate triggering the pipeline, you can define a Webhook on your Git repository to notify OpenShift on every commit that is made to the Git repository and trigger a pipeline execution.
You can get see the webhook links in the {{OPENSHIFT_CONSOLE_URL}}[OpenShift Web Console^].
Select 'Build >> Pipelines' menu
, then click on your pipeline
and go to the Configuration tab
.
Warning
|
Copy the Generic Webhook URL which you will need in the next steps. |
Go to your 'inventory-thorntail' repository in {{ GIT_URL }}[Gogs^]
and click on 'Settings'
.
On the left menu, click on 'Webhooks >> Add Webhook >> Gogs'
.
Create a webhook with the following details:
-
Payload URL: paste the Generic webhook url you copied from the inventory-pipeline
-
Content type: application/json
Click on 'Add Webhook'
.
All done. You can click on the newly defined webhook to see the list of Recent Delivery. Clicking on the Test Delivery button allows you to manually trigger the webhook for testing purposes. Click on it and verify that the inventory-pipeline starts running immediately.
Well done! You are ready for the next lab.