A brief introduction to Jenkins

1. What is CI/CD

  • CI (Continuous Integration)

    Continuous integration is a software development practice in which team development members integrate their work frequently, usually at least once a day per member, which means that integration may occur multiple times a day, each time through automated builds (including compilations, releases, Continuous integration does not eliminate bugs, but makes them very easy to find and fix

  • CD (Continuous delivery)

    Continuous delivery Is based on continuous integration, where the integrated code is deployed to a production-like environment that is closer to the real world for more testing to detect problems earlier.

    For example, after we finish unit testing, we can deploy the code to more automated integration tests in the Staging environment where we connect to the database, and if the code is ok, we can continue to deploy it manually into production.

  • Continuous integration tools are commonly used

    1. Jenkins
    2. Atlassian Bamboo
    3. CircleCI
    4. GitLab CI
    5. JetBrains TeamCity
    6. ThoughtWorks GoCD
    7. ThoughtWorks Snap

2, Jenkins

Jenkins is an open source, extensible Web-based platform for continuous integration, delivery, deployment (compilation, packaging, deployment of software/code). Allow continuous integration and continuous delivery of projects, regardless of platform, to handle any type of build or continuous integration

  • Jenkins advantages
    1. Free open source
    2. Cross-platform, support all platforms
    3. Master/Slave supports distributed automatic testing
    4. Visual management page in Web form
    5. Simple installation and configuration
    6. Tips Help quickly and in a timely manner
    7. Rich plug-in support (nearly 2000)

Second, Jenkins deployment

1. Environmental requirements

Not less than 256MB memory, not less than 1G disk, JDK version >=8 (OpenJDK also works)

  • Install the JDK
 #Its installation
 sudo apt-get install openjdk-8-jdk
Copy the code

The following operations are based on Ubuntu 18.04

2. Install Jenkins

  • Add repository keys

    wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
    #After the key is added, the system returns OK
    Copy the code
  • Append the Debian package repository address to the server’s sources.list

    echo deb http://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list
    Copy the code
  • To perform the Update

    sudo apt-get update
    Copy the code
  • Install Jenkins and dependencies

    #Install the latest version
    sudo apt-get install jenkins
    #Install the specified Jenkins versionSudo apt - get the install Jenkins = 2.138.1Copy the code

3. Start Jenkins

  • Start Jenkins with systemctl

    sudo systemctl start jenkins
    Copy the code
  • Check Jenkins running status

    sudo systemctl status jenkins
    Copy the code

    If Jenkins runs correctly, the following figure is returned (active):

4. Set Jenkins

After Jenkins runs successfully, port 8080 will be opened (can be changed in configuration file), we can access Jenkins page by typing IP :8080 in the browser

  • Enter the original password

    The first time you visit Jenkins, you will see the following image, prompting you to enter the original Jenkin password:

    On his way from tip can see the original password stored in the/var/lib/Jenkins/secrets/initialAdminPassword files, In the terminal type sudo cat/var/lib/Jenkins/secrets/initialAdminPassword access after the password is copied to the browser passwords in the table.

  • Installing a plug-in

    After entering the password, it will jump to the plug-in installation interface, and select the first recommended plug-in installation:

    Click Install and wait for the plug-in to complete installation:

  • Create a user

    After the plug-in is installed, the interface for creating a new user is redirected:

    Although you can log in as the admin user using the original password above, creating a new user is recommended

    After the user is created, Jenkins is ready! , click Start Using Jenkins to enter Jenkins interface

    At this point, Jenkins deployment is complete

Create your first build task

1. Create a task

  • Click New on Jenkins home page:

  • Enter the task name, select the free style project, and click OK:

2. Configure tasks

After task creation, enter the configuration interface, you can see that the configuration interface is mainly composed of six parts: General, source management, build trigger, build environment, build, build operation

  • General

    This section describes how to configure basic task information, such as name, description, and old version management

  • Source code management

    Configure your repository, configure username and password (private repository), select branch:

  • Build trigger

    • Triggering remote builds (for example, using scripts)

      This option provides an interface that can be used to trigger builds at the code level

    • Build after other projects are built

      Build after a project is built

    • Build periodically

      Build periodically. It’s a good idea to build every once in a while. The calendar format is similar to the Linux Crontab format. The Settings in the figure below represent a build every 30 minutes

    • GitHub hook trigger for GITScm polling

      When changes are pushed to the GitLab repository, the build is triggered. This is followed by an address that triggers the build, commonly referred to as Webhooks. This address needs to be configured into Gitlab, how webhooks do this is described later. This is a common build trigger

    • Poll SCM

      This option is used in conjunction with the option above. Jenkins doesn’t know when the code repository changes. This option needs to be configured periodically to check for changes to the code repository

  • Build environment

    Building an environment is just a bit of preparation before you build it

  • build

    Build steps, where there are many options, such as executing Windows batch commands, executing shell commands, using Ant build tools, etc., to choose on demand

  • Post-build operation

    After the build, the common operation is to open the report, generate the build result to the mailbox, and select the configuration as required

3. Build tasks

After the task is successfully created, click Build Now on the task screen to start building the current task.

4. Change the theme

Jenkins’ default Theme is old, so we used the Simple Theme plugin to change the Theme

1. Install the Simple Theme plug-in

Search for Simple Theme in Jenkins Plugins Manager and click Install

2. Generate a custom theme CSS file

Open afonsof.com/jenkins-mat… Website, choose the color you want, and upload the logo, click download

Upload the downloaded CSS file to userContent/material/ in the Jenkins_home directory, for example: workspace/userContent/material/jenkins-material-theme-green.css

3. Choose a theme

Jenkins System Settings select the Theme, select the CSS URL, and configure the CSS file URL:

Click Save and refresh the page. The theme switch is successful