acammies
2018-04-18 6769e357eb35a6fb586bba2d76cadf95d0fa0522
commit | author | age
e36a5b 1 # An Enslaved Hope
43f2f2 2
dd12d4 3 > In this exercise we'll break free from the chains of point'n'click Jenkins by introducing pipeline as code in the form of `Jenkinsfile`. Following this we will introduce some new Jenkins slaves that will be used in later labs. 
e36a5b 4
D 5 There are a number of ways pipeline as code can be achieved in Jenkins.
6  * The Job DSL Plugin - this is a slightly older but very functional DSL mechanism to create reusable pipelines. Create a `groovy` file to run Jenkins Domain Specific Language to create jobs, functions and other items. In Jenkins; you then can execute this file which will build all of the config.xml files needed for each Job. 
dd12d4 7  * The Scripted Pipeline - The scripted pipeline introduced the Jenkinsfile and the ability for developers to write their jenkins setup as groovy code. A repo with a Jenkinsfile in it's root can be pointed to by Jenkins and it will automatically build out each of the stages described within. The scripted pipeline is ultimately Groovy at it's core.
A 8  * The Declarative Pipeline - This approach looks to simplify and opinionate what you can do and when you can do it in a pipeline. It does this by giving you top level `block` which define sections, directives and steps. The declarative syntax is not run as groovy but you can execute groovy inside script blocks. The advantage of it over scripted is validation of the config and lighter approach with requirement to understand all of the `groovy` syntax
e36a5b 9
43f2f2 10 _____
D 11
12 ## Learning Outcomes
13 As a learner you will be able to
e36a5b 14 - Use a Jenkinsfile to create a declarative pipeline to build, bake and deploy the Todolist App 
D 15 - Identify the differences between scripted, declarative and DSL pipelines 
16 - Create Jenkins slave nodes for use in builds in future labs
43f2f2 17
D 18 ## Tools and Frameworks
19 > Name of tool - short description and link to docs or website
20
e36a5b 21 1. [Pipeline](https://jenkins.io/doc/book/pipeline/) - Overview of the Jenkinsfile approach
D 22 1. [Pipeline Syntax](https://jenkins.io/doc/book/pipeline/syntax/) - Documentation for the declarative pipeline
23 1. [Groovy](http://groovy-lang.org/) - Groovy is a powerful, optionally typed and dynamic language, with static-typing and static compilation capabilities, for the Java platform aimed at improving developer productivity thanks to a concise, familiar and easy to learn syntax. It integrates smoothly with any Java program, and immediately delivers to your application powerful features, including scripting capabilities, Domain-Specific Language authoring, runtime and compile-time meta-programming and functional programming.
43f2f2 24
D 25 ## Big Picture
26 This exercise begins cluster containing blah blah
27
28 _____
29
30 ## 10,000 Ft View
e36a5b 31 > The goal of this exercise is to move to using the Jenkinsfile in the todolist-api and todolist-fe projects. Additionally we will create new slaves for use in the next lab
43f2f2 32
e36a5b 33 2. On Jenkins; create a multibranch pipeline project to scan the GitLab endpoint for each app. Use the Jenkinsfile provided to run the stages. Replace the `<YOUR_NAME>` with appropriate variable.
43f2f2 34
e36a5b 35 2. Create two new Jenkins slaves for the `OWASP ZAP` scanner and the `Arachni` WebCrawler
43f2f2 36
D 37 ## Step by Step Instructions
e36a5b 38 > This is a fairly structured guide with references to exact filenames and sections of text to be added. 
43f2f2 39
e36a5b 40 ### Part 1 - The Jenkinsfile
D 41 > _In this exercise we'll replace the Pipeline we created in Lab 2 with a Jenkinsfile approach_
43f2f2 42
e36a5b 43 2. On your terminal navigate to your `todolist-api` project and checkout the pipeline feature branch that's been already created for you.
D 44 ```bash
45 $ git checkout feature/jenkinsfile
43f2f2 46 ```
D 47
e36a5b 48 2. Open up your `todolist-api` application in your favourite editor and move to the `Jenkinsfile` in the root of the project. The highlevel structure of the file is shown collapsed below. 
D 49 ![pipeline-overview](../images/exercise4/pipeline-overview.png)
50 Some of the key things to note:
51     * `pipeline {}` is how all declarative jenkins pipelines begin.
52     * `environment {}` defines environment varibales to be used across all build stages
53     * `options {}` contains specific Job specs you want to run globally across the jobs eg setting the terminal colour
54     * `stage {}` all jobs must have one stage. This is the logical part of the build that will be executed eg `bake-image`
55     * `steps {}` each `stage` has one or more steps involved. These could be execute shell or git checkout etc.
56     * `agent {}` specifies the node the build should be run on eg `jenkins-slave-npm`
57     * `post {}` hook is used to specify the post-build-actions. Jenkins declarative provides very useful callbacks for `success`, `failure` and `always` which are useful for controlling the job flow
58     * `when {}` is used for flow control. It can be used at stage level and be used to stop pipeline entering that stage. eg when branch is master; deploy to `test` environment.
59
60 2. The Jenkinsfile is mostly complete to do all the testing etc that was done in previous labs. Some minor changes will be needed to orchestrate namespaces. Find and replace all instances of `<YOUR_NAME>` in the Jenkinsfile and update the `GITLAB_DOMAIN` accordingly. 
61 ```groovy
62     environment {
63         // GLobal Vars
dd12d4 64         PIPELINES_NAMESPACE = "<YOUR_NAME>-ci-cd"
e36a5b 65         APP_NAME = "todolist-api"
D 66
67         JENKINS_TAG = "${JOB_NAME}.${BUILD_NUMBER}".replace("/", "-")
68         JOB_NAME = "${JOB_NAME}".replace("/", "-")
69
70         GIT_SSL_NO_VERIFY = true
71         GIT_CREDENTIALS = credentials('jenkins-git-creds')
dd12d4 72         GITLAB_DOMAIN = "gitlab-<YOUR_NAME>-ci-cd.apps.somedomain.com"
A 73         GITLAB_PROJECT = "<YOUR_NAME>"
e36a5b 74     }
D 75 ```
76
77 2. With these changes in place, push your changes to the `feature/jenkinsfile` branch.
78 ```bash
79 $ git add Jenkinsfile
80 $ git commit -m "ADD - namespace and git repo to pipeline"
81 $ git push
82 ```
83
84 2. When the changes have been successfully pushed; Open Jenkins.
85
86 2. Create a `New Item` on Jenkins. Give it the name `todolist-api` and select `Multibranch Pipeline` from the bottom of the list as the job type.
87 ![multibranch-select](../images/exercise4/multibranch-select.png)
88
89 2. On the job's configure page; set the Branch Sources to `git`
90 ![multibranch-select-git](../images/exercise4/multibranch-select-git.png)
91
92 2. Fill in the Git settings with your `todolist-api` project url and setting the credentials
93 ![multibranch-git](../images/exercise4/multibranch-git.png)
94
95 2. Set the `Scan Multibranch Pipeline Triggers` to be periodical and the internal to 1 minute. This will poll the gitlab instance for new branches or change sets to build.
96 ![multibranch-scan-time](../images/exercise4/multibranch-scan-time.png)
97
98 2. Save the Job configuration to run the intial scan. The log will show scans for `master` and `develop` branch which have no `Jenkinsfile` so are skipped. The resulting view will show the `feature/jenkisifle` job corresponding the only branch that currently has one. The build should run automatically. 
99 ![todolist-api-multi](../images/exercise4/todolist-api-multi.png)
100
dd12d4 101 2. The pipeline file is setup to only run `bake` & `deploy` stages when on `master` or `develop` branch. This is to provide us with very fast feedback for team members working on feature or bug fix branches. Each time someone commits or creates a new branch a basic build with testing occurs to give very rapid feedback to the team. Let's now update our branches to include the Jenkinsfile and delete the feature branch.
e36a5b 102 ```bash
D 103 $ git checkout develop
104 $ git merge feature/jenkinsfile
105 $ git checkout master
106 $ git merge develop
107 $ git push -u origin --all
108 # this is to delete the branch from the remote
109 $ git push origin :feature/jenkinsfile
110 ```
111
112 2. Back on Jenkins we should see our `todolist-api` pipelines have changed with the `develop` and `master` now appearing. The feature was deleted so this job should have gone away.
113 ![todolist-api-multi-dev-test](../images/exercise4/todolist-api-multi-dev-test.png)
114
115 2. With the builds running for  `develop` and `master` we can explore the Blue Ocean View for Jenkins. On the Job overview page, hit the Open Blue Ocean ![open-blue-ocean](../images/exercise4/open-blue-ocean.png)
116  button on the side to see what modern Jenkins looks like.
117 ![blue-ocean-todolist-api](../images/exercise4/blue-ocean-todolist-api.png)
118
119 2.  We can move on to the `todolist-fe` job. The process is the same as before, checkout the feature branch
120 ```bash
121 $ cd todolist-fe
122 $ git checkout feature/jenkinsfile
123 ```
124
125 2. Open up your `todolist-fe` application in your favourite editor and move to the `Jenkinsfile` in the root of the project. Update all `<YOUR_NAME>` and `GITLAB_DOMAIN` references accordingly. 
126
127 2. Commit your changes to your feature branch as you did previously. 
128 ```bash
129 $ git add Jenkinsfile
130 $ git commit -m "ADD - namespace and git repo to pipeline"
131 $ git push
132 ```
133
134 2. This time update your master and develop branches before creating config in Jenkins
135 ```
136 git checkout develop
137 git merge feature/jenkinsfile
138 git checkout master
139 git merge develop
140 git push -u origin --all
141 ```
142
143 2. On Jenkins; create a new `Multibranch Pipeline` job called `todolist-fe`.
144
145 2. Add the `todolist-fe` git repository and set the credentials for git accordingly. 
146
147 2. Set the trigger to scan every minute as done previously. Save the configuration and we should see the collection of Jobs as shown below.
148 ![todolist-fe-multi](../images/exercise4/todolist-fe-multi.png)
149
6769e3 150 2. Run the jobs and validate the app is working as expected in the `test` environment!
e36a5b 151
D 152 ### Part 2 - Security Scanning Slaves
153 > _This exercise focuses on updating the `enablement-ci-cd` repo with some new jenkins-slave pods for use in future exercise_
43f2f2 154
6769e3 155 #### Part 2a - OWASP ZAP
A 156 > _OWASP ZAP (Zed Attack Proxy) is a free open source security tool used for finding security vulnerabilities in web applications._
157
158
159 3. _Remove jenkins bit if this is already in somewhere, also check syntax, actually only do one git checkout_ First we're going to take the generic jenkins slave template from our exercise4/zap branch and the params.
160 ```bash
161 $ git checkout exercise4/zap-and-arachni templates/jenkins-slave-generic-template.yml params/
162 ```
163
164 3. This should have created the following files:
165 - `templates/jenkins-slave-generic-template.yml`
166 - `params/ zap-bulid-pod arachni-build-pod`
167
168 3. Create an object in `insert donal's new layout here` called `zap-build-pod` and the following content:
169 ```yml
170     - name: "zap-build-pod"
171     namespace: "<YOUR_NAME>-ci-cd"
172     template: "{{ inventory_dir }}/../templates/jenkins-slave-generic-template.yml"
173     params: "{{ inventory_dir }}/../params/zap-build-pod"
174     tags:
175     - zap
176 ```
177
178 3. Install ansible-y stuff (only if not run before???)
179 ```bash
180 ansible-galaxy install -r requirements.yml --roles-path=roles
181 ```
182
183 3. Remember to login to the cluster!
184 ```bash
185 oc login https://console.s8.core.rht-labs.com --token=<INSERT_LOGIN_TOKEN_HERE>
186 ```
187
188 3. Run the ansible playbook filtering with tag `zap` so only the zap build pods are run.
189 ```bash
190 ansible-playbook roles/openshift-applier/playbooks/openshift-cluster-seed.yml \  -i inventory/ \  -e "filter_tags=zap"
191 ```
192
193 3. Head to (https://console.s8.core.rht-labs.com/console/project/<YOUR_NAME>-ci-cd/browse/builds) on Openshift and you should see `zap-build-pod`.
194 include screenshot here.
195
196 #### Part 2b - Arachni Scan
197 > _Arachni is a feature-full, modular, high-performance Ruby framework aimed towards helping penetration testers and administrators evaluate the security of web applications._
198
199 3. Create an object in `insert donal's new layout here` called `arachni-build-pod` and the following content:
200 ```yml
201     - name: "arachni-build-pod"
202     namespace: "<YOUR_NAME>-ci-cd"
203     template: "{{ inventory_dir }}/../templates/jenkins-slave-generic-template.yml"
204     params: "{{ inventory_dir }}/../params/arachni-build-pod"
205     tags:
206     - arachni
207 ```
208
209 3. Run the ansible playbook filtering with tag `arachni` so only the zap build pods are run.
210 ```bash
211 ansible-playbook roles/openshift-applier/playbooks/openshift-cluster-seed.yml \  -i inventory/ \  -e "filter_tags=arachni"
212 ```
213
214 3. Head to (https://console.s8.core.rht-labs.com/console/project/<YOUR_NAME>-ci-cd/browse/builds) on Openshift and you should see `zap-build-pod`.
215 include screenshot here.
43f2f2 216
D 217 _____
218
219 ## Extension Tasks
220 > _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._
221
e36a5b 222 OpenShift Sync plugin
D 223  - Use the `Jenkinsfile` provided to create a pipeline that runs in OpenShift's pipeline ie using it as the BuildConfig.
224
225 Jenkins S2I
226  - Add the multi-branch configuration to the S2I to have Jenkins come alive with the `todolist-api` and `-fe` configuration cooked into it for future uses.
227
d608d6 228 Jenkins Pipeline Extension
D 229  - Add an extension to the pipeline that promotes code to UAT environment once the master job has been successful. 
230  - Use a WAIT to allow for manual input to appove the promotion
231
232 Jenkins e2e extension (blue/green)
233  - Add a step in the pipeline to only deploy to the `test` environment if the e2e tests have run successfully against which ever environemnt (blue or green) is not deployed.
43f2f2 234
D 235 ## Additional Reading
236 > List of links or other reading that might be of use / reference for the exercise
237
238 ## Slide links
dd12d4 239 > link back to the deck for the supporting material