introduction
This paper describes commonly used Linux server springboard machine jenkins, remote compile deployment environment to build.
Series Document Catalog
Linux server Java environment deployment series (a): Install the JDK
Linux server Java environment deployment series (two): Install nginx, mysql, tomcat, redis
Linux server Java environment deployment series (three): install git, maven
Linux server Java environment deployment series (four): jenkins environment to build, deploy projects
Build jenkins
Jenkins installation, installation using downloaded from a remote online form, after the completion of the corresponding need to modify the configuration.
installation steps
- Download: wget https://pkg.jenkins.io/redhat-stable/jenkins-2.176.3-1.1.noarch.rpm
- Installation: rpm -ivh jenkins-2.176.3-1.1.noarch.rpm
- Modify the configuration file: vim / etc / sysconfig / jenkins
- jenkins default port is 8080 (proposed changes, in order to avoid a conflict of tomcat ports, etc.)
- user to root
- Jenkins modify the jdk directory:
- View jdk installed Location: whereis jdk
- Edit initial configuration file (add jdk directory): vim /etc/init.d/jenkins
- Save Changes: systemctl daemon-reload
- 启动 jenkins:systemctl start jenkins
- View password (required when you first start): cat / var / lib / jenkins / secrets / initialAdminPassword
jenkins code and cloud
jenkins can and GitHub, Gogs, code-yun, a common project management git remote site deployment, paper yard cloud, for example, the other is also similar.
- git config --global user.name "[your username]"
- git config --global user.email "[Your email]"
- ssh-keygen -t rsa -C "[Your email]" (default Enter on the line)
- View generated public key: tail /root/.ssh/id_rsa.pub
- Check whether a successful connection: ssh [email protected]
- /Root/.ssh/id_rsa.pub copy the contents to the cloud configure ssh public key code
Click in gitee in: Settings -> Security Settings -> ssh public key, copy the contents of the public key to the public key in the text box, you can just write a title
System Configuration
Jenkins console access addresses, external network server using public ip: http: // server ip: 8888 /, the initial password: / var / lib / jenkins / secrets / initialAdminPassword
Select "Install suggested plugins" to install the default plug-ins, following Jenkins will themselves to download the plug-in installation.
Create a super administrator account, you can skip, if you encounter the case of 403, returned to try again next
System Management -> Global Configuration Tool configuration (if the server is installed maven, jdk, remove the automatic installation, choose their own server installation directory) -> Click Save me here is already installed on the server.
System Management -> Plugin Manager (install plug-ins that you need)
It recommended because the front part of the installation, and then install a few more useful Jenkins plugins: Rebuilder, Safe Restart, Publish Over SSH, Maven Integration Click: System Management -> Plugin Manager -> Optional plug-in, enter a search plug-in plug-in name, installing log in again next to take effect.
Project deployment
In the Jenkins home page click on <create a new task>, then enter the task name, chose to build a maven project, Note: Only the Maven Integration plugin installed have this option
Use the shell to compile the project into a jar manner, according to the actual situation to write project
#!/bin/sh echo "========== Start shell mv and build ============" # 在jenkins环境中一定要加这句话,否则这个脚本进程最后会被杀死 export BUILD_ID=dontKillMe # 指定最后编译好的jar的存放位置 JAR_PATH=/data/module/demo # 如果路径不存在,就创建路径 [ ! -e $JAR_PATH ] && mkdir -p $JAR_PATH # 指定jenkins中存放编译好的jar的位置 JENKINS_JAR_PATH=/var/lib/jenkins/workspace/springboot-demo/target # 如果路径不存在,就创建路径 [ ! -e $JENKINS_JAR_PATH ] && mkdir -p $JENKINS_JAR_PATH # 指定jenkins中存放编译好的jar的名称(这个jar的名字和pom文件配置有关) JENKINS_JAR_NAME=demo-1.0.0.jar # 获取该项目的进程号,用于重新部署项目前杀死进程 process_id=$(ps -ef | grep elim-church | awk '{print $2}') # 如果该项目正在运行,就杀死项目进程 if [[ ! -z "$process_id" ]] then echo "========== Stop Service ============" kill -9 $process_id echo "========== Restart Service ============" else echo "========== Service is not Start ============" fi # 进入Jenkins中编译好的jar的位置 cd ${JENKINS_JAR_PATH} # 将Jenkins中编译好的jar复制到最终存放项目jar的位置 cp $JENKINS_JAR_PATH/$JENKINS_JAR_NAME $JAR_PATH # 进入到存放项目jar的位置 cd ${JAR_PATH} # 后台启动项目,并且将控制台日志输出到nohup.out中 nohup java -jar ${JENKINS_JAR_NAME} >execute.log & echo "========== End shell mv and build ============" echo "shell脚本执行完毕"
保存后,进到该maven项目下,点击立即构建,构建失败的话可以先清理下工作空间
查看控制定输出日志
然后到阿里云服务器使用netstat -nltp查看是否有已经成功运行的项目,这儿的端口是springboot的application.properties一样的端口。