Continuous integration + Automated deployment [code pipeline management and Jenkins and Gitlab integration]
Label (Space delimited): Jenkins
First, the Code pipeline management
PipelineThe name is the meaning of the assembly line, because the company may have many projects. If you build with Jenkins, it's a hassle to develop a build project that requires a single click. So the pipeline noun appears.
After the code quality check is complete, we need to deploy the code to the test environment for automated testing
new Deployment Code Project
Click New
I just need to write a description here.
Execute shell Script
Warm tip: the execution of the command is mainly related to the issue of permissions, we have to understand, Jenkins is what permissions to execute the command. So here's the problem, we're 192.168.56.11 now, if we want to execute the order on 192.168.56.12. What do we need to do?
There are 2 different kinds of things we do without secret key:
1. Use Jenkins user to distribute secret key to 192.168.56.12
2, use the root user to distribute the secret key to 192.168.56.12, if the use of root user also visudo authorization. Because the user who executes the command by default on the Web is Jenkins
1. We use root for password verification
#这里我们的key已经做好, if you don't do it, you can ssh-keygen -t ras generate the secret key directly.
We'll copy the public key on the 192.168.56.11 to 192.168.56.12.
[[Email protected] ~]# Cat. Ssh/id_rsa.pubSsh-rsa aaaab3nzac1yc2eaaaadaqabaaabaqchvqufrgwqp5dkziu4zwxcjrusvmvgn5ljdvl/ qfckmlvphwmsqw06vsphgci1ndjgbkoh5pbjrylyjucig5yij1xumoz2yak32scemxnvheb/g4wnb9vmsgq/ vs4cxru1hdatkth9zdav4qz81x2poyjw5b5lavwz4owqnipz7o3ya6xbxevcimsvtd17okrnqaphsg+ e68kvrexincebcbrggq3bkevgidswpsgncysjc0+csruxuzeo3g6aqgi/qr3noeg91rosoap3fpfjbkgb/sxggkwwjmgiqfxjrug+xmczef4kg /rurnbdy84e5ryhois3xknjurjtxhyd [email Protected][[email protected] ~]# vim. Ssh/authorized_keysSsh-rsa aaaab3nzac1yc2eaaaadaqabaaabaqchvqufrgwqp5dkziu4zwxcjrusvmvgn5ljdvl/ qfckmlvphwmsqw06vsphgci1ndjgbkoh5pbjrylyjucig5yij1xumoz2yak32scemxnvheb/g4wnb9vmsgq/ vs4cxru1hdatkth9zdav4qz81x2poyjw5b5lavwz4owqnipz7o3ya6xbxevcimsvtd17okrnqaphsg+ e68kvrexincebcbrggq3bkevgidswpsgncysjc0+csruxuzeo3g6aqgi/qr3noeg91rosoap3fpfjbkgb/sxggkwwjmgiqfxjrug+xmczef4kg /rurnbdy84e5ryhois3xknjurjtxhyd [email Protected][[email protected] ~]# SSH 192.168.56.12The authenticity ofHost '192.168. the.(192.168. the.) ' Can ' t be established. ECDSA Key Fingerprint isB5: About:8F:F1:Geneva:2D:CB:7D: on: -: -: A: the:9C: *:8C.are sure you want to ContinueConnecting (yes/no)? Yeswarning:permanently added '192.168. the.' (ECDSA) to the List ofKnown hosts. Last Login:sat Dec - Geneva: -: to . from 192.168. the. 1[[Email protected] ~]# llTotal4-rw-------. 1 root root 1021 Dec 05:56 anaconda-ks.cfg#现在SSH连接就不需要密码了Empower Jenkins users, use Visudo or edit profiles/etc/sudoers[[email protected] ~]# vim/etc/sudoers theJenkins all= (All) nopasswd:/usr/bin/ssh#jenkins授权所有主机, SSH is not required for password execution. Remember not to authorize all
We write a simple script on the 192.168.56.12 shell that detects if it can be executed successfully. Formal environment can write a script for automated deployment
[root@linux-node2 ~]# echo "echo "hello word"" >demo.sh[root@linux-node2 ~]# chmod +x demo.sh[root@linux-node2 ~]# ll demo.sh116Dec1702:24 demo.sh
Jenkins writes execution scripts
Then we click Build Now
Success!
Now we're going to connect the code quality management and test deployment together.
That's when you use git hooks.
We need to install the Jenkins plugin parameterized
We choose demo-deploy
It will appear Trigger parameterized build on other projects when you click on the project settings again
Tip: Projects to build set up a project for the build. For example, if we want to execute the test after building the code project, we can fill in the test here.
Finally click Save and click Build. We look at the effect
#这样我们每次点击demo-deploy It builds on Auto-deploy after the build is complete
download Pipeline. This only requires building a project that will help us complete all relevant projects
Search Plugin Pipeline
Wait for installation to complete
We click on the homepage + number, create a new attempt to
Click OK
Pipeline configuration
Then we click Save
The pipeline view is as follows:
Click Run
This allows for code quality to be managed before you start deploying the
After the build is successful:
So next time we want to see the pipeline view, click on the demo-pipeline above
Second, Jenkins + Gitlab Integration
Jenkins + gitlabAfter integration, the implementation of the function is to develop and write code submitted to the Gitlab, when the start push to Gitlab, Jenkins automatically help us build immediately
We need to install a gitlab钩子 script for this project
Hint: Jenkins needs to install plugins no matter what function you want to implement!!
After installing the plugin, we start to configure the hook script
Here we need to write a token on the server and write a token on Jenkins. These two can be connected together.
#因为用到了令牌我们还需要在安装一个插件, otherwise it will not be available. Because tokens are required to be logged in, there is a need to have a managed plug-in
Plugin Search: Build Aut
For the sake of token security, we use OpenSSL to generate a
[root@linux-node1 ~]# openssl rand -hex 100a37c6d7ba1fe3472e26
Then we can click Save to
Because Jenkins also prompts us to add a hook script on Gitlab.
Click on the project we created
Select Webhooks
Build Authorization Token Root Plugin Plugin usage Instructions
Https://wiki.jenkins-ci.org/display/JENKINS/Build+Token+Root+Plugin
After using the build plugin, the URL is as follows:
http://192.168.56.11:8080/buildByToken/build?job=auto-deploy&token=0a37c6d7ba1fe3472e26auto-deploy=项目名称(构建时的项目名称)0a37c6d7ba1fe3472e26=jenkins填写的令牌
And then tap Add Webhook
We'll see this option below and we'll click Test to test
Test results
gitsubmit code to the server to verify that it can be deployed automatically:
[root@linux-node1 ~]# echo "Build Token Root Plugin" > index.html[root@linux-node1 ~]# git add index.html [root@saltmaster ~/weather]# git commit -m "text"[root@linux-node1 ~]# git push origin master
Logging of Jenkins Servers:
[root@linux-node1 ~]# tail -f /var/log/jenkins/jenkins.log
Jenkins Project Build:
Access the Web interface to verify that the code is up-to-date:
Jenkins Console output information:
Continuous integration + Automated deployment [code pipeline management and Jenkins and Gitlab integration]