具有WildFly,Arquillian,Jenkins和OpenShift的Java EE 7部署管道

技术提示#54展示了如何Arquillianate(Arquillianize?)一个现有的Java EE项目并在WildFly在已知主机和端口上运行的远程模式下运行这些测试。 技术提示#55展示了当WildFly在OpenShift中运行时如何运行这些测试。 这两个技巧都使用Maven配置文件来分隔“ pom.xml”中的适当Arquillian依赖关系和“ arquillian.xml”中的<container>配置,以定义WildFy在何处运行以及如何连接。

本技巧将展示如何在OpenShift中配置Jenkins以及如何从Jenkins调用这些测试。 让我们首先看看它的作用!


从OpenShift上的Jenkins连接到OpenShift上的WildFly实例所需的配置与从本地计算机到OpenShift上的WildFly连接所需的配置类似。 此配置在“ arquillian.xml”中指定,我们可以指定一些参数,然后可以在Jenkins中定义这些参数。

概括地说,这是我们要做的:

  • 使用在技术提示#54#55中创建的代码,并为Arquillian / Jenkins / OpenShift添加配置
  • 启用詹金斯
  • 创建一个新的WildFly Test实例
  • 配置Jenkins在测试实例上运行测试
  • 仅当测试通过测试实例时,才将应用程序推入生产环境

让我们开始吧!

  1. Tech Tip#55中创建的WildFly git repo中删除现有的样板源代码,仅删除src目录。
    mywildfly> git rm -rf src/ pom.xml
    rm 'pom.xml'
    rm 'src/main/java/.gitkeep'
    rm 'src/main/resources/.gitkeep'
    rm 'src/main/webapp/WEB-INF/web.xml'
    rm 'src/main/webapp/images/jbosscorp_logo.png'
    rm 'src/main/webapp/index.html'
    rm 'src/main/webapp/snoop.jsp'
    mywildfly> git commit . -m"removing source and pom"
    [master 564b275] removing source and pom
     7 files changed, 647 deletions(-)
     delete mode 100644 pom.xml
     delete mode 100644 src/main/java/.gitkeep
     delete mode 100644 src/main/resources/.gitkeep
     delete mode 100644 src/main/webapp/WEB-INF/web.xml
     delete mode 100644 src/main/webapp/images/jbosscorp_logo.png
     delete mode 100644 src/main/webapp/index.html
     delete mode 100644 src/main/webapp/snoop.jsp
  2. 将新的远程设置为javaee7-continuous-delivery存储库:
    mywildfly> git remote add javaee7 https://github.com/arun-gupta/javaee7-continuous-delivery.git
    mywildfly> git remote -v
    javaee7	https://github.com/arun-gupta/javaee7-continuous-delivery.git (fetch)
    javaee7	https://github.com/arun-gupta/javaee7-continuous-delivery.git (push)
    origin	ssh://54699516ecb8d41cb8000016@mywildfly-milestogo.rhcloud.com/~/git/mywildfly.git/ (fetch)
    origin	ssh://54699516ecb8d41cb8000016@mywildfly-milestogo.rhcloud.com/~/git/mywildfly.git/ (push)
  3. 从新的遥控器中提取代码:
    mywildfly> git pull javaee7 master
    warning: no common commits
    remote: Counting objects: 62, done.
    remote: Compressing objects: 100% (45/45), done.
    remote: Total 62 (delta 14), reused 53 (delta 5)
    Unpacking objects: 100% (62/62), done.
    From https://github.com/arun-gupta/javaee7-continuous-delivery
     * branch            master     -> FETCH_HEAD
     * [new branch]      master     -> javaee7/master
    Merge made by the 'recursive' strategy.
     .gitignore                                           |   6 +++
     README.asciidoc                                      |  15 ++++++
     pom.xml                                              | 197 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     src/main/java/org/javaee7/sample/MyApplication.java  |   9 ++++
     src/main/java/org/javaee7/sample/Person.java         |  31 ++++++++++++
     src/main/java/org/javaee7/sample/PersonDatabase.java |  39 ++++++++++++++
     src/main/java/org/javaee7/sample/PersonResource.java |  29 +++++++++++
     src/main/webapp/index.jsp                            |  13 +++++
     src/test/java/org/javaee7/sample/PersonTest.java     |  77 ++++++++++++++++++++++++++++
     src/test/resources/arquillian.xml                    |  26 ++++++++++
     10 files changed, 442 insertions(+)
     create mode 100644 .gitignore
     create mode 100644 README.asciidoc
     create mode 100644 pom.xml
     create mode 100644 src/main/java/org/javaee7/sample/MyApplication.java
     create mode 100644 src/main/java/org/javaee7/sample/Person.java
     create mode 100644 src/main/java/org/javaee7/sample/PersonDatabase.java
     create mode 100644 src/main/java/org/javaee7/sample/PersonResource.java
     create mode 100644 src/main/webapp/index.jsp
     create mode 100644 src/test/java/org/javaee7/sample/PersonTest.java
     create mode 100644 src/test/resources/arquillian.xml

    这将带来所有源代码,包括我们的REST端点,网页,测试,更新的“ pom.xml”和“ arquillian.xml”。 更新的“ pom.xml”具有两个新的配置文件。

    
         
         
     
          
          
           
           openshift
          
          
     
          
          
        
           
           
          
            
            
                
             
             
               org.apache.maven.plugins 
             
                
             
             
               maven-war-plugin 
             
                
             
             
               2.3 
             
                
             
              
              
                false 
               
              
                deployments 
               
              
                ROOT 
               
             
            
            
            
        
           
           
      
          
          
    
         
         
    
         
         
        
          
          
           
           jenkins-openshift
          
          
        
          
          
            
           
           
                
            
            
                    
             
             
               maven-surefire-plugin 
             
                    
             
             
               2.14.1 
             
                    
             
              
               
               
                 jenkins-openshift 
                
               
             
                
            
            
            
           
           
        
          
          
        
          
          
            
           
           
                
            
            
             
             org.jboss.arquillian.container
            
            
                
            
            
             
             arquillian-openshift
            
            
                
            
            
             
             1.0.0.Final-SNAPSHOT
            
            
                
            
            
             
             test
            
            
            
           
           
        
          
          
    
         
         

    这里要注意的几点:

    1. 在OpenShift上构建应用程序时使用“ openshift”配置文件。 这是创建应用程序的WAR文件并将其部署到WildFly的地方。
    2. 添加了新的配置文件“ jenkins-openshift”,供OpenShift中的Jenkins实例(即将启用)使用它来运行测试。
    3. “ arquillian-openshift”依赖项与技术提示#55中使用的依赖项相同,并且允许在OpenShift的WildFly实例上运行Arquillian测试。
    4. 此配置文件引用将在“ arquillian.xml”中定义的“ jenkins-openshift”容器配置。

    更新的“ src / test / resources / arquillian.xml”具有以下容器:

    <container qualifier="jenkins-openshift">
        <configuration>
            <property name="namespace">${env.ARQ_DOMAIN}</property>
            <property name="application">${env.ARQ_APPLICATION}</property>
            <property name="libraDomain">rhcloud.com</property>
            <property name="sshUserName">${env.ARQ_SSH_USER_NAME}</property>
            <property name="login">arungupta@redhat.com</property>
            <property name="deploymentTimeoutInSeconds">300</property>
            <property name="disableStrictHostChecking">true</property> 
        </configuration>
    </container>

    此容器配置类似于技术提示#55中添加的容器配置。 唯一的区别是域名,应用程序名称和SSH用户名已参数化。 这些属性的值在Jenkins实例的配置中定义,并允许针对单独的测试节点运行测试。

  4. 在将更改推送到远程存储库之前,还需要完成两件事。 首先是创建一个WildFly Test实例,该实例可用于运行测试。 可以很容易地完成,如下所示:
    workspaces> rhc app-create mywildflytest jboss-wildfly-8
    Application Options
    -------------------
    Domain:     milestogo
    Cartridges: jboss-wildfly-8
    Gear Size:  default
    Scaling:    no
    
    Creating application 'mywildflytest' ... Artifacts deployed: ./ROOT.war
    done
    
      WildFly 8 administrator added.  Please make note of these credentials:
    
       Username: adminITJt7Yh
       Password: yXP2mUd1w4_8
       
       run 'rhc port-forward mywildflytest' to access the web admin area on port 9990.
    
    Waiting for your DNS name to be available ... done
    
    Cloning into 'mywildflytest'...
    Warning: Permanently added the RSA host key for IP address '54.205.69.88' to the list of known hosts.
    
    Your application 'mywildflytest' is now available.
    
      URL:        http://mywildflytest-milestogo.rhcloud.com/
      SSH to:     546e3743ecb8d49ca9000014@mywildflytest-milestogo.rhcloud.com
      Git remote: ssh://546e3743ecb8d49ca9000014@mywildflytest-milestogo.rhcloud.com/~/git/mywildflytest.git/
      Cloned to:  /Users/arungupta/workspaces/javaee7/mywildflytest
    
    Run 'rhc show-app mywildflytest' for more details about your app.

    注意这里的域是milestogo ,应用程序名称是mywildflytest ,SSH用户名称是546e3743ecb8d49ca9000014 。 这些将传递给Arquillian进行测试。

  5. 其次是启用和配置Jenkins。在您的OpenShift控制台中 ,选择“ mywildfly”应用程序,然后单击“ Enable Jenkins”链接,如下所示:
    techtip56-enable-jenkins 请记住,这不是您的Test实例,因为所有源代码都位于先前创建的实例上。提供适当的名称,例如jenkins-milestogo.rhcloud.com(在我的情况下),然后单击“添加Jenkins”按钮。 这将提供一个Jenkins实例(如果尚未存在的话),并使用脚本配置该项目以构建和部署该应用程序。 记下名称和密码凭据。
  6. 使用凭据登录到您的Jenkins实例。在这种情况下,选择适当的版本“ mywildfly-build”。 向下滚动到“ Build”部分,然后在Execute Shell中的“#Run tests here”之后添加以下脚本:
    export ARQ_DOMAIN=milestogo
    export ARQ_SSH_USER_NAME=546e3743ecb8d49ca9000014
    export ARQ_APPLICATION=mywildflytest
    mvn test -Pjenkins-openshift

    单击“保存”以保存配置。 这将允许在Test实例上运行Arquillian测试。 如果测试通过,则将部署该应用程序。 如果测试失败,则该步骤之后的所有步骤均不会执行,因此不会部署该应用程序。

  7. 现在让我们将更改推送到远程仓库:
    mywildfly> git push
    Counting objects: 68, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (49/49), done.
    Writing objects: 100% (61/61), 8.85 KiB | 0 bytes/s, done.
    Total 61 (delta 14), reused 0 (delta 0)
    remote: Executing Jenkins build.
    remote: 
    remote: You can track your build at https://jenkins-milestogo.rhcloud.com/job/mywildfly-build
    remote: 
    remote: Waiting for build to schedule............................................................................................Done
    remote: Waiting for job to complete................................................................................................................................................................................................................................................................................................................................................................................................Done
    remote: SUCCESS
    remote: New build has been deployed.
    remote: -------------------------
    remote: Git Post-Receive Result: success
    remote: Deployment completed with status: success
    To ssh://546cef93ecb8d4ff37000003@mywildfly-milestogo.rhcloud.com/~/git/mywildfly.git/
       e8f6c61..e9ad206  master -> master

    点的数量表示等待特定任务,并且很可能会因运行不同而有所不同。 而Jenkins控制台( jenkins-milestogo.rhcloud.com/job/mywildfly-build/1/console )将输出显示为:

    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running org.javaee7.sample.PersonTest
    Nov 20, 2014 2:54:56 PM org.jboss.arquillian.container.openshift.OpenShiftContainer start
    INFO: Preparing Arquillian OpenShift container at http://mywildflytest-milestogo.rhcloud.com
    Nov 20, 2014 2:55:48 PM org.jboss.arquillian.container.openshift.OpenShiftRepository push
    INFO: Pushed to the remote repository ssh://546e3743ecb8d49ca9000014@mywildflytest-milestogo.rhcloud.com/~/git/mywildflytest.git/
    Nov 20, 2014 2:56:37 PM org.jboss.arquillian.container.openshift.OpenShiftRepository push
    INFO: Pushed to the remote repository ssh://546e3743ecb8d49ca9000014@mywildflytest-milestogo.rhcloud.com/~/git/mywildflytest.git/
    Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 103.056 sec
    Nov 20, 2014 2:56:37 PM org.jboss.arquillian.container.openshift.OpenShiftContainer stop
    INFO: Shutting down Arquillian OpenShift container at http://mywildflytest-milestogo.rhcloud.com
    Results :
    
    Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 3:13.069s
    [INFO] Finished at: Thu Nov 20 14:57:34 EST 2014
    [INFO] Final Memory: 10M/101M
    [INFO] ------------------------------------------------------------------------
    + /usr/libexec/openshift/cartridges/jenkins/bin/git_ssh_wrapper.sh 546e36e5e0b8cd4e2a000007@mywildfly-milestogo.rhcloud.com 'gear stop --conditional'
    Warning: Permanently added 'mywildfly-milestogo.rhcloud.com,10.5.171.43' (RSA) to the list of known hosts.
    Stopping gear...
    Stopping wildfly cart
    Sending SIGTERM to wildfly:418673 ...
    + rsync --delete-after -azO -e /usr/libexec/openshift/cartridges/jenkins/bin/git_ssh_wrapper.sh /var/lib/openshift/546e46304382ec3f29000012//.m2/ '546e36e5e0b8cd4e2a000007@mywildfly-milestogo.rhcloud.com:~/.m2/'
    Warning: Permanently added 'mywildfly-milestogo.rhcloud.com,10.5.171.43' (RSA) to the list of known hosts.
    + rsync --delete-after -azO -e /usr/libexec/openshift/cartridges/jenkins/bin/git_ssh_wrapper.sh /var/lib/openshift/546e46304382ec3f29000012/app-root/runtime/repo/deployments/ '546e36e5e0b8cd4e2a000007@mywildfly-milestogo.rhcloud.com:${OPENSHIFT_REPO_DIR}deployments/'
    Warning: Permanently added 'mywildfly-milestogo.rhcloud.com,10.5.171.43' (RSA) to the list of known hosts.
    + rsync --delete-after -azO -e /usr/libexec/openshift/cartridges/jenkins/bin/git_ssh_wrapper.sh /var/lib/openshift/546e46304382ec3f29000012/app-root/runtime/repo/.openshift/ '546e36e5e0b8cd4e2a000007@mywildfly-milestogo.rhcloud.com:${OPENSHIFT_REPO_DIR}.openshift/'
    Warning: Permanently added 'mywildfly-milestogo.rhcloud.com,10.5.171.43' (RSA) to the list of known hosts.
    + /usr/libexec/openshift/cartridges/jenkins/bin/git_ssh_wrapper.sh 546e36e5e0b8cd4e2a000007@mywildfly-milestogo.rhcloud.com 'gear remotedeploy'
    Warning: Permanently added 'mywildfly-milestogo.rhcloud.com,10.5.171.43' (RSA) to the list of known hosts.
    Preparing build for deployment
    Deployment id is dff28e58
    Activating deployment
    Deploying WildFly
    Starting wildfly cart
    Found 127.12.255.129:8080 listening port
    Found 127.12.255.129:9990 listening port
    /var/lib/openshift/546e36e5e0b8cd4e2a000007/wildfly/standalone/deployments /var/lib/openshift/546e36e5e0b8cd4e2a000007/wildfly
    /var/lib/openshift/546e36e5e0b8cd4e2a000007/wildfly
    CLIENT_MESSAGE: Artifacts deployed: ./ROOT.war
    Archiving artifacts
    Finished: SUCCESS

    可以查看Jenkins的日志文件,如下所示:

    Nov 20, 2014 2:51:11 PM hudson.plugins.openshift.OpenShiftCloud provision
    INFO: Provisioning new node for workload = 2 and label = mywildfly-build in domain milestogo
    Nov 20, 2014 2:51:11 PM hudson.plugins.openshift.OpenShiftCloud getOpenShiftConnection
    INFO: Initiating Java Client Service - Configured for OpenShift Server https://openshift.redhat.com
    Nov 20, 2014 2:51:11 PM com.openshift.internal.client.RestService request
    INFO: Requesting GET with protocol 1.2 on https://openshift.redhat.com/broker/rest/api
    Nov 20, 2014 2:51:11 PM com.openshift.internal.client.RestService request
    INFO: Requesting GET with protocol 1.2 on https://openshift.redhat.com/broker/rest/user
    Nov 20, 2014 2:51:11 PM com.openshift.internal.client.RestService request
    
    . . .
    
    INFO: Checking availability of computer hudson.plugins.openshift.OpenShiftSlave@8ce21115
    Nov 20, 2014 2:53:35 PM com.openshift.internal.client.RestService request
    INFO: Requesting GET with protocol 1.2 on https://openshift.redhat.com/broker/rest/domain/milestogo/application/mywildflybldr/gear_groups
    Nov 20, 2014 2:53:35 PM hudson.plugins.openshift.OpenShiftComputerLauncher launch
    INFO: Checking SSH access to application mywildflybldr-milestogo.rhcloud.com
    Nov 20, 2014 2:53:35 PM hudson.plugins.openshift.OpenShiftComputerLauncher launch
    INFO: Connecting via SSH '546e46304382ec3f29000012' 'mywildflybldr-milestogo.rhcloud.com' '/var/lib/openshift/546e393e5973ca0492000070/app-root/data/.ssh/jenkins_id_rsa'
    Nov 20, 2014 2:53:35 PM hudson.slaves.NodeProvisioner update
    INFO: mywildfly-build provisioningE successfully completed. We have now 2 computer(s)
    Nov 20, 2014 2:53:35 PM hudson.plugins.openshift.OpenShiftComputerLauncher launch
    INFO: Connected via SSH.
    Nov 20, 2014 2:53:35 PM hudson.plugins.openshift.OpenShiftComputerLauncher launch
    INFO: Exec mkdir -p $OPENSHIFT_DATA_DIR/jenkins && cd $OPENSHIFT_DATA_DIR/jenkins && rm -f slave.jar && wget -q --no-check-certificate https://jenkins-milestogo.rhcloud.com/jnlpJars/slave.jar
    Nov 20, 2014 2:53:42 PM hudson.plugins.openshift.OpenShiftComputerLauncher launch
    INFO: Slave connected.
    Nov 20, 2014 2:58:24 PM hudson.model.Run execute
    INFO: mywildfly-build #1 main build action completed: SUCCESS

    这表明该应用程序已成功部署在mywildfly-milestogo.rhcloud.com/index.jsp上 ,如下所示:
    techtip56-mywildfly输出测试通过

现在更改“ src / main / webapp / index.jsp”以显示不同的标题。 并更改“ src / test / java / org / javaee7 / sample / PersonTest.java”以使其中一项测试失败。 进行“ git commit”和“ git push”会在命令行上显示以下结果:

mywildfly> git commit . -m"breaking the test"
[master ff2de09] breaking the test
 2 files changed, 2 insertions(+), 2 deletions(-)
mywildfly> git push
Counting objects: 23, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (12/12), 771 bytes | 0 bytes/s, done.
Total 12 (delta 5), reused 0 (delta 0)
remote: Executing Jenkins build.
remote: 
remote: You can track your build at https://jenkins-milestogo.rhcloud.com/job/mywildfly-build
remote: 
remote: Waiting for build to schedule.......Done
remote: Waiting for job to complete.....................................................................................................................................................................Done
remote: FAILED
remote: !!!!!!!!
remote: Deployment Halted!
remote: If the build failed before the deploy step, your previous
remote: build is still running.  Otherwise, your application may be
remote: partially deployed or inaccessible.
remote: Fix the build and try again.
remote: !!!!!!!!
remote: An error occurred executing 'gear postreceive' (exit code: 1)
remote: Error message: CLIENT_ERROR: Failed to execute: 'control post-receive' for /var/lib/openshift/546e36e5e0b8cd4e2a000007/jenkins-client
remote: 
remote: For more details about the problem, try running the command again with the '--trace' option.
To ssh://546e36e5e0b8cd4e2a000007@mywildfly-milestogo.rhcloud.com/~/git/mywildfly.git/
   d618fad..ff2de09  master -> master

要注意的关键声明是,测试失败后,将停止部署。 您可以通过重新访问mywildfly-milestogo.rhcloud.com/index.jsp并检查更新后的“ index.jsp”是否不可见来验证这一点。

简而言之,测试通过,网站已更新。 测试失败,该网站未更新。 因此,您已经使用WildFly,OpenShift,Arquillian和Jenkins为Java EE 7建立了一个简单的部署管道!

翻译自: https://www.javacodegeeks.com/2014/12/deployment-pipeline-for-java-ee-7-with-wildfly-arquillian-jenkins-and-openshift.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值