This is a modified version of the code for Getting started tutorial for Liberty for use in IBM Cloud Private developer training.
The main difference with this version is that it does not use an external IBM Cloudant service but rather uses a containerized version of Cloudant called Cloudant for Developers so it can be deployed in it's entirety to the IBM Cloud Private Kubernetes environment.
This version of the Liberty app also uses the Cloudant Java Client to add information to a database and then return information from a database to the UI.
This app adheres to best practices of the 12-Factor app. A detailed checklist can be found here.
The following steps are the general procedure to set up and deploy your app to IBM Cloud. See more detailed instructions in the Getting started tutorial for Liberty.
You'll need IBM Cloud Private, the IBM Cloud Private CLI and the command line tools kubectl, and helm.
-
Go to the repository located at https://github.com/odrodrig/get-started-java-icp
-
Sign into GitHub if you haven't already
-
Click on the Fork button at the top right of the page
This will create a copy of the application in your GitHub account for you to be able to modify
-
Go to Blue Demos and request an instance of ICP
-
You should get an email with a link to the environment and a password to sign in with.
-
Once logged into your IBM Cloud Private virtual environment, click on the profile icon at the top right of the dashboard and select Configure client.
- Copy all the lines or just click on the double page icon to copy all lines to the clipboard.
- Then, open up the terminal by either double clicking the icon on the desktop or by clicking on the blue button at the top left of taskbar and selecting terminal emulator.
- Paste in the lines that were copied in the previous step.
You should see Switched to context "cluster.local-context" when done.
- Then, CD into your ~/Documents/ directory and clone the GitHub project that you forked earlier. You can find this by navigating to the repository in your browser and clicking on the green Clone or Download button on the right side of the page.
To clone the repo use the following comand replacing <url to repo> with the url that you copied.
git clone <url to repo>
-
Next, change directory in the repository that was cloned and find the /setup/ directory.
-
From the /setup/ directory run the following command:
bash setup.sh
This setup script will install the IBM Cloud Private plugin for the command line and configure it to point to the cluster within ICP. This also properly configures helm to work with ICP.
- CD into the /chart/liberty-starter/ directory of this repository and run the following command:
helm install . --tls --name liberty-starter
This will deploy the java application as well as a cloudant database within the kubernetes cluster.
In this section we will be connecting our cloned GitRepo to a Continuous Integration/Continuous Deployment pipeline built with Jenkins. This pipeline contains 4 different steps as follows:
Stage | Purpose |
---|---|
Build Application War File | Pulls in dependencies from Maven and packages application into .war file |
Build Docker Image | Builds the Docker image based on the Dockerfile |
Push Docker Image to Registry | Uploads the Docker image to the Docker image registry withinin ICP |
Deploy New Docker Image | Updates the image tag in the Kubernetes deployment triggering a rolling update |
More details of this pipline can be found in the Jenkinsfile.
- Jenkins is already installed in this instance of IBM Cloud Private. To access it, open the Firefox browser and click on the Jenkins bookmark from the bookmark bar.
- Log into Jenkins. The username and password should be autofilled for you. If not, the username should be "admin" and the password is stored in a kubernetes secret. To view the secret, run the following command in the terminal:
printf $(kubectl get secret --namespace default jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo
- Click on New Item, enter "liberty_starter_pipeline" for the name, and select Pipeline. When done, click ok.
-
Find and click on the Configure button on the left side of the page.
-
Scroll down to the Pipeline section and find the definition drop down menu. Select Pipeline script from SCM and for SCM select Git.
-
For Repository URL enter the url to the cloned repository that you forked earlier. Ensure the /master branch is being targeted and click Save.
Now with our pipeline configured we can make a change and deploy the new version.
- On your local machine, clone the repo that you forked before. To clone the repo use the following comand replacing <url to repo> with the url that you copied.
git clone <url to repo>
-
Open the application in a code editor or IDE of your choice. For this lab we will be making a change to the index.html page to change the display language.
-
Open /src/main/webapp/index.html and on line 2 locate the <html lang="es"> tag.
- Change the "es" to one of the following:
- en: English
- es: Spanish
- pt: Portuguese
- fr: French
- ja: Japanese
This will change the language that loads on the webpage to whatever language you selected.
- Save the file and push to github.
Since this instance of ICP is for demos, it can't be accessed by github which makes automatic deployments not possible on code commits but we can manually trigger pipeline builds.
-
Swich back to ICP and open the pipeline that was created earlier.
-
Click on the Build now button to manually start the build process. This will pull the latest code from your code repository on GitHub.
- Your pipeline will now start building. You should see the different stages appear on the pipeline page.
- When the pipeline is finish deploying, open up the app from ICP and you can see that the language of the page has been changed.