Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jenkins Multibranch Pipeline

Similar presentations


Presentation on theme: "Jenkins Multibranch Pipeline"— Presentation transcript:

1

2 Jenkins Multibranch Pipeline

3 Oleksandr Metelytsia

4 Content CI Git branching Shift left Docker A bit of code

5

6 Continuous integration
What is the difference? Continuous integration Developers practicing continuous integration merge their changes back to the main branch as often as possible. The developer's changes are validated by creating a build and running automated tests against the build. By doing so, you avoid the integration hell that usually happens when people wait for release day to merge their changes into the release branch. Continuous delivery Continuous delivery is an extension of continuous integration to make sure that you can release new changes to your customers quickly in a sustainable way. This means that on top of having automated your testing, you also have automated your release process and you can deploy your application at any point of time by clicking on a button. Continuous deployment Continuous deployment goes one step further than continuous delivery. With this practice, every change that passes all stages of your production pipeline is released to your customers. There's no human intervention, and only a failed test will prevent a new change to be deployed to production.

7 Nhan Ngo, a QA engineer at Spotify, made fabulous visualizations while reading Continuous Delivery. She has very kindly agreed to make them available under a Creative Commons license so feel free to share them, download them, and print them out. just for fun:

8 Why octopus? The core idea behind the Feature Branch Workflow is that all feature development should take place in a dedicated branch instead of the master branch. This encapsulation makes it easy for multiple developers to work on a particular feature without disturbing the main codebase. It also means the master branch will never contain broken code, which is a huge advantage for continuous integration environments.

9 Why? Defects are less costly when caught early

10

11 Shift left Shift left testing is an approach to software testing and system testing in which testing is performed earlier in the lifecycle (i.e., moved left on the project timeline). It is the first half of the maxim "Test early and often.” Shift Left approach just does not end with the Testers alone. Moving to the let and carrying out the testing activities continuously will also allow the Developers to take more ownership of their code and increase their responsibilities on testing. In a nutshell, Shift Left Testing process is: Finding the defects early thereby reducing the cost of the project. Testing continuously again and again to reduce defects in the end. To automate everything and improve time to market. To focus on customer requirements and improve the customer experience.

12 It’s a regular practice to combine your git flow with tests so you guarantee your code is fully working and avoid some undesired regressions. Context Switching destroys Developers Productivity The reality is that the whole team owns the acceptance tests, in the same way as the whole team owns every stage of the pipeline. If the acceptance tests fail, the whole team should stop and fix them immediately.

13 Jenkins Blue Ocean

14 What is Jenkins Pipeline
Jenkins Pipeline (or simply "Pipeline" with a capital "P") is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins.

15 https://jenkins.io/doc/book/pipeline/syntax/

16 Docker Lightweight Boots in seconds
Docker is an open source project that offers a software development solution known as containers. A container is ”a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it.” Docker can run across both Windows- and Linux-based platforms. Docker can also be run within a virtual machine if need be. Lightweight Boots in seconds Containers can be shared across multiple team members

17 Docker 101 docker build docker run docker login docker pull
docker images docker ps docker exec docker network Dockerfile Image vs container Docker registry

18

19

20 https://goo.gl/KdUhwg
Link to presentation

21 Do not forget to switch to Linux Containers
For Windows 10 Do not forget to switch to Linux Containers

22 Start from pulling $ docker pull jenkinsci/blueocean
The very first command, but it may take some time so do it right now: $ docker pull jenkinsci/blueocean * if have unauthorised error, run following command: $ docker login Create working dir and cd to it: $ mkdir workshop $ cd workshop

23 Start Jenkins container
$ docker run \ --rm \ -u root \ -p 8080:8080 \ -v jenkins-data:/var/jenkins_home \ -v /var/run/docker.sock:/var/run/docker.sock \ jenkinsci/blueocean * For Win10 use ^ instead of \ In case of Windows: docker run ^ --rm ^ -u root ^ -p 8080:8080 ^ -v jenkins-data:/var/jenkins_home ^ -v /var/run/docker.sock:/var/run/docker.sock ^ jenkinsci/blueocean From “docker run --help”: -v, --volume list Bind mount a volume

24 Setup Wizard Go to http://localhost:8080
Unlock Jenkins with password which is in terminal between two sets of asterisks Click “Install suggested plugins” Create First Admin User Save and Finish Start using Jenkins

25 Stop and restart Jenkins (if needed)
If something went “terribly wrong”: Stop Jenkins by Ctrl+C in terminal Run the same “docker run …​” command to restart Wait till login page Login back If you want to debug container: Add flag “--name jenkins-workshop” to “docker run …​” command In separate terminal window run: $ docker exec -it jenkins-workshop bash

26 Fork repo

27 Fork repo with tests

28 Clone forked repo $ git clone \ https://YOUR_BITBUCKET_USERNAME\
@bitbucket.org/YOUR_BITBUCKET_USERNAME\ /multibranch-pipeline-project.git

29 Create and push branches: dev and prod
$ cd multibranch-pipeline-project $ git branch dev $ git branch prod $ git branch dev * master prod $ git push origin -u dev:dev $ git push origin -u prod:prod

30 Create pipeline project in BlueOcean
1. Browse to 2. Login and click Open Blue Ocean on the left 3. Click on “Create a new Pipeline” 4. Where do you store your code: “Bitbucket Cloud” 5. Connect to bitbucket with bitbucket credentials and fork of the repo

31 Setup Scan Trigger Open pipeline settings by clicking on Gear button and setup: f

32 Let’s take a look on Jenkinsfile stub
$ cat Jenkinsfile pipeline { agent any stages { stage('Build') { steps { sh 'echo "Hello world!"' }

33 Jenkins UI

34 Finally branching... $ git checkout dev
Now we want to start CI development on a branch. $ git checkout dev Add new stage to Jenkinsfile which will build web docker container: $ cat Jenkinsfile pipeline { agent none stages { stage('Build web') { agent { dockerfile { additionalBuildArgs '-t web' } steps { sh 'echo web built'

35 View new stage on Jenkins UI
$ git commit -a -m "Add build web stage" $ git push origin dev

36 Next stage: build test env container
Add to Jenkins file stage to build container with selenium, firefox, etc. stage('Build Test Env') { agent { dockerfile { dir 'tests' additionalBuildArgs '-t test_env' } } steps { sh 'echo test env container was built' } }

37 Back to Jenkins UI $ git commit -a -m "Add build test env stage"
$ git push origin dev

38 “General” stage: e.g. Create network
We can run any kind of bash scripts or commands. Let’s run ‘create_network.sh’. $ cat create_network.sh #!/usr/bin/env sh if docker network ls | grep net; then echo "Network with net name was found. Skipping network creation." else docker network create net fi Add new stage to Jenkinsfile: stage('Create network') { agent any steps { sh 'sh ./create_network.sh' } }

39 Jenkins UI $ git commit -a -m "Add create network stage"
$ git push origin dev

40 Run Application locally
* for Win10 just run: $ docker network create net $ docker network ls | grep net > /dev/null || docker network create net $ docker run --rm --network net -p 6379: name redis -d redis $ docker run --rm --network net -p 80:80 --name web -d web $ curl :80 <h3>Hello World!</h3><b>Visits:</b><b id='visits'>1</b><br/> Go to in browser

41 Run tests locally $ docker ps CONTAINER ID IMAGE COMMAND NAMES 572c2c78327f web "python app.py" web c5fa2dbd7e redis "docker-entrypoint.s…" redis 3f8df588fc5e jenkinsci/blueocean "/sbin/tini -- /usr/…" blissful_nightingale $ docker run --network net test_env test_app (test.SeleniumTest) ... ok Ran 1 test in 2.734s OK $ docker kill web redis

42 Finally Test stage (yey)
stage('Test') { agent any steps { sh 'docker run --rm --network net -p 6379: name redis -d redis' sh 'echo Redis started!' sh 'docker run --rm --network net -p 80:80 --name web -d web' sh 'echo Web started!' sh 'docker run --network net test_env' } post { always { sh 'docker kill web 2>/dev/null || true' sh 'docker kill redis 2>/dev/null || true' } } }

43 Jenkins UI $ git commit -a -m "Add create Test stage"
$ git push origin dev

44 ‘bad feature’ development
$ git checkout -b bad_feature Edit app.py: change “Hello World!” to “Goodbye World!”. $ git commit -a -m "New cool feature done" $ git push origin bad_feature Create pull request from bad_feature branch to dev branch. Make sure you are doing PR against your Fork repo and not AlexMetelitsa repo.

45 New feature PR

46 See broken tests before PR merge

47 Add deploy to Dev and to Prod stages
$ git checkout dev Add deploy stages: stage('Deploy for Dev') { agent any when { branch 'dev' } steps { sh 'echo Deploying to dev...' sh 'echo Deployed to Dev!' } } stage('Deploy for Prod') { agent any when { branch 'prod' } steps { input message: 'Click proceed to continue' sh 'echo Deploying to Prod...' sh 'echo Deployed to Prod!' } }

48 View new stages on Jenkins UI
$ git commit -a -m "Add build web stage" $ git push

49 Create PR and merge to prod
Create PR dev to prod and merge it:

50 Conclusion. Finally :)

51 Useful links https://jenkins.io/doc/book/pipeline/syntax/

52 Questions?


Download ppt "Jenkins Multibranch Pipeline"

Similar presentations


Ads by Google