仅 Tomcat WAR 部署使用 Jenkins
Only Tomcat WAR deployment using Jenkins
仅 Tomcat WAR 使用 Jenkins 部署:
部署场景如下:
- 3 环境生产、预生产和鉴定
- 2 War 个文件,例如 app1 和 app2
- 2 Tomcat 个服务器,例如每个环境的 Server1 和 Server2(点 1)
War 个文件场景:
- 开发人员提供了 war 个文件,例如 app1##20171122.war 和 app2##20171122.war
- app1 和 app2 设置文件不是 war 的一部分。
- 每个环境(生产、预生产和鉴定)的设置文件不同
- 部署 app1 和 app2 后,我们将设置文件放在 webapps/app1##20171122/config/Application.settings 和 webapps/app2##20171122/config/Application.settings
最后 app1 和 app2 开始使用 tomcat 管理器
这是我想用 Jenkins 做的事情:
使用 Mavin 部署或任何其他工具:
注意:此处War文件大小超过450MB
Jenkin 项目 App1:
- 我们会将 app1##20171122.war 文件放在 Jenkins 服务器上
- 我们应该能够自动 select 用于部署的 war 文件以及 war 版本
- 我们应该能够在多个服务器(Server1 和 Server2)上部署 war 个文件
- 在 webapps/app1##20171122/config/Application.settings
下部署 app1 的设置文件
- 启动应用程序1
注意:应用程序尚未准备好从 webapps 外部读取配置
简而言之:
(Jenkins 服务器: App1 war 文件及其设置文件)==(部署在远程 VM 上)==>(Tomcat 服务器)
(Jenkins 服务器: 自动 select App1 war) ==(部署在远程虚拟机上)==>(Tomcat 服务器)
(Jenkins 服务器: App1 设置文件) ==(在远程虚拟机上部署设置)==>(Tomcat 服务器)
(Jenkins 服务器: 启动 App1 命令)==(发送命令到远程 VM)==>(Tomcat 服务器)
您是在问如何让 Jenkins 将您的应用程序及其配置部署到您的 Tomcat 个实例吗?
我认为使用 Maven deploy
插件可能是物理部署文件的最佳选择。
当我需要将一个应用程序的多个 copies/versions 部署到多个 Tomcat 服务器时,我所做的是创建一个新目录 wars
来包含 .war 文件,并使用 Tomcat
Context Containers 配置各个应用程序实例。像这样:
$CATALINA_HOME/conf/Catalina/localhost/app1.xml
$CATALINA_HOME/conf/Catalina/localhost/app2.xml
$CATALINA_HOME/wars/app1-1.0.0.war
$CATALINA_HOME/wars/app1-1.0.1.war
$CATALINA_HOME/wars/app2-1.0.1.war
当您将 .war 文件部署到 ../webapps
时,Tomcat 将每个 .war 视为一个单独的应用程序。我想你可以为这些应用程序提供额外的配置,但我不记得如何。
但是,如果您使用上下文容器文件,多个应用程序实例可以使用相同的 .war 文件。您可以手头保留 .war 文件的多个修订版,以防您需要回滚到早期版本,或者想要保留 运行 旧版本。
您可以执行以下操作:
- 在 jenkins 中通过 ssh 配置 "target" 服务器。
- 以参数为输入;什么目标,什么 .war,什么 .settings 文件等等
- 通过 jenkins 将 .war 和 .settings 文件复制到 SCP/SSH 的目标服务器以及执行以下操作的脚本。
- 分解 .war(解压)
- 在 exploded 中复制所需的 .settings 文件 war
- 开始tomcat
我构建了 .war,使用 Jenkins 作为构建作业,下游作业用于在基于 tomcat 的环境中进行部署,其详细信息已作为构建参数获取。
Developer gives war files like app1##20171122.war and app2##20171122.war
您没有提到开发人员 "gives" 您是如何获得这些 war 文件的。如果它们是 "built",我建议创建一个作业来使用 ant/maven 等来构建这些 war,以便已知这些 war 所在的位置。
如果要提供它们,您可以将它们上传到任何集中位置(FTP 服务器)或通过某些人工制品(例如 JFrog/Nexu 并直接在 tomcat 服务器上下载。
我创建了以下内容来解决这个问题:
专家:
- 安装 Maven 并在 pom.xml
之后创建
- 创建了一个小 shell 脚本来解压缩 war,放入与特定环境相关的设置文件并再次创建 war 文件
詹金斯:
- 使用带有 "This project is parameterized" 选项的 Jenkins 文件系统列表参数插件。
- 从构建选项中使用 "Invoke top level Maven targets" 并使用以下 maven 命令
配置文件系统参数时请使用以下"WAR-NAME"
deploy -Papp1 -Papp2 -DwarFile=${WAR-NAME}
Pom.xml:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.app1.app</groupId>
<artifactId>app1</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<warFile>none</warFile>
</properties>
<profiles>
<profile>
<id>app1</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>regex-property</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>warFilename</name>
<value>${warFile}</value>
<regex>.war$</regex>
<replacement></replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id> unzip, put setting file and zip again </id>
<phase>deploy</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<commandlineArgs>${warFile}</commandlineArgs>
<executable>${basedir}/make_war.sh</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>deploy-app1-1</id>
<goals>
<goal>deploy-only</goal>
</goals>
<phase>deploy</phase>
<configuration>
<username>admin</username>
<password>adminpass</password>
<warFile>${basedir}/deploy/${warFile}</warFile>
<url>http://localhost:9090/manager/text</url>
<server>Tomcat</server>
<path>/app1</path>
<update>true</update>
<ignorePackaging>true</ignorePackaging>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>app2</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>deploy-app1-2</id>
<goals>
<goal>deploy-only</goal>
</goals>
<phase>deploy</phase>
<configuration>
<username>admin</username>
<password>adminpass</password>
<warFile>${basedir}/deploy/${warFile}</warFile>
<url>http://localhost:8080/manager/text</url>
<server>Tomcat</server>
<path>/app1</path>
<update>true</update>
<ignorePackaging>true</ignorePackaging>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
make_war.sh
#!/usr/bin/sh
if [ $# -lt 1 ]; then
echo 1>&2 "[=11=]: not enough arguments. Please pass the WAR file name !!!"
exit 2
elif [ $# -gt 1 ]; then
echo 1>&2 "[=11=]: too many arguments. Please only pass the WAR file name !!!"
exit 2
fi
war_file_name=
file_name=$(basename "$war_file_name")
extension="${war_file_name##*.}"
file_name="${file_name%.*}"
if [ $extension != "war" ]; then
echo 1>&2 "[=11=]: Invalid arguments. Please pass valid WAR file with version name !!!"
exit 2
fi
echo "Cleaning up the deploy folder ..."
/usr/bin/rm -rf ./deploy/*
/usr/bin/unzip $war_file_name -d ./deploy/$file_name
/usr/bin/cp -f Application.settings ./deploy/$file_name/config/Application.settings
/usr/local/java/latest/bin/jar -cvf ./deploy/$war_file_name -C ./deploy/$file_name .
仅 Tomcat WAR 使用 Jenkins 部署:
部署场景如下:
- 3 环境生产、预生产和鉴定
- 2 War 个文件,例如 app1 和 app2
- 2 Tomcat 个服务器,例如每个环境的 Server1 和 Server2(点 1)
War 个文件场景:
- 开发人员提供了 war 个文件,例如 app1##20171122.war 和 app2##20171122.war
- app1 和 app2 设置文件不是 war 的一部分。
- 每个环境(生产、预生产和鉴定)的设置文件不同
- 部署 app1 和 app2 后,我们将设置文件放在 webapps/app1##20171122/config/Application.settings 和 webapps/app2##20171122/config/Application.settings
最后 app1 和 app2 开始使用 tomcat 管理器
这是我想用 Jenkins 做的事情:
使用 Mavin 部署或任何其他工具:
注意:此处War文件大小超过450MB
Jenkin 项目 App1:
- 我们会将 app1##20171122.war 文件放在 Jenkins 服务器上
- 我们应该能够自动 select 用于部署的 war 文件以及 war 版本
- 我们应该能够在多个服务器(Server1 和 Server2)上部署 war 个文件
- 在 webapps/app1##20171122/config/Application.settings 下部署 app1 的设置文件
- 启动应用程序1
注意:应用程序尚未准备好从 webapps 外部读取配置
简而言之:
(Jenkins 服务器: App1 war 文件及其设置文件)==(部署在远程 VM 上)==>(Tomcat 服务器)
(Jenkins 服务器: 自动 select App1 war) ==(部署在远程虚拟机上)==>(Tomcat 服务器)
(Jenkins 服务器: App1 设置文件) ==(在远程虚拟机上部署设置)==>(Tomcat 服务器)
(Jenkins 服务器: 启动 App1 命令)==(发送命令到远程 VM)==>(Tomcat 服务器)
您是在问如何让 Jenkins 将您的应用程序及其配置部署到您的 Tomcat 个实例吗?
我认为使用 Maven deploy
插件可能是物理部署文件的最佳选择。
当我需要将一个应用程序的多个 copies/versions 部署到多个 Tomcat 服务器时,我所做的是创建一个新目录 wars
来包含 .war 文件,并使用 Tomcat
Context Containers 配置各个应用程序实例。像这样:
$CATALINA_HOME/conf/Catalina/localhost/app1.xml
$CATALINA_HOME/conf/Catalina/localhost/app2.xml
$CATALINA_HOME/wars/app1-1.0.0.war
$CATALINA_HOME/wars/app1-1.0.1.war
$CATALINA_HOME/wars/app2-1.0.1.war
当您将 .war 文件部署到 ../webapps
时,Tomcat 将每个 .war 视为一个单独的应用程序。我想你可以为这些应用程序提供额外的配置,但我不记得如何。
但是,如果您使用上下文容器文件,多个应用程序实例可以使用相同的 .war 文件。您可以手头保留 .war 文件的多个修订版,以防您需要回滚到早期版本,或者想要保留 运行 旧版本。
您可以执行以下操作:
- 在 jenkins 中通过 ssh 配置 "target" 服务器。
- 以参数为输入;什么目标,什么 .war,什么 .settings 文件等等
- 通过 jenkins 将 .war 和 .settings 文件复制到 SCP/SSH 的目标服务器以及执行以下操作的脚本。
- 分解 .war(解压)
- 在 exploded 中复制所需的 .settings 文件 war
- 开始tomcat
我构建了 .war,使用 Jenkins 作为构建作业,下游作业用于在基于 tomcat 的环境中进行部署,其详细信息已作为构建参数获取。
Developer gives war files like app1##20171122.war and app2##20171122.war
您没有提到开发人员 "gives" 您是如何获得这些 war 文件的。如果它们是 "built",我建议创建一个作业来使用 ant/maven 等来构建这些 war,以便已知这些 war 所在的位置。 如果要提供它们,您可以将它们上传到任何集中位置(FTP 服务器)或通过某些人工制品(例如 JFrog/Nexu 并直接在 tomcat 服务器上下载。
我创建了以下内容来解决这个问题:
专家:
- 安装 Maven 并在 pom.xml 之后创建
- 创建了一个小 shell 脚本来解压缩 war,放入与特定环境相关的设置文件并再次创建 war 文件
詹金斯:
- 使用带有 "This project is parameterized" 选项的 Jenkins 文件系统列表参数插件。
- 从构建选项中使用 "Invoke top level Maven targets" 并使用以下 maven 命令
配置文件系统参数时请使用以下"WAR-NAME"
deploy -Papp1 -Papp2 -DwarFile=${WAR-NAME}
Pom.xml:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.app1.app</groupId>
<artifactId>app1</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<warFile>none</warFile>
</properties>
<profiles>
<profile>
<id>app1</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>regex-property</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>warFilename</name>
<value>${warFile}</value>
<regex>.war$</regex>
<replacement></replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id> unzip, put setting file and zip again </id>
<phase>deploy</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<commandlineArgs>${warFile}</commandlineArgs>
<executable>${basedir}/make_war.sh</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>deploy-app1-1</id>
<goals>
<goal>deploy-only</goal>
</goals>
<phase>deploy</phase>
<configuration>
<username>admin</username>
<password>adminpass</password>
<warFile>${basedir}/deploy/${warFile}</warFile>
<url>http://localhost:9090/manager/text</url>
<server>Tomcat</server>
<path>/app1</path>
<update>true</update>
<ignorePackaging>true</ignorePackaging>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>app2</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>deploy-app1-2</id>
<goals>
<goal>deploy-only</goal>
</goals>
<phase>deploy</phase>
<configuration>
<username>admin</username>
<password>adminpass</password>
<warFile>${basedir}/deploy/${warFile}</warFile>
<url>http://localhost:8080/manager/text</url>
<server>Tomcat</server>
<path>/app1</path>
<update>true</update>
<ignorePackaging>true</ignorePackaging>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
make_war.sh
#!/usr/bin/sh
if [ $# -lt 1 ]; then
echo 1>&2 "[=11=]: not enough arguments. Please pass the WAR file name !!!"
exit 2
elif [ $# -gt 1 ]; then
echo 1>&2 "[=11=]: too many arguments. Please only pass the WAR file name !!!"
exit 2
fi
war_file_name=
file_name=$(basename "$war_file_name")
extension="${war_file_name##*.}"
file_name="${file_name%.*}"
if [ $extension != "war" ]; then
echo 1>&2 "[=11=]: Invalid arguments. Please pass valid WAR file with version name !!!"
exit 2
fi
echo "Cleaning up the deploy folder ..."
/usr/bin/rm -rf ./deploy/*
/usr/bin/unzip $war_file_name -d ./deploy/$file_name
/usr/bin/cp -f Application.settings ./deploy/$file_name/config/Application.settings
/usr/local/java/latest/bin/jar -cvf ./deploy/$war_file_name -C ./deploy/$file_name .