Code Monkey home page Code Monkey logo

training-ci's Introduction

Jenkins Workshop

Setup Master Environment

  1. Copy flask-app content to your repository

  2. Run Jenkins

cd jenkins;
docker-compose up -d
  1. Open Jenkins URL
http://127.0.0.1:8080
  • Unlock Jenkins
docker ps
docker exec -it jenkins_jenkins_1 cat /var/jenkins_home/secrets/initialAdminPassword
  • Install Suggested Plugins, Create First Admin User and Configure Jenkins URL
  1. Install Additional Plugin - BlueOcean https://jenkins.io/doc/book/blueocean/
  • Manage Jenkins --> Manage Plugins --> Available --> BlueOcean
  1. Add Jenkins Credentials to access source repository
  • Generate Jenkins SSH keys

    ssh-keygen -t rsa -C "[email protected]"

  • Jenkins --> Credentials --> System --> Global Credentials --> Add Credentials --> SSH Username with private key

  • Open your GitHub repository settings --> Deploy keys --> Add new

  1. Create Simple Freestyle Job
  • New Item --> FreeStyle Project
  • Source Code Management Tab --> Set Git url and Credentials
  • Build Tab --> Execute Shell --> echo "My First Jenkins Job"
  • Open Job url and Build Job
  1. Add Build Step and Run Flask Application
cd flask-app;
docker-compose up -d --build
  1. Create BlueOcean Pipeline, which run Flask App

https://jenkins.io/projects/blueocean/ https://jenkins.io/doc/book/blueocean/getting-started/

  • Jenkins --> BlueOcean --> New Pipeline
  • Provide GitHub Access Token
  • Add parallel stages
  • Add Git Checkout stage
  • Run Application

Setup Jenkins Slave

  1. Register Google Cloud Free Tier Account https://cloud.google.com/free/

  2. Create VM Instance for Jenkins Slave https://console.cloud.google.com

  • Navigation Menu --> Compute Engine --> VM Instances --> Create Instance
  • Please use any OS type you preferred. For me it works on Ubuntu 18.04, machine type - n1-standard-1 (1 vCPU, 3.75 GB memory), Disk Size - 20 GB
  1. SSH connect to created instance
gcloud beta compute --project "alpine-scholar-232716" ssh --zone "europe-west1-b" "jenkins-slave-1"
  1. Create Jenkins user (later Jenkins jobs will be running from this user)
sudo useradd jenkins -U -m -s /bin/bash
  1. Add Sudo Privileges to Jenkins user
visudo 
jenkins        ALL=(ALL)       NOPASSWD: ALL
  1. Generate Jenkins SSH Keys
ssh-keygen -t rsa -C "[email protected]"
  1. Add public ssh key to authorized
sudo su - jenkins
vim .ssh/authorized_keys

chmod 600 .ssh/authorized_keys
  1. Install docker and add Jenkins user to docker group

https://docs.docker.com/install/linux/docker-ce/ubuntu/

sudo usermod -a -G docker jenkins
  1. Install Docker Compose

https://docs.docker.com/compose/install/

sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose
  1. Install Java on Jenkins slave
sudo apt-get install openjdk-8-jre -y
  1. Add Slave Node to Jenkins
  • Manage Jenkins --> Manage Nodes --> New Node --> fill in the required fields
  1. Open BlueOcean Pipeline and change label to 'slave'

  2. Run BlueOcean Pipeline and test if application deployed successfully on Jenkins slave

Run Application Tests

  1. Open BluOcean Pipeline and Add 'Run Tests' stage
  • Add Step Change Current Dir
  • Add Child Step
docker-compose down
docker-compose build flask-app
docker-compose run flask-app pytest -v
docker-compose down
  • Build Job
  1. Archive JUnit test results
  • Replace run flask-app string from previous point
docker-compose run flask-app pytest -v --junit-xml=/var/opt/junit-report/report.xml
  • Add Step Archive JUnit-formatted test results
  • Select path flask-app/junit-report/report.xml
  • Add cleanup Step sudo rm -rf flask-app/junit-report
  1. If tests passed - deploy new application release

  2. Check Test Report from Jenkins UI

Go to the next lesson https://github.com/pavlobornia/training-ci/tree/master/lesson2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.