Containerized An Application
In this exercise we will learn how to containerize an application and enable with CICD pipeline.
Step 1: Create projects
If you want to, you can create a new project based on what you have learned in the previous lab. Since we already have a project we will use it. Run the following command to make sure.
Important
|
Please replace username with the username assigned to you in the commands below. |
To create a new project"
-
Click
Create Project
-
Enter project information, i.e.
lab1-username
-
Click
Create
Step 2: Create Application
-
Click
Add to project
if you are not already in the Browse Catalog. -
Click
PHP
under Browse Catalog -
Click
Select
in the PHP box -
Enter: myapp as name
-
Enter: https://gitlab.caascloud.net/shannachan/welcome-php.git as Git Repository URL
IMPORTANT: please fork this git repo if you like to use this php code and use your repo URL for this Git Repo URL
-
Click
advance options
-
Click
Create New Secret
(if you git repository require credential) -
Enter Secret name
-
Use
Basic Authentication
as Authentication Type -
Enter your username for gitlab
-
Enter your password
-
Click
Create
-
Click
Create
-
Click
Continue to Overview
-
Wait until the build is down. You will see a blue solid circle on Overview page
-
Click the link on the top right corner
http://myapp-lab1-username.app.caascloud.net
to view your php application
Step 3: Create QA project
-
Click
Create Project
-
Enter project name as
qa-<username>
-
Click
Create
-
Click back to the top left corner the
home
icon
Step 4: Create CICD pipeline
-
Make sure you are in
lab1-username
project -
Once you are inside a project, you will click
Add to project
-→Import YAML/JSON
tab -
Copy the information below and update the context with your information
Important
|
Please update the following |
-
replace
lab1-username
andqa-username
to match your project name -
replace
myapp
must match your application name
---
apiVersion: v1
kind: BuildConfig
metadata:
annotations: ~
labels:
app: jenkins-pipeline-example
name: sample-pipeline
template: application-template-sample-pipeline
name: sample-pipeline
spec:
nodeSelector: ~
output: {}
postCommit: {}
resources: {}
runPolicy: Serial
source:
type: None
strategy:
jenkinsPipelineStrategy:
jenkinsfile: |
node {
stage 'buildInDev'
openshiftBuild(namespace: 'lab1-username', buildConfig: 'myapp', showBuildLogs: 'true')
stage ('deployInDev')
openshiftDeploy(namespace: 'lab1-username', deploymentConfig: 'myapp')
stage ('tagImage')
openshiftTag(namespace: 'lab1-username', sourceStream: 'myapp', sourceTag: 'latest', destinationNamespace:'qa-username', destinationStream: 'myapp', destinationTag: 'promoteToQA')
stage ('deployInQA') {
input 'Promote Dev Image to Testing?'
sh "oc project qa-username"
sh "oc new-app myapp:promoteToQA"
openshiftScale(namespace: 'qa-username', deploymentConfig: 'myapp',replicaCount: '2')
sh "oc expose svc myapp"
}
}
triggers:
-
generic:
secret: efe7f857458df9c3
type: Generic
-
github:
secret: 0084feefe5b6b26c
type: GitHub
type: JenkinsPipeline
triggers: []
-
Paste below onto the text area
-
Click create
-
To view your newly created pipeline, click 'Builds' -→
Pipelines
-
Click on the name of the pipeline
sample-pipeline
-
Click
Configuration
tab to view -
Click
Action
(at the top right) -→ Edit to edit the pipeline
Step 5: Grant Permission to the projects
Important
|
Please update the following |
-
lab1-username must match the project name
-
qa-username must match created from step 4
-
Please replace the <lab1-username> and <qa-username> with appropriate project name.
-
Switch to the command line
-
Execute the following via OC CLI:
``` $oc login -u <your_username> https://openshift.caascloud.net:8443 $oc project <lab1-username> $oc policy add-role-to-user edit system:serviceaccount:<lab1-username>:jenkins -n <lab1-username> $oc policy add-role-to-user edit system:serviceaccount:<lab1-username>:jenkins -n <qa-username> $oc policy add-role-to-group system:image-puller system:serviceaccount:<qa-username> -n <lab1-username>
```
-
Step 6: Build Triggers (GitLab Integrations)
-
Login OpenShift Web UI (https://openshift.caascloud.net:8443)
-
Click on
lab1-username
project -
Click
Builds
-→Pipelines
-
Click
sample-pipeline
or your pipeline’s name -
Click
Configuration
tab -
Copy
Generic Webhook URL
under Triggers -
Login to https://gitlab.caascloud.net/
-
Click into the project that you use to build your application
Step #4
-
Click
Settings
-
Click
Integration
-
Paste the
Generic Webhook URL
fromstep 6
above to the URL field -
Uncheck
Enable SSL verification
-
Click
Add webhook
Step 7: Automatically Build new containers and shift workload to new container
-
Run the following to cleanup the qa-<username> projects
``` oc login -u <username> https://openshift.caascloud.net:8443 oc project qa-<username> oc delete all --all ```
-
Make changes to the code to index.php or info.php you can make changes via the gitlab web interface via
Edit
``` $ git clone https://gitlab.caascloud.net/<yourname>/welcome-php.git <make code changes> with index.php $ git add <update files> $ git commit -m "description of the commit" $ git push -u origin master ```
-
Execute the following
``` git add <file-that-has-changes> git commit -m "update files" git push -u origin master ``` Once the new source is pushed, the pipeline will be triggered to rebuild.
-
Login to https://openshift.caascloud.net:8443
-
Click onto your project
-
Click
Builds
-→Pipelines
-
Click
Input Required
from your pipeline build -
Click
Login Openshift
-
Click allow access
-
Click
Proceed
-
take few minutes to promote the image to qa project
-
Click to qa-username project
-
Application is deployed and scale up.
Step 8: Scaling/Decommissions
-
Switch back command line terminal
-
Execute the following via OC CLI:
``` $oc login -u <your_username> https://openshift.caascloud.net:8443 $oc project <qa-username> $oc delete all --all ```