Try WSO2 Cloud for Free
Sign in

All docs This doc

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. To create a new job in Jenkins, open the Jenkins user interface and click Jenkins > New Item.

  2. Give an appropriate name (say HelloWebApp-1.0) as the Item name, select the Maven project option, and click OK.

    When you select Maven as the job type, Jenkins  automatically connects the required Maven plugins and reads configuration from your POM file. This reduces the work that you have to do manually to set up your Maven project.
  3. The project's configuration opens. Under GitBucket, click the GitHub project option, and give your GitHub repository URL.
     
  4. Under Source Code Management, click the Git option, and give your GitHub repository URL.
    Note that you are building the master branch of your GitHub repository in this example. Under Branches to build, you see */master. 

    Next, let's schedule Jenkins to poll GitHub every five minutes. 
  5. Go to the  Build Trigger section, click  Poll SCM and give "H/5 * * *" as your schedule.

    Tip

    Tip: Note that a schedule follows a syntax similar to Cron. A schedule is given as  <MINUTE> <HOUR> <DAY OF THE MONTH> <MONTH> <DAY OF THE WEEK>, and * accepts all valid values. You can read up more about the syntax by clicking the  Help  tip right next to the Schedule text area in the Jenkins user interface.


  6. In the Build section, configure Jenkins to build and deploy the source code using the Maven POM file that is in your master branch of your repository. 
    • Root POM: pom.xml
    • Goals and options: clean deploy -Dbuild.number=${BUILD_NUMBER} -Dappcloud.password=<App_Cloud_password> -Dappcloud.username=<App_Cloud_username@tenant_name>

      Tip

      Tip: When you give the Integration Cloud username, be sure to convert the @ to a dot. For example, if your usename is user1@wso2.com and your tenant name is WSO2, then Dappcloud.username=user1.wso2.com@wso2.

      Tip

      Tip: You cannot update the same version of an application that is already deployed in WSO2 Integration Cloud. Therefore, the project that Jenkins builds must be deployed as a new version of an existing application. To create a new version, we append the {BUILD_NUMBER} to the current version before it is deployed in the Integration Cloud. For example, if the application version is 1.0 and the build number is 53, then the new application version will be 1.0.53.


    You have now configured Jenkins to poll GitHub, and if there are code changes, to automatically build and deploy the code as instructed in the POM file.


    Expand
    titleClick here to read more about the configurations in the POM file...

    We added the following configurations inside the <build> element of the POM file. It contains the configuration that allows Maven to log in to the Integration Cloud and create a new application version using the built artifact.

    Code Block
    <build>
        <plugins>
            <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.2</version>
                    <executions>
                    <execution>
                        <id>login</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                                <executable>curl</executable>
                                <arguments>
                                    <argument>-v</argument>
                                    <argument>-k</argument>
                                    <argument>-c</argument>
                                    <argument>cookies</argument>
                                    <argument>-X</argument>
                                    <argument>POST</argument>
                                    <argument>-F</argument>
                                    <argument>action=login</argument>
                                    <argument>-F</argument>
                                    <argument>userName=${appcloud.username}</argument>
                                    <argument>-F</argument>
                                    <argument>password=${appcloud.password}</argument>
                                    <argument>https://apps.cloud.wso2.com/appmgt/site/blocks/user/login/ajax/login.jag</argument>
                                </arguments>
                        </configuration>
                    </execution>
     
                    <execution>
                        <id>create application</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                                <executable>curl</executable>
                                <arguments>
                                    <argument>-v</argument>
                                    <argument>-k</argument>
                                    <argument>-b</argument>
                                    <argument>cookies</argument>
                                    <argument>-X</argument>
                                    <argument>POST</argument>
                                    <argument>https://apps.cloud.wso2.com/appmgt/site/blocks/application/application.jag</argument>
                                    <argument>-F</argument>
                                    <argument>action=createApplication</argument>
                                    <argument>-F</argument>
                                    <argument>applicationName=${project.artifactId}</argument>
                                    <argument>-F</argument>
                                    <argument>applicationDescription=${project.artifactId}_${project.version}_${build.number}</argument>
                                    <argument>-F</argument>
                                    <argument>conSpec=3</argument>
                                    <argument>-F</argument>
                                    <argument>runtime=1</argument>
                                    <argument>-F</argument>
                                    <argument>appTypeName=war</argument>
                                    <argument>-F</argument>
                                    <argument>applicationRevision=${deploy.version}.${build.number}</argument>
                                    <argument>-F</argument>
                                    <argument>uploadedFileName=${project.artifactId}-${project.version}.war</argument>
                                    <argument>-F</argument>
                                    <argument>runtimeProperties=[]</argument>
                                    <argument>-F</argument>
                                    <argument>tags=[{"key":"BUILD_NUMBER","value":"${build.number}"}]</argument>
                                    <argument>-F</argument>
                                    <argument>fileupload=@${project.build.directory}/${project.artifactId}-${project.version}.war</argument>
                                    <argument>-F</argument>
                                    <argument>isFileAttached=true</argument>
                                    <argument>-F</argument>
                                    <argument>isNewVersion=true</argument>
                                    <argument>-F</argument>
                                    <argument>appCreationMethod=default</argument>
                                </arguments>
                        </configuration>
                    </execution>
                    </executions>
            </plugin>
            <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.7</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
            </plugin>
        </plugins>
    </build>

    To help you understand the above configuration, here's a description of the code blocks:

    ConfigurationDescription
    Code Block
    <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <configuration> <skip>true</skip</configuration>
    </plugin>

    Maven automatically deploys the artifacts that it builds in your local Nexus repository. We avoid this by adding <skip>true</skip> inside maven-deploy-plugin as we want the artifacts to be deployed in WSO2 Integration Cloud instead.


    Code Block
    <build>
      ...
      <artifactId>exec-maven-plugin</artifactId>
      ....
         <phase>deploy</phase>
    </build>
    • We use the Maven exec plugin to configure the login and application creation functions using CURL commands.
    • We set the <phase> of the execution to deploy. When we run the clean deploy command as done in step 6 above, both the login and application creation sections of the code will be run.
    Code Block
    <argument>userName=${appcloud.username}</argument>                         <argument>password=${appcloud.password}</argument>

    We pass the Integration Cloud's credentials inside the login configuration in order to get a session cookie for authentication. 

    It is not recommended to hard-code the credentials in the POM file as the file is publicly available in your GitHub repository. Therefore, we have given the credentials as variable here, and you set the actual username and password when running the clean deploy command as shown in step 6 above.

    Code Block
    <argument>applicationRevision=${deploy.version}.${build.number}</argument>
    ....
    <argument>tags=[{"key":"BUILD_NUMBER","value":"${build.number}"}]</argument>
    We cannot update the same version of an application that is already deployed in WSO2 Integration Cloud. Therefore, we create a new version with the Jenkins {build.number}
    appended to the version of the original application (i.e., 1.0.0). For example, if the build number is 53, then the new application version will be 1.0.53.
    We then Jenkins build number is assigned to the key BUILD_NUMBER, which is used in the command in step 6 above.
    Code Block
    languagexml
    <argument>isNewVersion=true</argument>
    We set isNewVersion to true to force an application to be deployed as a new version of an existing application in the Integration Cloud.

    Next, let's edit the code in your GitHub repository and push the changes to the master branch. The next time Jenkins polls GitHub, it will detect that a code change has happened, trigger a build, and deploy the built artifact as a new version of the HelloWebApp application that you created in WSO2 Integration Cloud.

  7. Navigate to a suitable location on your machine using the command-line or terminal and take a local copy of the repository using the following command. Let's call this local copy <SAMPLE_HOME>.

    Code Block
    git clone <your_repository_URL>
  8. Let's do a small tweak to the code. Open the <SAMPLE_HOME>/src/main/webapp/index.jsp file and change the <h2> heading as follows:

    Code Block
    <html>
       <body>
          <h2>Hello World Version 2</h2>
          <h5>By WSO2 Integration Cloud Team</h5>
    </body>
    </html>
  9. Navigate to  <SAMPLE_HOME> using the command-line or terminal and execute the following command to push the code changes to the repository. You will be asked to log in to the repository.

    Code Block
    git add .
    git commit -am "give any commit message"
    git push origin master

    You have pushed code changes to the GitHub repository. 

  10. Periodically check the Integration Cloud for a new version of the  HelloWebApp application. You can do this by expanding the version drop-down list.
    Image Removed 
  11. Select the new version from the drop-down list and click on its URL to launch the application.
    Image Removed
    The application's homepage opens. Click  Launch App  to run the application that you just created, and note  
  12. Note that the changes you did to the code are visible in the heading.

...