donal
2018-07-03 949ccbe50ac2f43648064439085042eb58fc3b95
commit | author | age
74d1f1 1 # The Manual Menace
5e7a31 2 > In this exercise learners will use Ansible to drive automated provisioning of Projects in OpenShift, Git, Jenkins and Nexus.
0f4d08 3
9eed0b 4 ![automation-xkcd](https://imgs.xkcd.com/comics/automation.png)
D 5
6 ## Exercise Intro
7 In this exercise we will use automation tooling to create Project namespaces for our `CI/CD` tooling along with the `dev` and `test` namespaces for our deployments to live. We do this to manually using the OpenShift CLI; but as we go from cluster to cluster or project to project Dev and Ops teams often find themselves having to redo these tasks again and again. Configuring our cluster using code; we can easily store this in Git and repeat the process again and again. By minimising the time taken to do these repetitive tasks we can accelerate our ability to deliver value to our customers; working on the hard problems they face.
8
9 This exercise uses Ansible to drive the creation of the cluster content. In particular; we'll use a play book called the `OpenShift Applier`. Once the project namespace have been created; we will add some tools to support CI/CD such as Jenkins, Git and Nexus. These tools will be needed by later lessons to automate the build and deploy of our apps. Again; we will use OpenShift Templates and drive their creation in the cluster using Ansible. To prove things are working, finally we'll delete all our content and re-apply the inventory to re-create our clusters content.
74d1f1 10
5e7a31 11 #### Why is config-as-code important?
78b569 12 * Assurance - Prevents unwanted config changes from people making arbitrary changes to environments. No more Snowflake servers!
5e7a31 13 * Traceability - Committing config as code means a user has approved and changes can be tracked.
78b569 14 * Phoenix Server -  Burn it all to the ground and bring it back; exactly the way it was!
D 15
74d1f1 16 _____
0f4d08 17
D 18 ## Learning Outcomes
19 As a learner you will be able to
74d1f1 20
D 21 1. Run the OpenShift Applier to automate creating cluster content
22 1. Create and admin project namespaces in OpenShift
23 1. Deploy commonly used applications to support the development process
0f4d08 24
D 25 ## Tools and Frameworks
26
74d1f1 27 * [GitLab](https://about.gitlab.com/) - Community driven Git server now with integrated DevOps Toolchain.
D 28 * [Nexus](https://www.sonatype.com/nexus-repository-sonatype) - Repository manager for storing lots of application types. Can also host `npm` and `Docker` registries.
29 * [Jenkins](https://jenkins.io/) - OpenSource Build automation server. Highly customisable with plugins.
30 * [Ansible](https://www.ansible.com/) - IT Automation tool used to provision and manage state of cloud and physical infrastructure.
b4d469 31 * [OpenShift Applier](https://github.com/redhat-cop/openshift-applier) - used to apply OpenShift objects to an OpenShift Cluster.
A 32
74d1f1 33 ## Big Picture
5e7a31 34 > The Big Picture is our emerging architecture; starting with an empty cluster we populate it with projects and some ci/cd tooling.
74d1f1 35
b62fab 36 ![ds-messing-around.gif](../images/exercise1/ds-messing-around.gif)
949ccb 37
D 38 ## The Practices
39 [event-storming](https://rht-labs.github.io/practice-library/practices/impact-mapping/ ':include :type=iframe :')
b62fab 40
74d1f1 41 _____
0f4d08 42
D 43 ## 10,000 Ft View
5e7a31 44 > This exercise is aimed at the creation of the tooling that will be used to support the rest of the Exercises. The high-level goal is to create a collection of project namespaces and populate them with Git, Jenkins & Nexus.
74d1f1 45
5e7a31 46 If you're feeling confident and don't want to follow the step-by-step guide these high-level instructions should provide a challenge for you:
74d1f1 47
949ccb 48 1. Clone the repo `https://github.com/rht-labs/enablement-ci-cd` which contains the scaffold of the project. Ensure you get all remote branches.
92099a 49
74d1f1 50 2. Create `<your-name>-ci-cd`, `<your-name>-dev` and `<your-name>-test` project namespaces using the inventory and run them with the OpenShift Applier to populate the cluster
e23af1 51
949ccb 52 3. Use the templates provided to create build of the jenkins-s2i. The templates are in `exercise1/jenkins-s2i`
92099a 53
949ccb 54 4. Use the templates provided to create build and deployment configs in `<your-name>-ci-cd` for. Templates are on a branch called `exercise1/git-nexus` && `exercise1/jenkins`:
74d1f1 55     * Nexus
D 56     * GitLab
5e7a31 57     * Jenkins (using an s2i to pre-configure Jenkins)
92099a 58
949ccb 59 5. Commit your `enablement-ci-cd` repository to the GitLab Instance you've created
92099a 60
949ccb 61 6. Burn it all down and re-apply your inventory proving config-as-code works.
0f4d08 62
D 63 ## Step by Step Instructions
bd7806 64 > This is a structured guide with references to exact filenames and explanations.
0f4d08 65
bd7806 66 ### Part 1 - Create OpenShift Projects
d28811 67 > _Using the OpenShift Applier, we will add new project namespaces to the cluster which will be used throughout the exercise._
D 68
45eb81 69 3. In this course three different git projects will be created. To setup your local machine for each of these, create a new folder on the terminal in the root of your HOME directory for convenience. To do this, open a new Terminal session and create the new folder using the following command (new terminal sessions will start in your HOME dir).
bd7806 70 ```bash
45eb81 71 mkdir -p ~/innovation-labs && cd ~/innovation-labs
2a3d5b 72 ```
784073 73 <p class="tip">
D 74 NOTE - If you do not want to have this folder at the root of your home dir that's fine, just ensure any parent directories of this `innovation-labs` folder do NOT have any spaces in them as it breaks Ansible in later labs...
75 </p>
76
77 3. Clone the scaffold project to your local machine's `innovation-labs` folder and pull all remote branches for use in later exercises. Note - this may error saying `fatal: A branch named 'develop' already exists.` but it can be ignored
78 ```bash
79 git clone https://github.com/rht-labs/enablement-ci-cd && cd enablement-ci-cd
2a3d5b 80 ```
784073 81 ```bash
D 82 ./git-pull-all.sh
0f4d08 83 ```
D 84
784073 85 3. Open the `innovation-labs` folder in VSCode (or your favourite editor). The project is laid out as follows
bd7806 86 ```
D 87 .
88 ├── README.md
fd78e6 89 ├── apply.yml
bd7806 90 ├── docker
D 91 ├── inventory
fd78e6 92 │   ├── host_vars
D 93 │   │   ├── ci-cd-tooling.yml
94 │   │   └── projects-and-policies.yml
95 │   └── hosts
bd7806 96 ├── jenkins-s2i
D 97 ├── params
fd78e6 98 │   └── project-requests-ci-cd
bd7806 99 ├── requirements.yml
D 100 └── templates
fd78e6 101     └── project-requests.yml
bd7806 102 ```
D 103  * `docker` folder contains our jenkins-slave images that will be used by the builds.
104  * `jenkins-s2i` contains the configuration and plugins we want to bring jenkins to life with
105  * `params` houses the variables we will load the templates with
106  * `templates` is a collection of OpenShift templates
fd78e6 107  * `inventory/host_vars/*.yml` is the collection of objects we want to insert into the cluster.
bd7806 108  * `requirements.yml` is a manifest which contains the ansible modules needed to run the playbook
fd78e6 109  * `apply.yml` is a playbook that sets up some variables and runs the OpenShift Applier role.
bd7806 110
5e7a31 111 3. Open the `apply.yml` file in the root of the project. Update the namespace variables by replacing the `<YOUR_NAME>` with your name or initials. Don't use uppercase or special characters. For example; my name is Dónal so I've created:
fd78e6 112 ```yaml
f49b0d 113   hosts: "{{ target }}"
fd78e6 114   vars:
D 115     ci_cd_namespace: donal-ci-cd
116     dev_namespace: donal-dev
117     test_namespace: donal-test
f49b0d 118   tasks:
fd78e6 119 ```
f49b0d 120 <p class="tip">
D 121 NOTE - yaml is indentation sensitive so keep things lined up properly!
122 </p>
fd78e6 123
D 124 3. Open the `inventory/host_vars/projects-and-policies.yml` file; you should see some variables setup already to create the `<YOUR_NAME>-ci-cd` namespace. This object is passed to the OpenShift Applier to call the `templates/project-requests.yml` template with the `params/project-requests-ci-cd` parameters. We will add some additional content here but first let's explore the parameters and the template
125
5e7a31 126 3. Open the `params/project-requests-ci-cd` and replace the `<YOUR_NAME>` with your name to create the corresponding projects in the cluster.
1c9328 127 ![new-item](../images/exercise1/ci-cd-project-namespace.png)
bd7806 128
5e7a31 129 3. Let's add two more params files to pass to our template to be able to create a `dev` and `test` project.
CM 130   * Create another two params files `params/project-requests-dev` & `params/project-requests-test`. On the terminal run
e23af1 131 ```bash
784073 132 touch params/project-requests-dev params/project-requests-test
e23af1 133 ```
784073 134   * In your editor; Open `params/project-requests-dev` and add the following by substituting `<YOUR_NAME>` accordingly
fd78e6 135 ```
D 136 NAMESPACE=<YOUR_NAME>-dev
137 NAMESPACE_DISPLAY_NAME=<YOUR-NAME> Dev
138 ```
784073 139   * In your editor; Open `params/project-requests-test` and add the following by substituting `<YOUR_NAME>` accordingly
fd78e6 140 ```
D 141 NAMESPACE=<YOUR_NAME>-test
142 NAMESPACE_DISPLAY_NAME=<YOUR-NAME> Test
143 ```
bd7806 144
fd78e6 145 3. In the `inventory/host_vars/projects-and-policies.yml` file; add the new objects for the projects you want to create (dev & test) by adding another object to the content array for each. You can copy and paste them from the `ci-cd` example and update them accordingly. If you do this; remember to change the params file! e.g.
bd7806 146 ```yaml
fd78e6 147     - name: "{{ dev_namespace }}"
D 148       template: "{{ playbook_dir }}/templates/project-requests.yml"
1c9328 149       template_action: create
fd78e6 150       params: "{{ playbook_dir }}/params/project-requests-dev"
1c9328 151       tags:
D 152       - projects
fd78e6 153     - name: "{{ test_namespace }}"
D 154       template: "{{ playbook_dir }}/templates/project-requests.yml"
1c9328 155       template_action: create
fd78e6 156       params: "{{ playbook_dir }}/params/project-requests-test"
1c9328 157       tags:
D 158       - projects
bd7806 159 ```
1c9328 160 ![project-request-yaml](../images/exercise1/project-request-yml.png)
bd7806 161
D 162 3. With the configuration in place; install the OpenShift Applier dependency
163 ```bash
784073 164 ansible-galaxy install -r requirements.yml --roles-path=roles
bd7806 165 ```
D 166
5e7a31 167 3. Apply the inventory by logging into OpenShift on the terminal and running the playbook as follows (<CLUSTER_URL> should be replaced with the one you've been sent as shown below). Accept any insecure connection warning 👍:
bd7806 168 ```bash
a9627c 169 oc login https://console.lader.rht-labs.com
784073 170 ```
D 171 ```bash
172 ansible-playbook apply.yml -i inventory/ -e target=bootstrap
fd78e6 173 ```
D 174 where the `-e target=bootstrap` is passing an additional variable specifying that we run the `bootstrap` inventory
bd7806 175
a9627c 176 3. Once successful you should see an output similar to this (Cow's not included): ![playbook-success](../images/exercise1/play-book-success.png)
bd7806 177
5e7a31 178 3. You can check to see the projects have been created successfully by running
f49b0d 179 ```bash
784073 180 oc projects
f49b0d 181 ```
D 182 ![project-success](../images/exercise1/project-success.png)
183
784073 184 ### Part 2 - Nexus
da55a5 185 > _Now that we have our Projects setup; we can start to populate them with Apps to be used in our dev lifecycle_
bd7806 186
92099a 187 4. In the `enablement-ci-cd` repo, checkout the templates for Nexus by running
da55a5 188 ```bash
784073 189 git checkout exercise1/git-nexus templates/nexus.yml
92099a 190 ```
11198f 191 The template contains all the things needed to setup a persistent nexus server, exposing a service and route while also creating the persistent volume needed. Have a read through the template; at the bottom you'll see a collection of parameters we will pass to the template.
92099a 192
5e7a31 193 4. Add some parameters for running the template by creating a new file in the `params` directory.
92099a 194 ```bash
784073 195 touch params/nexus
da55a5 196 ```
D 197
8fc7f6 198 4. The essential params to include in this file are:
92099a 199 ```bash
D 200 VOLUME_CAPACITY=5Gi
4dac3c 201 MEMORY_LIMIT=1Gi
92099a 202 ```
D 203
a9efee 204 4. Create a new object in the inventory variables `inventory/host_vars/ci-cd-tooling.yml` called `ci-cd-tooling` and populate its `content` is as follows
fd78e6 205
92099a 206 ```yaml
fd78e6 207 ---
D 208 ansible_connection: local
209 openshift_cluster_content:
210 - object: ci-cd-tooling
211   content:
4c20f0 212   - name: "nexus"
D 213     namespace: "{{ ci_cd_namespace }}"
214     template: "{{ playbook_dir }}/templates/nexus.yml"
215     params: "{{ playbook_dir }}/params/nexus"
216     tags:
217     - nexus
92099a 218 ```
1c9328 219 ![ci-cd-deployments-yml](../images/exercise1/ci-cd-deployments-yml.png)
92099a 220
a9efee 221 4. Run the OpenShift applier, specifying the tag `nexus` to speed up its execution (`-e target=tools` is to run the other inventory).
92099a 222 ```bash
784073 223 ansible-playbook apply.yml -e target=tools \
92099a 224      -i inventory/ \
1c9328 225      -e "filter_tags=nexus"
92099a 226 ```
D 227
3acbf0 228 4. Once successful; login to the cluster through the browser (using cluster URL) and navigate to the `<YOUR_NAME>-ci-cd`. You should see Nexus up and running. You can login with default credentials (admin / admin123) ![nexus-up-and-running](../images/exercise1/nexus-up-and-running.png)
92099a 229
784073 230 ### Part 3 - GitLab
D 231
5e7a31 232 #### 3a - GitLab install
fdaee0 233 <p class="tip">
784073 234 NOTE - This section may already have been completed for you, please check with your tutor. If this is the case, skip to section 3b "Commit CI/CD" below to add your code to GitLab.
fdaee0 235 </p>
D 236
11198f 237 4. Now let's do the same thing for GitLab to get it up and running. Checkout the template and params provided by running
92099a 238 ```bash
784073 239 git checkout exercise1/git-nexus templates/gitlab.yml params/gitlab
5e7a31 240 ```
92099a 241 Explore the template; it contains the PVC, buildConfig and services. The DeploymentConfig is made up of these apps
D 242  - Redis (3.2.3)
243  - PostgreSQL (9.4)
244  - GitLab CE (v10.2.3)
245
3acbf0 246 4. Open the `params/gitlab` file and complete the following params
RH 247 <p class="tip">
248 Note - The values here for the LDAP and BIND credentials will be provided by your tutor.
249 </p>
92099a 250 ```
D 251 LDAP_BIND_DN=uid=<BIND_USER>,ou=People,dc=<YOUR_DOMAIN>,dc=com
252 LDAP_USER_FILTER=(memberof=CN=YourGroup,OU=Users,DC=<YOUR_DOMAIN>,DC=com)
253 LDAP_PASSWORD=<BIND_USER_PASSWORD>
254 LDAP_HOST=<LDAP_HOST>
255 LDAP_BASE=ou=People,dc=<YOUR_DOMAIN>,dc=com
256 LDAP_LABEL="<LDAP_DESCRIPTION>"
257 GITLAB_ROOT_PASSWORD=<GITLAB_ROOT_USER_PASSWORD>
258 GITLAB_DATA_VOL_SIZE=2Gi
259 POSTGRESQL_VOL_SIZE=1Gi
260 APPLICATION_HOSTNAME=<GITLAB_URL>
4feb7f 261 NAMESPACE=<YOUR_NAME>-ci-cd
92099a 262 ```
D 263 where the following need to be replaced by actual values:
264     * `<BIND_USER>` is the user used to query the LDAP
265     * `<BIND_USER_PASSWORD>` is the password used when querying the LDAP
266     * `<YOUR_DOMAIN>` is the domain the LDAP is hosted on
267     * `<LDAP_HOST>` is fqdn of the LDAP server
5e7a31 268     * `<LDAP_DESCRIPTION>` is the description to be used on the sign-in header for GitLab e.g. "Name LDAP Login"
CM 269     * `<GITLAB_ROOT_USER_PASSWORD>` is the root user for GOD access on the GitLab instance e.g. password123
92099a 270     * `<GITLAB_URL>` is the endpoint for gitlab. It will take the form `gitlab-<YOUR_NAME>-ci-cd.apps.<ENV_ID>.<YOUR_DOMAIN>.com`
D 271
fd78e6 272 4. Create another object in the inventory `inventory/host_vars/ci-cd-tooling.yml` file to run the build & deploy of this template. Add the following and update the `namespace:` accordingly
92099a 273 ```yaml
1c9328 274     - name: "gitlab"
fd78e6 275       namespace: "{{ ci_cd_namespace }}"
D 276       template: "{{ playbook_dir }}/templates/gitlab.yml"
277       params: "{{ playbook_dir }}/params/gitlab"
1c9328 278       tags:
D 279       - gitlab
92099a 280 ```
D 281
a9efee 282 4. Run the OpenShift applier, specifying the tag `gitlab` to speed up its execution.
92099a 283 ```bash
784073 284 ansible-playbook apply.yml -e target=tools \
92099a 285      -i inventory/ \
1c9328 286      -e "filter_tags=gitlab"
92099a 287 ```
D 288
1c9328 289 4. Once successful; login to the cluster and navigate to the `<YOUR_NAME>-ci-cd`. You should see GitLab up and running. ![gitlab-up-and-running](../images/exercise1/gitlab-up-and-running.png)
784073 290
D 291 #### 3b - Commit CI/CD
4feb7f 292
5e7a31 293 4. Navigate to GitLab (if you have just skipped straight to this step; ask your tutor for the URL). You can login using your cluster credentials using the LDAP tab
1c9328 294 ![gitlab-ui](../images/exercise1/gitlab-ui.png)
4feb7f 295
4c20f0 296 4. Once logged in create a new project called `enablement-ci-cd` and mark it as internal. Once created; copy out the `git url` for use on the next step.
1c9328 297 ![gitlab-new-project](../images/exercise1/gitlab-new-project.png)
D 298 <p class="tip">
d28811 299 Note - we would not normally make the project under your name but create an group and add the project there on residency but for simplicity of the exercise we'll do that here
1c9328 300 </p>
D 301
784073 302 4. If you have not used Git before; you may need to tell Git who you are and what your email is before we commit. Run the following commands, substitution your email and "Your Name". If you've done this before move on to the next step.
1c9328 303 ```bash
784073 304 git config --global user.email "yourname@mail.com"
1c9328 305 ```
784073 306 ```bash
D 307 git config --global user.name "Your Name"
308 ```
309
5e7a31 310 4. Commit your local project to this new remote by first removing the existing origin (github) where the Ansible project was cloned from in the first steps. Remember to substitute `<GIT_URL>` accordingly with the one created for your `enablement-ci-cd` repository a moment ago.
784073 311 ```bash
D 312 git remote set-url origin <GIT_URL>
313 ```
314 ```bash
315 git add .
316 ```
317 ```bash
318 git commit -m "Adding git and nexus config"
319 ```
320 ```bash
321 git push -u origin --all
322 ```
323
e41c20 324 ### Part 4 MongoDB for CI tests
a9627c 325 > In order to run our API tests in CI in later labs; we need there to be a MongoDB available for executing our tests. As this is part of our CI/CD Lifecycle; we will add it now.
D 326
327 4. In our `enablement-ci-cd` repo; checkout the mongo templates as shown below to bring in the template and params. The mongodb template we're using is the same as the one for our `todolist-fe` created in previous exercise.
328 ```bash
55ba17 329 git checkout exercise1/mongodb params/mongodb templates/mongodb.yml
a9627c 330 ```
D 331
5e7a31 332 4. Open `enablement-ci-cd` in your favourite editor. Edit the `inventory/host_vars/ci-cd-tooling.yml` to include a new object for our mongodb as shown below. This item can be added below the Jenkins slave in the `ci-cd-tooling` section.
a9627c 333 ```yaml
D 334   - name: "jenkins-mongodb"
335     namespace: "{{ ci_cd_namespace }}"
336     template: "{{ playbook_dir }}/templates/mongodb.yml"
337     params: "{{ playbook_dir }}/params/mongodb"
338     tags:
339     - mongodb
340 ```
55ba17 341 ![jenkins-mongo](../images/exercise1/jenkins-mongo.png)
a9627c 342
D 343 4. Git commit your updates to the inventory to git for traceability.
344 ```bash
345 git add .
346 ```
347 ```bash
348 git commit -m "ADD - mongodb for use in the pipeline"
349 ```
350 ```bash
351 git push
352 ```
353
5e7a31 354 4. Apply this change as done previously using Ansible. The deployment can be validated by going to your `<YOUR_NAME>-ci-cd` namespace and checking if it is there!
a9627c 355 ```bash
D 356 ansible-playbook apply.yml -e target=tools \
357   -i inventory/ \
358   -e "filter_tags=mongodb"
359 ```
360 ![ocp-mongo](../images/exercise3/ocp-mongo.png)
361
5cf97a 362 **Note - When making changes to enablement-ci-cd you should frequently commit the changes to git.**
bd7806 363
e41c20 364 ### Part 5 - Jenkins & s2i
d28811 365 > _Create a build and deployment config for Jenkins. Add new configuration and plugins to the OCP Stock Jenkins using s2i_
bd7806 366
1c9328 367 5. Add the Jenkins Build & Deployment configs to the `enablement-ci-cd` repo by merging the contents `exercise1/jenkins` in
D 368 ```bash
784073 369 git checkout exercise1/jenkins templates/jenkins.yml
1c9328 370 ```
5e7a31 371 The Jenkins template is essentially the standard persistent Jenkins one with OpenShift.
1c9328 372
a7a123 373 5. As before; create a new set of params by creating a `params/jenkins` file and adding some overrides to the template and updating the `<YOUR_NAME>` value accordingly.
D 374 ```
4dac3c 375 MEMORY_LIMIT=3Gi
1adb94 376 VOLUME_CAPACITY=10Gi
1c9328 377 JVM_ARCH=x86_64
D 378 NAMESPACE=<YOUR_NAME>-ci-cd
379 JENKINS_OPTS=--sessionTimeout=720
380 ```
fd78e6 381
5e7a31 382 5. Add a `jenkins` variable to the Ansible inventory underneath the jenkins-mongo (and git if you have it) in  `inventory/host_vars/ci-cd-tooling.yml`.
1c9328 383 ```yaml
D 384     - name: "jenkins"
fd78e6 385       namespace: "{{ ci_cd_namespace }}"
D 386       template: "{{ playbook_dir }}/templates/jenkins.yml"
387       params: "{{ playbook_dir }}/params/jenkins"
1c9328 388       tags:
D 389       - jenkins
390 ```
11198f 391 This configuration, if applied now, will create the deployment configuration needed for Jenkins but the `${NAMESPACE}:${JENKINS_IMAGE_STREAM_TAG}` in the template won't exist yet.
1c9328 392
5e7a31 393 5. To create this image we will take the supported OpenShift Jenkins Image and bake into it some extra configuration using an [s2i](https://github.com/openshift/source-to-image) builder image. More information on Jenkins s2i is found on the [openshift/jenkins](https://github.com/openshift/jenkins#installing-using-s2i-build) GitHub page. To create an s2i configuration for Jenkins, check out the pre-canned configuration source in the `enablement-ci-cd` repo
1c9328 394 ```bash
784073 395 git checkout exercise1/jenkins-s2i jenkins-s2i
1c9328 396 ```
5e7a31 397 The structure of the Jenkins s2i config is
1c9328 398 ```
D 399 jenkins-s2i
400 ├── README.md
401 ├── configuration
402 │   ├── build-failure-analyzer.xml
403 │   ├── init.groovy
404 │   ├── jenkins.plugins.slack.SlackNotifier.xml
a7a123 405 │   ├── scriptApproval.xml
1c9328 406 │   └── jobs
D 407 │       └── seed-multibranch-job
408 │           └── config.xml
409 └── plugins.txt
410 ```
411  * `plugins.txt` is a list of `pluginId:version` for Jenkins to pre-install when starting
412  * `./configuration` contains content that is placed in `${JENKINS_HOME}`. A `config.xml` could be placed in here to control the bulk of Jenkins configuration.
413  * `./configuration/jobs/*` contains job names and xml config that jenkins loads when starting. The seed job in there we will return to in later lessons.
d28811 414  * `build-failure-analyzer.xml` is config for the plugin to read the logs and look for key items based on a Regex. More on this in later lessons.
1c9328 415  * `init.groovy` contains a collection of settings jenkins configures itself with when launching
D 416
5e7a31 417 5. Let's add a plugin for Jenkins to be started with, [green-balls](https://plugins.jenkins.io/greenballs). This simply changes the default `SUCCESS` status of Jenkins from Blue to Green. Append the `jenkins-s2i/plugins.txt` file with
1c9328 418 ```txt
D 419 greenballs:1.15
5e7a31 420 ```
1c9328 421 ![green-balls.png](../images/exercise1/green-balls.png)
D 422 Why does Jenkins have Blue Balls? More can be found [on reddit](https://www.reddit.com/r/programming/comments/4lu6q8/why_does_jenkins_have_blue_balls/) or the [jenkins blog](https://jenkins.io/blog/2012/03/13/why-does-jenkins-have-blue-balls/)
423
5e7a31 424 5. Before building and deploying the Jenkins s2i; add git credentials to it. These will be used by Jenkins to access the Git Repositories where our apps will be stored. We want Jenkins to be able to push tags to it so write access is required. There are a few ways we can do this; either adding them to the `template/jenkins.yml` as environment Variables and then including them in the `params/jenkins` file.  We could also create a token in GitLab and use it as the source secret in the Jenkins template.
CM 425 But for simplicity just replace the `<USERNAME>` && `<PASSWORD>` in the `jenkins-s2i/configuration/init.groovy` with your LDAP credentials as seen below. This init file gets run when Jenkins launches and will setup the credentials for use in our Jobs in the next exercises
1c9328 426 ```groovy
D 427 gitUsername = System.getenv("GIT_USERNAME") ?: "<USERNAME>"
428 gitPassword = System.getenv("GIT_PASSWORD") ?: "<PASSWORD>"
429 ```
a7a123 430 <p class="tip">
5e7a31 431 Note in a residency we would not use your GitCredentials for pushing and pulling from Git, a service user would be created for this.
a7a123 432 </p>
1c9328 433
afef19 434 5. Checkout the params and the templates for the `jenkins-s2i`
a7a123 435 ```bash
784073 436 git checkout exercise1/jenkins-s2i params/jenkins-s2i templates/jenkins-s2i.yml
a7a123 437 ```
D 438
5e7a31 439 5. Open `params/jenkins-s2i` and add the following content; replacing variables as appropriate.
1c9328 440 ```
e23af1 441 SOURCE_REPOSITORY_URL=<GIT_URL>
1c9328 442 NAME=jenkins
D 443 SOURCE_REPOSITORY_CONTEXT_DIR=jenkins-s2i
444 IMAGE_STREAM_NAMESPACE=<YOUR_NAME>-ci-cd
e23af1 445 SOURCE_REPOSITORY_USERNAME=<YOUR_LDAP_USERNAME>
D 446 SOURCE_REPOSITORY_PASSWORD=<YOUR_LDAP_PASSWORD>
1c9328 447 ```
5e7a31 448 where
e23af1 449     * `<GIT_URL>` is the full path clone path of the repo where this project is stored (including the https && .git)
d28811 450     * `<YOUR_NAME>` is the prefix for your `-ci-cd` project.
D 451     * Explore some of the other parameters in `templates/jenkins-s2i.yml`
e23af1 452     * `<YOUR_LDAP_USERNAME>` is the username builder pod will use to login and clone the repo with
D 453     * `<YOUR_LDAP_PASSWORD>` is the password the builder pod will use to authenticate and clone the repo using
d28811 454 <p class="tip">
e23af1 455 Note in a residency we would not use your GitCredentials for pushing and pulling from Git, A service user would be created or a token generated.
d28811 456 </p>
1c9328 457
5e7a31 458 5. Create a new object `ci-cd-builds` in the Ansible `inventory/host_vars/ci-cd-tooling.yml` to drive the s2i build configuration.
1c9328 459 ```yaml
D 460   - object: ci-cd-builds
461     content:
462     - name: "jenkins-s2i"
fd78e6 463       namespace: "{{ ci_cd_namespace }}"
D 464       template: "{{ playbook_dir }}/templates/jenkins-s2i.yml"
465       params: "{{ playbook_dir }}/params/jenkins-s2i"
1c9328 466       tags:
D 467       - jenkins
468 ```
469
470 5. Commit your code to your GitLab instance
471 ```bash
784073 472 git add .
D 473 ```
474 ```bash
475 git commit -m "Adding Jenkins and Jenkins s2i"
476 ```
477 ```bash
478 git push
1c9328 479 ```
D 480
5e7a31 481 5.  In order for Jenkins to be able to run `npm` builds and installs we must configure a `jenkins-build-slave` for Jenkins to use. This slave will be dynamically provisioned when we run a build. It needs to have NodeJS and npm installed in it. These slaves can take a time to build themselves so to speed up we have placed the slave within OpenShift and you can use the following commands to be able to use them in your project.
350001 482 ```bash
D 483 oc project <YOUR_NAME>-ci-cd
484 ```
3437ff 485 ```bash
T 486 oc tag openshift/jenkins-slave-npm:latest jenkins-slave-npm:latest
487 ```
488 ```bash
489 oc label is jenkins-slave-npm role=jenkins-slave
490 ```
350001 491 This is pulling the container image into your namespace and then adding a label which will allow Jenkins to take notice of it. Don't worry if the label is already there and this last command fails!
3437ff 492
T 493 5. Now your code is commited, and you have bought in the Jenkins slave; run the OpenShift Applier to add the config to the cluster
1c9328 494 ```bash
784073 495 ansible-playbook apply.yml -e target=tools \
1c9328 496      -i inventory/ \
D 497      -e "filter_tags=jenkins"
498 ```
499
d28811 500 5. This will trigger a build of the s2i and when it's complete it will add an imagestream of `<YOUR_NAME>-ci-cd/jenkins:latest` to the project. The Deployment config should kick in and deploy the image once it arrives. You can follow the build of the s2i by going to the OpenShift console's project
D 501 ![jenkins-s2i-log](../images/exercise1/jenkins-s2i-log.png)
bd7806 502
5e7a31 503 5. When the Jenkins deployment has completed; login (using your OpenShift credentials) and accept the role permissions. You should now see a fairly empty Jenkins with just the seed job
92099a 504
5e7a31 505 ### Part 6 - Jenkins Hello World
d28811 506 > _To test things are working end-to-end; create a hello world job that doesn't do much but proves we can pull code from git and that our balls are green._
D 507
508 6. Log in to Jenkins and hit `New Item` ![new-item](../images/exercise1/new-item.png).
509
176e08 510 6. Create a freestyle job called `hello-world` ![jenkins-new-hello-world](../images/exercise1/jenkins-new-hello-world.png).
d28811 511
11198f 512 6. On the Source Code Management tab; add your `enablement-ci-cd` git repo and hit the dropdown to add your credentials we baked into the s2i on previous steps ![jenkins-scm-git](../images/exercise1/jenkins-scm-git.png)
d28811 513
D 514 6. On the build tab add an Execute Shell step and fill it with `echo "Hello World"` ![jenkins-hello-world](../images/exercise1/jenkins-hello-world.png).
515
5e7a31 516 6. Run the build and we should see if pass successfully and with Green Balls! ![jenkins-green-balls](../images/exercise1/jenkins-green-balls.png)
d28811 517
e41c20 518 ### Part 7 - Live, Die, Repeat
5e7a31 519 > _In this section you will prove the infra as code is working by deleting your Cluster Content and recreating it all_
d28811 520
D 521 7. Commit your code to the new repo in GitLab
9eed0b 522 ```bash
784073 523 git add .
D 524 ```
525 ```bash
526 git commit -m "ADD - all ci/cd contents"
527 ```
528 ```bash
529 git push
9eed0b 530 ```
d28811 531
D 532 7. Burn your OCP content to the ground
9eed0b 533 ```bash
784073 534 oc delete project <YOUR_NAME>-ci-cd
D 535 ```
536 ```bash
537 oc delete project <YOUR_NAME>-dev
538 ```
539 ```bash
540 oc delete project <YOUR_NAME>-test
9eed0b 541 ```
d28811 542
e23af1 543 7. Check to see the projects that were marked for deletion are removed.
D 544 ```bash
784073 545 oc get projects | egrep '<YOUR_NAME>-ci-cd|<YOUR_NAME>-dev|<YOUR_NAME>-test'
e23af1 546 ```
D 547
9eed0b 548 7. Re-apply the inventory to re-create it all!
D 549 ```bash
a9627c 550 oc login https://console.lader.rht-labs.com
784073 551 ```
D 552 ```bash
553 ansible-playbook apply.yml -i inventory/ -e target=bootstrap
554 ```
555 ```bash
556 ansible-playbook apply.yml -i inventory/ -e target=tools
9eed0b 557 ```
0f4d08 558
74d1f1 559 _____
D 560
0f4d08 561 ## Extension Tasks
d28811 562 > _Ideas for go-getters. Advanced topic for doers to get on with if they finish early. These will usually not have a solution and are provided for additional scope._
0f4d08 563
a9627c 564  - Install Cowsay for 100% more Ansible Fun!
92099a 565  - Add more secure access for Nexus (ie not admin / admin123) using the automation to drive secret creation
1c9328 566  - Add a SonarQube persistent deployment to the `ci-cd-deployments` section.
D 567  - Add `jenkins.plugins.slack.SlackNotifier.xml` to `jenkins-s2i/configuration` to include URL of Slack for team build notifications and rebuild Jenkins S2I
0f4d08 568
74d1f1 569 _____
D 570
0f4d08 571 ## Additional Reading
9eed0b 572 > List of links or other reading that might be of use / reference for the exercise
4f0295 573
RH 574 ## Slide links
575
576 - [Intro](https://docs.google.com/presentation/d/1LsfAkH8GfIhulEoy_yd-usWBfDHnZEyQdNvYeTmAg4A/)
577 - [Wrap-up](https://docs.google.com/presentation/d/1cfyJ6SHddZNbM61oz67r870rLYVKY335zGclXN2uLMY/)
3207b9 578 - [All Material](https://drive.google.com/drive/folders/13Bt4BXf9P2OB8VI4YQNcNONF1786dqOx)