Try WSO2 Cloud for Free
Sign in

All docs This doc
Skip to end of metadata
Go to start of metadata

Building and deploying are key aspects of software development. Automating these two tasks is referred to as continuous integration (CI) and continuous deployment (CD)Continuous integration is the practice of automatically testing the changes that you do to a program's source code, and continuous deployment is the practice of automatically pushing the changes to a staging or production server. CI and CD improve the productivity of developers by allowing them to automate repetitive tasks such as building, committing, deploying, and testing. 

In this tutorial, you integrate Jenkins (the CI server) with GitHub (repository) so that Jenkins polls the repository in GitHub periodically and triggers a new build if there are any code changes. After the build, a new version of an existing application is created in WSO2 Integration Cloud using the built artifacts.

Before you begin,

Let's get started!

  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: 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: 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: 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.


     Click 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.

    <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
    <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.


    <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.
    <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.

    <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.
    <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>.

    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:

    <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.

    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. 
  11. Select the new version from the drop-down list and click on its URL to launch the application. 
  12. Note that the changes you did to the code are visible in the heading.

You have integrated Jenkins with GitHub to build application artifacts and deploy them as new versions of an existing application that is deployed in the Integration Cloud.

What's next?

See Test your Applications Locally before Deploying in the Cloud or see Integration Solutions.

  • No labels