Pipline is an important role in helping Jenkins realize the transformation from CI to CD. It is a core plug-in running in Jenkins 2. The tasks of the nodes are connected to realize the complex release process that is difficult to complete with a single task, thereby achieving complex process orchestration and task visualization that is difficult to achieve with a single task. The implementation of Pipeline is a set of Groovy DSL, and any release process can be expressed as a Groovy section. script.
pipline syntax:
Stage: A pipeline can be divided into several stages. Each stage is an operation step, such as clone code, code compilation, code testing and code deployment. A stage is a logical grouping that can be executed across multiple nodes.
Node: Node, each node is a jenkins node, which can be jenkins master or jenkins agent. Node is the specific server that executes step.
Step: Step is the most basic operating unit of Jenkins pipline. From creating a directory on the server to building a container image, it is implemented by various Jenkins plug-ins. There can be multiple steps in a stage, for example: sh "make;
Advantages of pipeline:
Sustainability: The restart or interruption of jenkins will not affect the already executed Pipline Job;
Support for suspension: Pipline can choose to stop and wait for manual input or approval before continuing execution;
Extensible: Easier expansion of plug-ins through groovy programming;
Parallelism Execution: Parallel execution between steps and stages, and more complex interdependencies can be achieved through groovy scripts;
1. Create a pipline job
2. Test a simple pipline job Run
the Pipeline test command
3. Execute the pipline job
4. Automatically generate a pipline script that pulls the code:
Click on the pipeline syntax to jump to the generated script URL
First generate the pipeline grammar
Step 4 is to enter the account password. If it has already been set up, you can directly select root
to generate the pipeline script.
git branch: 'main', credentialsId: 'c2da95c2-0b69-408f-8569-ae5e34f95395', url: '[email protected]:magedu/app1.git'
5.Change pipeline job
node {
stage("clone 代码"){
git branch: 'main', credentialsId: 'c2da95c2-0b69-408f-8569-ae5e34f95395', url: '[email protected]:magedu/app1.git'
}
stage("代码构建"){
echo "代码构建"
}
stage("代码测试"){
echo "代码测试"
}
stage("代码部署"){
echo "代码部署"
}
6. Execute jenkins job
7. Verify git clone log
8. Jenkins server verifies clone code data
(it should be noted that this path file
ll /var/lib/jenkins/workspace/pipline-test/ must be present on the slave server)
9 Execute shell command packaging code in .pipline
node {
stage("clone 代码"){
sh 'rm -rf /var/lib/jenkins/workspace/pipline-test/*'
git branch: 'main', credentialsId: 'c2da95c2-0b69-408f-8569-ae5e34f95395', url: '[email protected]:magedu/app1.git'
}
stage("代码构建"){
sh 'cd /var/lib/jenkins/workspace/pipline-test/ && tar czvf code.tar.gz ./index.html'
}
stage("代码测试"){
echo "代码测试"
}
stage("代码部署"){
echo "代码部署"
}
}
10.pipline deployment example:
Note: Tomcat in web1 and web2 servers needs to be set up
mkdir -p /data/tomcat/tomcat_appdir # 保存 web 压缩包
mkdir -p /data/tomcat/tomcat_webdir # 保存解压后的web目录
mkdir -p /data/tomcat/tomcat_webapps # tomcat app 加载目录,在 server.xml 定义
Set in server.xml;
vim /usr/local/apache-tomcat-8.5.55/conf/server.xml
<Host name="localhost" appBase="/data/tomcat/tomcat_webapps"
unpackWARs="false" autoDeploy="false">
ln -sv /data/tomcat/tomcat_webdir/myapp /data/tomcat/tomcat_webapps/myapp #设置软链接
script style
node {
stage("clone 代码"){
sh 'rm -rf /var/lib/jenkins/workspace/pipline-test/*'
git branch: 'main', credentialsId: 'c2da95c2-0b69-408f-8569-ae5e34f95395', url: '[email protected]:magedu/app1.git'
}
stage("代码构建"){
sh 'cd /var/lib/jenkins/workspace/pipline-test/ && tar czvf code.tar.gz ./index.html'
}
stage("代码复制"){
sh 'cd /var/lib/jenkins/workspace/pipline-test/ && scp code.tar.gz [email protected]:/data/tomcat/tomcat_appdir/'
sh 'cd /var/lib/jenkins/workspace/pipline-test/ && scp code.tar.gz [email protected]:/data/tomcat/tomcat_appdir/'
}
stage("停止 tomcat 服务"){
sh 'ssh [email protected] "/usr/local/apache-tomcat-8.5.55/bin/catalina.sh stop"'
sh 'ssh [email protected] "/usr/local/apache-tomcat-8.5.55/bin/catalina.sh stop"'
}
stage("代码部署"){
sh 'ssh [email protected] "rm -rf /data/tomcat/tomcat_webdir/myapp/* && cd /data/tomcat/tomcat_appdir && tar xvf code.tar.gz -C /data/tomcat/tomcat_webdir/myapp/"'
sh 'ssh [email protected] "rm -rf /data/tomcat/tomcat_webdir/myapp/* && cd /data/tomcat/tomcat_appdir && tar xvf code.tar.gz -C /data/tomcat/tomcat_webdir/myapp/"'
}
stage("启动 tomcat 服务"){
sh 'ssh [email protected] "/usr/local/apache-tomcat-8.5.55/bin/catalina.sh start"'
sh 'ssh [email protected] "/usr/local/apache-tomcat-8.5.55/bin/catalina.sh start"'
}
}
Execute and verify the pipeline job:
Declarative:
Specify node node to run the job
- apt-get install git -y on slave1 and 2.
2. Set up slave1 and 2 and log in to both web1.2 without password.
ssh-keygen
ssh-copy-id jack@10.0.0.105
ssh-copy-id jack@10.0.0.106
git branch: 'develop', credentialsId: 'c2da95c2-0b69-408f-8569-ae5e34f95395', url: '[email protected]:magedu/app1.git'
Declare a node;
node("jenkins-slave1"){
stage("clone 代码"){
sh 'rm -rf /var/lib/jenkins/workspace/pipline-test/*'
git branch: 'develop', credentialsId: 'c2da95c2-0b69-408f-8569-ae5e34f95395', url: '[email protected]:magedu/app1.git',
url: '[email protected]:magedu/app1.git'
}
stage("代码构建"){
sh 'cd /var/lib/jenkins/workspace/pipline-test/ && tar czvf code.tar.gz ./index.html'
}
stage("代码复制"){
sh 'cd /var/lib/jenkins/workspace/pipline-test/ && scp code.tar.gz [email protected]:/data/tomcat/tomcat_appdir/'
sh 'cd /var/lib/jenkins/workspace/pipline-test/ && scp code.tar.gz [email protected]:/data/tomcat/tomcat_appdir/'
}
stage("停止 tomcat 服务"){
sh 'ssh [email protected] "/usr/local/apache-tomcat-8.5.55/bin/catalina.sh stop"'
sh 'ssh [email protected] "/usr/local/apache-tomcat-8.5.55/bin/catalina.sh stop"'
}
stage("代码部署"){
sh 'ssh [email protected] "rm -rf /data/tomcat/tomcat_webdir/myapp/* && cd /data/tomcat/tomcat_appdir && tar xvf code.tar.gz -C /data/tomcat/tomcat_webdir/myapp/"'
sh 'ssh [email protected] "rm -rf /data/tomcat/tomcat_webdir/myapp/* && cd /data/tomcat/tomcat_appdir && tar xvf code.tar.gz -C /data/tomcat/tomcat_webdir/myapp/"'
}
stage("启动 tomcat 服务"){
sh 'ssh [email protected] "/usr/local/apache-tomcat-8.5.55/bin/catalina.sh start"'
sh 'ssh [email protected] "/usr/local/apache-tomcat-8.5.55/bin/catalina.sh start"'
}
}
3. Verify slave execution build
4. Verify web server code version
Setup completed