通过同一Jenkins Job在多台服务器上执行应用程序部署

时间:2019-03-18 20:50:21

标签: maven jenkins deployment continuous-integration

我正在使用Jenkins CI自动执行Java应用程序的部署,并且目前正在pom.xml文件中使用以下构建配置:

<!-- teste1 config -->
<deploy.jboss.teste1.host>192.168.0.1</deploy.jboss.teste1.host>
<deploy.jboss.teste1.port>9999</deploy.jboss.teste1.port>
<deploy.jboss.teste1.user>admin</deploy.jboss.teste1.user>
<deploy.jboss.teste1.password>admin</deploy.jboss.teste1.password>

<liquibase.teste1.database>db_01</liquibase.teste1.database>
<liquibase.teste1.host>192.168.0.2</liquibase.teste1.host>
<liquibase.teste1.user>admin</liquibase.teste1.user>
<liquibase.teste1.password>admin</liquibase.teste1.password>

<!-- teste2 config -->
<deploy.jboss.teste2.host>192.168.0.3</deploy.jboss.teste2.host>
<deploy.jboss.teste2.port>9999</deploy.jboss.teste2.port>
<deploy.jboss.teste2.user>admin</deploy.jboss.teste2.user>
<deploy.jboss.teste2.password>admin</deploy.jboss.teste2.password>

<liquibase.teste2.database>db_02</liquibase.teste2.database>
<liquibase.teste2.host>192.168.0.4</liquibase.teste2.host>
<liquibase.teste2.user>admin</liquibase.teste2.user>
<liquibase.teste2.password>admin</liquibase.teste2.password>

<profile>
    <id>teste1</id>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.jboss.as.plugins</groupId>
                <artifactId>jboss-as-maven-plugin</artifactId>
                <version>7.7.Final</version>
                <configuration>
                    <hostname>${deploy.jboss.teste1.host}</hostname>
                    <port>${deploy.jboss.teste1.port}</port>
                    <username>${deploy.jboss.teste1.user}</username>
                    <password>${deploy.jboss.teste1.password}</password>
                    <name>${backend.deployment-name}</name>
                    <filename>${project.build.finalName}.war</filename>
                    <skip>${skipDeployment}</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>deploy-jar</id>
                        <phase>install</phase>
                        <goals>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>3.4.1</version>

                <configuration>
                    <changeLogFile>${liquibase.changelog.file}</changeLogFile>
                    <diffChangeLogFile>${liquibase.changelog.file}</diffChangeLogFile>
                    <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                    <driver>com.mysql.jdbc.Driver</driver>
                    <url>jdbc:mysql://${liquibase.teste1.host}:3306/${liquibase.teste1.database}?zeroDateTimeBehavior=convertToNull</url>
                    <username>${liquibase.teste1.user}</username>
                    <password>${liquibase.teste1.password}</password>
                </configuration>

                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.34</version>
                    </dependency>
                </dependencies>

                <executions>
                    <execution>
                        <id>update</id>
                        <phase>deploy</phase>
                       <goals>
                         <goal>update</goal>
                       </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>
<profile>
    <id>teste2</id>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.jboss.as.plugins</groupId>
                <artifactId>jboss-as-maven-plugin</artifactId>
                <version>7.7.Final</version>
                <configuration>
                    <hostname>${deploy.jboss.teste2.host}</hostname>
                    <port>${deploy.jboss.teste2.port}</port>
                    <username>${deploy.jboss.teste2.user}</username>
                    <password>${deploy.jboss.teste2.password}</password>
                    <name>${backend.deployment-name}</name>
                    <filename>${project.build.finalName}.war</filename>
                    <skip>${skipDeployment}</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>deploy-jar</id>
                        <phase>install</phase>
                        <goals>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>3.4.1</version>

                <configuration>
                    <changeLogFile>${liquibase.changelog.file}</changeLogFile>
                    <diffChangeLogFile>${liquibase.changelog.file}</diffChangeLogFile>
                    <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                    <driver>com.mysql.jdbc.Driver</driver>
                    <url>jdbc:mysql://${liquibase.teste2.host}:3306/${liquibase.teste2.database}?zeroDateTimeBehavior=convertToNull</url>
                    <username>${liquibase.teste2.user}</username>
                    <password>${liquibase.teste2.password}</password>
                </configuration>

                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.34</version>
                    </dependency>
                </dependencies>

                <executions>
                    <execution>
                        <id>update</id>
                        <phase>deploy</phase>
                       <goals>
                         <goal>update</goal>
                       </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

然后在Jenkins中创建了两个Job,每个配置文件一个,在每个Job中分别执行以下maven命令:

clean package jboss-as:deploy javadoc:javadoc liquibase:update -P teste1
clean package jboss-as:deploy javadoc:javadoc liquibase:update -P teste2

但是我想使用相同的Job在两个服务器上同时部署应用程序,但是到目前为止,我还无法弄清执行此任务的方法,我已经尝试了以下命令,但是没有成功:

clean package jboss-as:deploy javadoc:javadoc liquibase:update -P teste1,teste2

clean package jboss-as:deploy javadoc:javadoc liquibase:update -P teste1 -P teste2

clean package jboss-as:deploy javadoc:javadoc liquibase:update -P teste1;clean package jboss-as:deploy javadoc:javadoc liquibase:update -P teste2

在所有情况下,部署仅在一台服务器中完成。欢迎任何帮助。谢谢。

编辑:

enter image description here

1 个答案:

答案 0 :(得分:0)

要完成任务,我希望最终使用“发布步骤”说明,如下图所示。它不是最优雅的解决方案,但可以正常工作。如果有人有更好的解决方案,请分享。

Post Steps