jenkins流水线项目发布
jenkins流水线项目发布(拉代码、打包、发布)
环境
系统 | ip、主机名 |
redhat 8 | 192.168.100.137/redhat:提前安装好了jenkins |
redhat 8 | 192.168.100.138/tomcat1:提前安装好了jenkins |
安装jenkins请参考Jenkins配置使用
替换Jenkins源为国内
https://updates.jenkins.io/update-center.json 改为 https://mirror.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
Jenkins打包的方式:
maven
ant
gradle
在使用功能之前,先安装几个插件
git
pipeline
gitee
如果安装报错,请多试几遍,多半是网络问题
创建新项目
这里的连接可以去github或者码云找,我贴一个现在用的
http://github.com/lizhenliang/tomcat-java-demo.git
//在本地虚拟机可以看得到拉取下来的代码 [root@RedHat ~]# cd .jenkins/ [root@RedHat .jenkins]# ls com.dabsquared.gitlabjenkins.connection.GitLabConnectionConfig.xml com.dabsquared.gitlabjenkins.GitLabPushTrigger.xml com.gitee.jenkins.connection.GiteeConnectionConfig.xml com.gitee.jenkins.trigger.GiteePushTrigger.xml config.xml hudson.model.UpdateCenter.xml hudson.plugins.git.GitTool.xml identity.key.enc jenkins.install.InstallUtil.lastExecVersion jenkins.install.UpgradeWizard.state jenkins.model.JenkinsLocationConfiguration.xml jenkins.security.apitoken.ApiTokenPropertyConfiguration.xml jenkins.security.QueueItemAuthenticatorConfiguration.xml jenkins.security.UpdateSiteWarningsConfiguration.xml jenkins.telemetry.Correlator.xml jobs logs nodeMonitors.xml nodes org.jenkinsci.plugins.workflow.flow.FlowExecutionList.xml plugins queue.xml queue.xml.bak secret.key secret.key.not-so-secret secrets updates userContent users workflow-libs workspace [root@RedHat .jenkins]# cd workspace/ [root@RedHat workspace]# ls tomcat_demo [root@RedHat workspace]# ls tomcat_demo/ db Dockerfile LICENSE pom.xml README.md src
尝试打包代码
代码
pipeline { agent any stages { stage('pull code') { steps { git 'http://github.com/lizhenliang/tomcat-java-demo.git' } } stage('package') { steps { sh """ mvn clean mvn package """ } } } }
这里失败了好多次,因为网络的原因,请在网络环境良好的情况下做
尝试部署
给第二台机器做免密认证
//RedHat //做免密认证 [root@RedHat ~]# ssh-keygen -t rsa [root@RedHat ~]# ssh-copy-id root@192.168.100.138 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.100.138's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.100.138'" and check to make sure that only the key(s) you wanted were added.
//tomcat1 //关闭防火墙和selinux [root@RedHat tomcat]# setenforce 0 [root@RedHat tomcat]# systemctl disable --now firewalld
代码
pipeline { agent any stages { stage('pull code') { steps { git 'http://github.com/lizhenliang/tomcat-java-demo.git' } } stage('package') { steps { sh """ mvn clean mvn package """ } } stage('deploy') { steps { sh """ scp target/ly-simple-tomcat-0.0.1-SNAPSHOT.war root@192.168.100.138:/usr/src/tomcat/webapps/ ssh root@192.168.100.138 '/usr/src/tomcat/bin/catalina.sh stop && /usr/src/tomcat/bin/catalina.sh start' """ } } } }
//tomcat1 //查看tomcat端口号,已经起来了,说明部署成功 [root@RedHat tomcat]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:* LISTEN 0 100 *:8080 *:* [root@RedHat tomcat]# ls bin conf lib logs README.md RUNNING.txt webapps BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work [root@RedHat tomcat]# ls webapps/ docs host-manager ly-simple-tomcat-0.0.1-SNAPSHOT.war ROOT examples ly-simple-tomcat-0.0.1-SNAPSHOT manager
去浏览器访问tomcat1机器,访问到了部署的项目