③ of continuous Integration: Automatically deploy code to test environment
One: This article in the last article on the basis of continuing to operate, the last one to get the code from GIT and code testing, this article will be on the basis of the previous implementation of the code to deploy to the test environment.
1.1: Create a new project called Web-demo-deploy for Code Publishing, the last project Web-demo can be used for code testing, when there is a problem in the test phase will not be immediately published, only when the test passed after the release of the project can be executed:
1.2: How to publish the code to the Web server:
1.2.1: You can publish code by executing commands or scripts, creating a WWW user on each Web server to start the Web service and publish the code:
# useradd www # echo ' 123456 ' |
passwd--stdin www # su-www $ ssh-keygen-t RSA generating public/private RSA key pair.
Enter file in which to save the key (/HOME/WWW/.SSH/ID_RSA): Created directory '/home/www/.ssh '. Enter passphrase (empty for no passphrase): Enter same passphrase Again:your identification has been saved
/.ssh/id_rsa.
Your public key has been saved in/home/www/.ssh/id_rsa.pub. The key fingerprint is:bc:51:20:7d:cc:bb:de:e8:e4:11:d3:f7:1b:ec:0c:0d www@node1.chinasoft.com the key ' s Randomart Image is: +--[RSA 2048]----+ | .. O | | ...+ |
|
... | | .
... | | S O.. E. | | o.o. +. |
| .. Oo.
+.| | Oo..
+ o| |. O + | +-----------------+ [www@node1 ~]$ cat ssh/id_rsa.pub Ssh-rsa aaaab3nzac1yc2eaaaadaqabaaabaqdivexdg2txu3+xzvdjxuur/ orc0c9g1vgfkd5c67mokije+oi1eydl4yoqsabjbp7ahjedomfo7mjojsqeqdhebgpcvg7/ Ron5iof7ql3rllhobdhmrmjhsuhbzqjcpm2qqiejkdwm4qpnkfcjuxozlgdkfivfniaajky3bubykrt64gz4pykozxqtlx7fdhaoqzujqy3iuclhk0judrdlu Waywnrxorbgxfukibuxifixhkbdvdr93zlDqcgxxcmsctlilq+yfaeeb11k+/z0uequ6l9skd4i8v5503kifsvwssfl40zbfwcp20nk3prrh5cfd2piwblpqbyvhzpl Www@node1.chinasoft.com
1.2.2: The user's public key is added to the deployment key on the GIT server, and the root's public key is added to the SSH key to give the WWW user permission to obtain code, giving the root user permission to submit code:
SSH keys differs from deploy keys:
GitHub account SSH keys, the equivalent of this account of the highest key, as long as the account has the right (any project), can be operated.
Warehouse deploy keys, as the name implies is this warehouse's proprietary key, with this key, can only operate this project, other projects do not have permissions.
As you have a big villa, SSH key can open any room in the villa. And deploy key can only into a villa in a single room.
1.2.4: Verify that the WWW user has the pull substitution code permission:
1.3: About shell script Execution permissions:
#稍后会通过jenkins执行一个脚本, to complete the release of the code, but the default execution of the user is Jenkins, you need to give Jenkins certain permissions, the other published scripts may or may not be on the local computer, this idea is not native save script, then set the following:
1.3.1: Troubleshoot script run issues:
#将脚本放在www用户家目录/home/www,git Code is also placed in the home directory, so need to Jenkins server remote to the code Publisher to execute remote command, need to do a login-free authentication, The public key of the Jenkins server root and the WWW user is placed in the WWW user home directory. Ssh/authorized_keys file of the code deployment server so that the Jenkins server can invoke the script that deploys the server without entering a password:
Jenkins Server: 192.168.3.199
Deploy Deployment server: 192.168.3.12
$ chmod authorized_keys
$ cat Authorized_keys
[www@192.168.3.12 ~]$ cat. Ssh/authorized_keys Ssh-rsa
Aaaab3nzac1yc2eaaaadaqabaaabaqdivexdg2txu3+xzvdjxuur/orc0c9g1vgfkd5c67mokije+ oi1eydl4yoqsabjbp7ahjedomfo7mjojsqeqdhebgpcvg7/ Ron5iof7ql3rllhobdhmrmjhsuhbzqjcpm2qqiejkdwm4qpnkfcjuxozlgdkfivfniaajky3bubykrt64gz4pykozxqtlx7fdhaoqzujqy3iuclhk0judrdlu waywnrxorbgxfukibuxifixhkbdvdr93zldqcgxxcmsctlilq+yfaeeb11k+/ Z0UEQU6L9SKD4I8V5503KIFSVWSSFL40ZBFWCP20NK3PRRH5CFD2PIWBLPQBYVHZPL www@node1.chinasoft.com
Ssh-rsa aaaab3nzac1yc2eaaaadaqabaaabaqcsyf1plyfbuhthxz5pqpml9tvjxzkekb/6vimecdnbqdhrwze+oqiwp+ gtbkhncxdejd1pbvvqscpiuxlz/r7oebrtptjmzoaalcrmljhx2imsgtdyjsqzfxmaxri+f/zpkkypdw2zlmljyqb6zhk+9/simvgwzw/ ey3kqaqovi7uqmol/59xjah+9zngbotpzi613lx5vrgcghwus5nhxu/dnuwjaxfuyjqr7elkvrg/ Vzjcbtwmgprdccq03kl2mz0lhhkgzvdhwqhipcyrjkrdh0/wqatlpuiyz3bzu33aqsxxv5gmgv6vqfikyvu0ufewl4znpkfpa1z4majpr Root@node1.chinasoft.com
1.3.2: Confirm can be password-free remote login:
Use root and WWW users to test whether a secret key can be logged in to allow the deployment server to add the user's key to the Know_keys, or the error host key verification failed
SSH www@192.168.3.12
ssh www@192.168.3.13
1.3.3: Resolves Jenkins problem with no permissions and operates on Jenkins server 192.168.3.199:
# vim/etc/sudoers
#Defaults requiretty #注释掉, do not need TTY
Jenkins all= (All) nopasswd:/usr/bin/ssh
1.3.3: Configure Jenkins Project execution shell script:
1.3.3.1: script content (needs to be placed in the/HOME/WWW directory of the Deployment server 192.168.3.12):
www@192.168.3.12 $ vim dep.sh
#!/bin/bash
cd/home/www/web-demo_deploy/#进入到本地代码库
git pull #从git服务器更新代码
scp-r./* www@192.168.3.12:/webroot/web_www #将代码部署至web服务器
scp-r./* Www@192.168.3.13:/webroot/web_www
www@192.168.3.12 $ chmod +x dep.sh
1.3.3.2: Invoke script in Jenkins:
#在项目的构建步骤调用, Project-Configuration-build-add build steps-execute Shell
To access a Web page test:
Create code in GIT warehouse and update to GIT server:
[Www@master web-demo_deploy]$ vim index.html # add www.chinasoft.com
[www@master web-demo_deploy]$ git add index.html
[www@master web-demo_deploy]$ git commit-m ' edit index.html add www.chinasoft.com '
[Master 51f8f11] Edit index.html Add www.chinasoft.com
1 file changed, 1 insertion (+), 1 deletion (-)
[Www@master W eb-demo_deploy]$ git push origin master
counting Objects:5, done.
Compressing objects:100% (3/3), done.
Writing objects:100% (3/3), 313 bytes | 0 bytes/s, done.
Total 3 (Delta 2), reused 0 (Delta 0) to
git@192.168.3.198:web/web-demo_deploy.git
ac41e81. 51F8F11 Master-> Master
Execute project build again in Jenkins
You can see that the code you just updated succeeded
1.4: Let the Code test Project Management code publish the project, and automatically invoke the code-published project to complete the code deployment after the successful execution of the Code test project:
1.4.1: Install plugin, Jenkins plugin default installation path
# ll/var/lib/jenkins/plugins/
Installation path for Tomcat version:
/usr/local/tomcat/webapps/jenkins/web-inf/detached-plugins/
If the plug-in online installation is not successful, you can download the plugin to this directory and then change the owner group to Jenkins and restart the Jenkins service to complete the installation:
#系统管理-Admin Plugin-optional plugins, search parameterized:
1.4.2: Configure the Project demo after the build operation, the demo build completed automatically build Demp-deploy project:
#jenkins-->web-demo--> Configuration--> after build operation:
1.4.3: The configuration is as follows:
1.5: Test, execute code test if the project succeeds, will the code deployment project be executed automatically:
Console output
Started by user admin building in Workspace/home/jenkins/.jenkins/workspace/web-demo > Git rev-parse--is-inside-work -tree # timeout=10 fetching changes from the remote git repository > git config remote.origin.url git@192.168.3.198:web /web-demo.git # timeout=10 fetching upstream changes from Git@192.168.3.198:web/web-demo.git > Git--version # timeout= Using GIT_SSH to set credentials Gitlab_web-demo > GIT fetch--tags--progress git@192.168.3.198:web/web-demo.git +r efs/heads/*:refs/remotes/origin/* > Git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > Git Rev-parse Refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision B8f3be4385efdf64606158c23f9f1992bb2da1d3 (refs/remotes/origin/master) Commit message: "Add www.chinasoft.com" > git config core.sparsecheckout # timeout=10 > Git checkout-f b8f3be4385efdf64606158c23f9f1992bb2da1d3 > Git rev-lis T b8f3be4385efdf64606158c23f9f1992bb2da1d3 # timeout=10 [Web-demo] $/usr/locAL/SONAR-SCANNER/BIN/SONAR-SCANNER-E-dsonar.host.url=http://192.168.3.199:9000/-Dsonar.language=php- Dsonar.projectname=web-demo-dsonar.projectversion=1.0-dsonar.sourceencoding=utf-8-Dsonar.projectKey=web-demo- dsonar.sources=./-dsonar.projectbasedir=/home/jenkins/.jenkins/workspace/web-demo info:scanner configuration file :/usr/local/sonar-scanner/conf/sonar-scanner.properties info:project root configuration File:none info:sonarqube Scanner 2.6.1 Info:java 1.8.0_111 Oracle Corporation (64-bit) info:linux 3.10.0-514.el7.x86_64 amd64 info:error
Aces are turned on. Info:user cache:/home/jenkins/.sonar/cache info:load global repositories Info:load Global repositories (done) | time=172ms warn:property ' Sonar.jdbc.url ' isn't supported any more. It'll be ignored.
There is no longer any DB connection to the SQ database. Warn:property ' Sonar.jdbc.username ' isn't supported any more. It'll be ignored.
There is no longer any DB connection to the SQ database. WARN: Property ' Sonar.jdbc.password ' isn't supported any more. It'll be ignored.
There is no longer any DB connection to the SQ database. Info:user cache:/home/jenkins/.sonar/cache info:load plugins index info:load Plugins index (done) | TIME=3MS info:sonarqube server 5.6.6 info:default locale: "en_US", Source code encoding: "UTF-8" Info:process Project P Roperties Info:load Project repositories Info:load Project repositories (done) | Time=97ms info:load Quality Profiles info:load quality profiles (done) | Time=34ms info:load Active Rules info:load Active rules (done) | Time=380ms WARN:SCM provider Autodetection failed. No SCM provider claims to support this project.
Please use the Sonar.scm.provider to define SCM of your project. Info:publish mode INFO:-------------Scan Web-demo info:language is forced to PHP info:load server rules info:load SE RVer rules (done) | Time=71ms info:base dir:/home/jenkins/.jenkins/workspace/web-demo info:working dir:/home/jenkins/.jenkins/Workspace/web-demo/.sonar info:source paths:. Info:source encoding:utf-8, default locale:en_us info:index files info:0 files indexed profiles for PHP: Sonar Way Info:sensor Lines Sensor info:sensor Lines Sensor (done) | time=0ms info:sensor SCM Sensor Info:no SCM System was detected.
can use the ' Sonar.scm.provider ' property to explicitly specify it. Info:sensor SCM Sensor (done) | TIME=0MS info:sensor Analyzer for "php.ini" Files Info:sensor Analyzer for "php.ini" files | Time=3ms info:sensor sonarjavaxmlfilesensor info:sensor sonarjavaxmlfilesensor (done) | time=0ms info:sensor Zero Coverage Sensor info:sensor Zero Coverage Sensor (done) | time=0ms info:sensor Code colorizer Sensor info:sensor Code colorizer Sensor (done) | time=0ms info:sensor CPD block Indexer info:defaultcpdblockindexer are used for PHP info:sensor CPD blocks Indexer (done) | time=0ms info:calculating CPD for 0 files INFO:CPD calculation finished info:analysis Generated in 47ms, dir size=8 KB info:analysis reports compressed in 7ms, zip size=3 KB info:analysis i N 47ms info:analysis Successful, you can browse Http://192.168.3.199:9000/dashboard/index/web-demo info:note so you wi ll be able to access the updated dashboard once the server has processed the "analysis of the submitted" about th e-processing at http://192.168.3.199:9000/api/ce/task?id=AV0YJcbrykzBCcoFv4Mt INFO:------------------------- -----------------------------------------------info:execution SUCCESS INFO:------------------------------------- -----------------------------------info:total time:43.045s info:final memory:42m/137m INFO:----------------------- -------------------------------------------------Warning:you have no plugins providing access control for builds, so FAL
Ling back to legacy behavior of the permitting any downstream builds to is triggered triggering a new build of Web-demo_deploy Finished:success
1.6:pipeline plugin:
1.6.1: #安装插件, System Management-management Plug-ins-installable Plug-ins:
1.6.2: Create a view:
1.6.3: Custom Name:
1.6.4: Configure pipeline information, click OK, pop up the following view
Click on the final interface shown after the Save: