Jenkins easy to use tutorial

Source: Internet
Author: User
Jenkins is a software that can improve the efficiency of the software development process, which can help you to form a workflow, typical workflows include the following steps 1. Developed 2. Submitted 3. Compile 4. Test 5. Released with the help of Jenkins, in these 5 steps, in addition to the 1th step, the next 4 steps are automated, specific, when you complete the submission, Jenkins will automatically run your compilation script, after the successful compilation, and then run your test script, this step succeeds, then it will help you release the new program, In particular, in the final step, you can choose to manually publish, or automatically released, after all, release this matter, or the need for artificial confirmation is better. In short > Jenkins can help you after the completion of the code, a key to complete the development process of a series of work to use the benefits of Jenkins is obvious, it reduces the duplication of your work. More importantly, the development process of a team at the beginning is inconsistent, inconsistent often brings a variety of problems, ultimately reflected in the quality of software or development efficiency is not high enough, and Jenkins will help you regulate the behavior of everyone, so as to avoid a series of problems. # # Install Jenkins installation is very simple, take CentOS as an example, execute the following command
sudo wget-o/etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
sudo rpm-- Import https://jenkins-ci.org/redhat/jenkins-ci.org.key
sudo yum-y install Jenkins
It's worth noting that if your operating system is CentOS and running a gcj version of Java, you need to update
$ java-version
java Version "1.5.0"
gij (GNU libgcj) version 4.4.6 20110731 (Red Hat 4.4.6-3)
$ sudo yum Remo ve java
$ sudo yum install-y java-1.7.0-openjdk
$ java-version
java Version "1.7.0_79"
openjdk Runtime E Nvironment (rhel-2.5.5.1.el6_6-x86_64 u79-b14)
OpenJDK 64-bit Server VM (build 24.79-b02, Mixed mode)
If your Jenkins uses Git as a conduit for data transfer, then all Jenkins nodes will have to install Git
$ sudo yum install-y git
Set up git accounts
$ git config--global user.name "yourname"
$ git config--global user.email "Yourmail"
# # Configuration After the successful installation, the configuration file under '/etc/sysconfig/jenkins ', the default port is ' 8080 ', you need to set up a firewall, so that the port can be external access to the settings allowed to boot
$ sudo chkconfig Jenkins on
Make sure that there is a Jenkins account in the system, and if not, create it, and the user will be created automatically when Jenkins is installed. The SSH key is then created, and the key is used for free access in multiple nodes while helping to get through the GIT data channel. Before that, verify that the Jenkins user's ' home ' directory is valid (in the example ' home ' is '/var/bin/jenkins ') and switch to the Jenkins user
$ grep jenkins/etc/passwd
jenkins:x:496:496:jenkins Continuous Integration Server:/var/lib/jenkins:/bin/bash
$ su Jenkins
$ cd ~
$ pwd
/var/lib/jenkins
Creates an asymmetric key, executes the ' ssh-keygen ' command, and returns all the way
$ ssh-keygen
generating public/private RSA key pair.
Enter file in which to save the key (/HOME/FENGYAJIE/.SSH/ID_RSA): Enter passphrase (empty for no passphrase):
enter S Ame Passphrase again:
Your identification has been saved.
Your public key has been saved in/home/fengyajie/.ssh/id_rsa.pub.
The key fingerprint is: The
key ' s randomart image is:
+--[RSA 2048]----+
|        ....   +=|
|        ... .....|
|         . ... o +|
|          E.. *.|
|        S  . = +  |
|         . O +.             | | o O |
|              o  |
+-----------------+
$ ls ~/.ssh/
id_rsa  id_rsa.pub  known_hosts
Jenkins is a Master-slave architecture that publishes tasks to different nodes, typically with 2 running environments, one for test environments and one for production environments, where you can specify which tasks are performed in a test environment, and Which tasks are executed in the production environment. Of course, if you do not have such a demand, you can not configure slave, this article discusses the situation is slave. If you need to configure slave, create a Jenkins user on the slave node, and build a trust relationship between master and slave (you need to replace the following ' host ' with a specific server IP, and be sure to ensure that the master and the slave are intranet communications, Otherwise, the public network environment delay is large, often appear slave off the line situation)
SSH jenkins@host ' mkdir-p. SSH && cat >> Ssh/authorized_keys ' < ~/.ssh/id_rsa.pub
At the same time, to allow Jenkins to execute commands with higher privileges, all nodes need to set the Jenkins user to sudo. Of course, I'm just trying to be lazy, and a better way is to set up a dedicated user group that has certain permissions, and then add Jenkins to this group of users.
$ sudo grep jenkins/etc/sudoers
Jenkins          all= (All)   Nopasswd:all
The above is the configuration of all terminals, the remaining operations are basically done on the Web page provided by Jenkins set Slave

Open Jenkins page http://hostname:8080, go to admin page and click New Node


Configuration Node,labels is an environment setting, such as a development environment, a test environment, a compilation environment, and so on, which can be followed by specifying specific tasks to be performed in an environment based on the lables value.

To set the credit method between master and node

After successful, you can see the new node in the console

Create Pipeline

After the basic environment is set up, let's configure a workflow to feel it personally.

The run-time behavior of a workflow in Jenkins, known as Pipeline,pipeline, is defined by the user, the content of the definition is stored in a jenkinsfile file, and the file is stored in the root directory of the Git repository, as follows: The user submits the code to git Jenkins the latest code from Git to read the jenkinsfile files in the root directory and sequentially execute the tasks defined in the file

The following is a specific configuration step ### writing Jenkinsfile

 Pipeline {agent {label ' Production '} stages {stage (' build ') {ste PS {echo ' Building '}} stage (' Te        
        St ') {steps {echo ' testing '} } stage (' Deploy-staging ') {steps {sh './deploy s        
        Taging ' sh './run-smoke-tests '}} Stage (' sanity check ') {steps {input ' does the staging Environme        
        NT look OK? "}}            
            Stage (' Deploy-production ') {steps {sh './deploy Production '        
 }} post {       Always {echo ' one way or another, I have finished ' Deletedir ()/*        
        Clean up our workspace */} success {echo ' I succeeeded! '        
        } unstable {echo ' I am Unstable:/'}        
        Failure {echo ' I failed:('}    
    Changed {echo ' things were different before ... '}
 }
}
The above is a basic Jenkinsfile template with the following key concepts * Agent-Specify on which machine to perform the task, remember the ' label ' when it is configured ' node ', and if the two ' label ' matches, execute in the ' node ' Stage-The large steps that make up the workflow, these steps are serial, such as Build,test,deploy steps-describe the small steps in stage, stage in the same steps can parallel * sh-execute SHELL command * input-requires you Manually Click OK, Pipeline will enter the follow-up, often used in the deployment process, because many times the deployment requires a number of people to make some confirmation * post-all pipeline after completion, will enter the post link, this link generally do some cleanup work, At the same time, you can also judge the execution status of pipeline. After you know this, you'll find it easy to write a jenkinsfile. OK, now to test the pipeline function, replace the ' sh ' in the above code with ' echo ', copy it into your jenkinsfile, and store it in the root directory of the Git repository.Create Pipeline

Back to Jenkins Web page, add pipeline

If you want to automatically execute the pipeline every time git commits, there are two ways to get Jenkins to poll git, check the git repository for updates every minute, and configure the following

Another way is to use the hook provided by Git, which is the principle that when git commits, it triggers the script in the hook, allowing the script to send Jenkins instructions to execute the pipeline, which is more elegant, but with a bit more to do, this is not the way to do it, Interested students can study for themselves.

Finally, we need to set up Git's address, where the credit settings are consistent with the above mentioned master to node credit settings

Once the setup is complete, once your git repository receives a new submission, it triggers the pipeline operation, and the following figure is the running state of the jenkinsfile example above, and you can see that when you run to the sanity check this step, you need to manually trigger whether or not to perform the following actions.

Let's summarize, this tutorial is mainly about the following aspects: Jenkins is what, and its application scenario example how to build a Jenkins service Jenkins Pileline is what we define a pipeline How to specifically operate a pipeline

Have fun.

Author: cow _ Feng Yaje
Link: https://www.jianshu.com/p/b524b151d35f
Source: Jianshu
Copyright belongs to the author. Commercial reprint please contact the author to obtain authorization, non-commercial reprint please indicate the source.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.