CI/CD with Jenkins-x on your existing (GKE) cluster — ruby on rails application
Jenkins X is a CI/CD platform for Kubernetes. This post is a practical approach to get Jenkins-X working on an existing Google Kubernetes Engine cluster.
Jenkins-X is based on helm. Its better to ensure
jx works from your shell. In case if you don’t, just get them installed from their respective github pages releases section. Also, ensure that
helm works on your cluster before setting up Jenkins-x.
This post assumes that you have a running GKE cluster and does not cover creating a fresh Jenkins-X kubernetes cluster. If you don’t have one, create one on a free tier account, just a few clicks away.
Install Jenkins-X on your cluster (jx install)
If you already have a Jenkins-X cluster setup on GKE, ensure you have atleast 3 nodes (6 vCPUs) and skip this section.
Note: You should have atleast 3nodes (6 vCPUs) in your GKE cluster.
jx install and select the relevant options to get the Jenkins-X initialised on your kubernetes cluster.
This step does the following things:
tiller, an ingress controller with external loadbalancer.
jenkins-x-platformto your kubernetes cluster helm repo which brings a ton of resources
- Creates two repositories each for
productionin git and creates pipelines for each of those in jenkins.
Now we have the setup ready to perform CI/CD for any project with our
Prepare the rails project
Get the rails application which you want to perform CI/CD. This does not even have to be rails, it can be any web application. You can either clone your existing application or generate a new one from scratch with
rails new <app>. For the sake of simplicity, I will create a new application.
Import your project into Jenkins-X
jx import <path to app> command to get your project imported.
This step does the following:
- Copies the needed Helm charts, Jenkinsfile, Dockerfile from templates if they do not exist in the repository.
- Creates Jenkins projects and pipelines for master branch and creates web-hook which triggers the jenkins job.
- Creates a pull request PR-1, runs checks, merges and creates the first release
- This is then promoted to staging and you can view how staging environment looks.
The command exists leaving things to run in the background. One can follow the activities, logs etc. from the commands above. E.g.
jx get activity looks like this:
Check the status of the application with
jx get app .
Grab the url of the staging environment and take a look. Mine looks like this:
Looks good, now its time to promote it to production, do it with
jx promote <app> .
jx get app and check if the production url works. Great! Now its time to make a change and see how that fits our GitOps work flow.
Update and publish our welcome page
Lets get to the application and create a
welcome branch and create a welcome controller
Lets make a welcome message with user name and make it as default page. Modifying controller, view and routes.
Test it locally with
rails server command on your shell. If it looks good, then
git push the
And then create a pull request from the
ui . Mine was github and hence I will click this button.
Now a new
PR pipeline kicks in and validates the changes and creates a preview environment which you can see from the pull request from the
ui . From the pull request, you can see this.
On clicking that link,
Alright, looks good, I would like to merge this
PR, which takes this to
staging . The activities and logs can all the time be followed with
jx get build logs and
jx get activity . After the logs and activity ends successfully, check the app status again.
Check the staging env and then promote it to production with
jx promote like before if it looks okay and refresh the production url and voila !
Now we deployed a change from desktop to staging to production with few commands. And all the configs saved in version control. Thats GitOps for you !
- Installed jenkins-x to an existing GKE cluster with
- Imported a project into our jenkins-x cluster with
- Promoted the project to production with
- Made a change, tested it locally, pushed the change as a branch, created pull request, Jenkins-X automatically created
previewenvironment for previewing.
- Accepted the
PRto get that automatically to staging for testing.
- Ran a
jx promoteto promote our new version from staging to production.
Excellent support from
James Strachen and
James Rawlings and other
jx community members. I got round the clock (24x7) support from these expert in Slack
#jenkins-x-dev channels. Hats-off for those guys !
- Installing helm on GKE cluster, a quick hands-on — https://medium.com/@maniankara/helm-on-gke-cluster-quick-hands-on-guide-ecffad94b0
- Jenkins documentation — https://jenkins-x.io/documentation/