自动化集成部署

代码持续集成+应用自动化部署

github+jenkins+tomcat, ubuntu 18.04

1. 安装Java

sudo add-apt-repository ppa:webupd8team/java

sudo apt update

sudo apt update

sudo nano /etc/environment,添加

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64/bin/"

source /etc/environment

2. 安装jenkins

安装jenkins

add the repository key to the system:

wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -

append the Debian package repository address to the server's sources.list

sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'

run update so that apt will use the new repository

sudo apt update

install Jenkins and its dependencies

sudo apt install jenkins

启用jenkins

设置端口号,打开/etc/sysconfig/jenkins,修改

JENKINS_PORT="9090"

开启jenkins

sudo systemctl start jenkins

配置jenkins

浏览器打开http://your_server_ip_or_domain:9090,按照引导设置。

系统设置

在插件管理中安装Maven Integration插件,Deploy to container插件

系统设置–>Global Tool Configuration

JDK配置为已安装的java

Maven配置为自动安装

3. 安装tomcat

安装tomcat

sudo apt install tomcat8

sudo apt install tomcat8-admin

打开/var/lib/tomcat8/conf/tomcat-users.xml,tomcat-users节点下添加

<role rolename="manager-gui"/>

<role rolename="manager-script"/>

<user username="admin" password="password" roles="manager-gui,manager-script" />

这里的user信息要在后面配置jenkins访问tomcat时使用。

<!--

打开/var/lib/tomcat8/conf/server.xml ,Host节点下添加

<Context path="" docBase="war_name" debug="0" reloadable="true"></Context>

配置nginx+tomcat来使用no app name path url(上面代码使用tomcat来做到no app name,jenkins部署的时候http://host/appname是最新的,但http://host无法更新)

https://stackoverflow.com/questions/18936753/nginx-reverse-proxy-for-tomcat

-->

绕了很多弯最后还是将项目部署到ROOT下来确保域名访问,注意这时jenkins中post-build actions中部署到tomcat的context path参数应该为 “/”而不是"/ROOT",不然会报错。

开启tomcat

sudo service tomcat8 start

sudo service tomcat8-admin start

4.集成

配置jenkins访问github repository的权限

生成jenkins的ssh秘钥:

sudo su jenkins

ssh-keygen

cat /var/lib/jenkins/.ssh/id_rsa.pub

复制公钥在剪贴板上。

github->repository->settings->deploy keys->add deploy key->将公钥粘贴上去

cat /var/lib/jenkins/.ssh/id_rsa.pub

复制私钥在剪切板上

jenkins->凭据->系统->全局凭据->添加凭据->选择ssh,将私钥粘贴上去,命名为github-key

现在jenkins可以通过github-key访问github repository

配置jenkins访问tomcat的权限

jenkins->凭据->系统->全局凭据->添加凭据->选择username,将上面配置tomcat时用的username和password写上去,命名为tomcat-key

现在jenkins可以通过tomcat-key访问tomcat

新建Maven任务

jenkins中 新建任务->选择Maven任务

Source Code Management选择git,填写repository的地址,使用github-key

Build中将pom的相对路径写进去

Post-build Actions中将tomcat的信息写进去



注:

nginx反向代理和spring security redirect会冲突,需要解决

jenkins中如果配置maven为默认行为,多次build源代码没变的话可能不执行clean,可能会出现已改名的文件仍然在target中,导致有多个matching bean对应一个qualifying bean,报错,最后容器启动失败。


Refer:

jenkins安装

jenkins生成秘钥

git+maven集成

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容