Nav

Lab 4: CI/CD Process - Automate Deployment

Overview

In this lab, we will learn how to integrate the deployment with a continous integration and deployment tool. We are going to use Git as a version control system and Jenkins as the continuous integration tool.

We are going to work with the OmniChannel API Implementation.

The completion of this lab will consist of a few steps:

  1. Creating a Jenkinsfile and initializing a Git repo

  2. Adding the Mule Maven Plugin configuration to the Mule pom.xml file.

  3. Creating a Pipeline Jenkins job

  4. Creating a post-commit file to automate a Jenkins job process after each git commit

To complete this workshop you need a Jenkins server and git gui. If you run the Lab in the EC2 instance, there is a Jenkins server installed. If you are running in your own machine, you can install Jenkins from here

Be sure to have the following plugins:

  • Blue Ocean

  • Build Pipeline plugin

  • Pipeline

This lab use git as the version controller. If you don’t have git installed on your mahcine, you can get it from here

Step 1: Setup Jenkins and initialize a Git repo

To get started we need to start up the Jenkins server and create a Git repository. In the following lab we will be using 'Git-Gui' to manage our git operations.

  1. Navigate to the Desktop of the VM. Open up the Jenkins server application by clicking the icon that looks like the following:

    module7 lab 4 jenkins logo
  2. Wait until you see a black command prompt box appear titled Jenkins Service. It will look as the following:

    module7 lab 4 cmd

    The Jenkins service will take several minutes to start, allow the process to run in the background in parallel while we complete the rest of the lab.

  3. Navigate to the Desktop of the VM. Open up the Git Gui application by clicking the icon that looks like the following:

    module7 lab 4 git gui
  4. Select Create New Repository. Insert the location of your project into the Directory field. If you followed the steps in Lab 1 you may use the following path. Note that if you are using your own project the name of the project may differ:

    C:/Users/workshop/AnypointStudio/studio-workspace/omni-channel-api
  5. Select the Create button. Your Mule project will be initialized within Git and you will be taken to the Git Gui management console.

  6. Review the unstaged commits then select the Stage Changed button.

    module7 lab 4 gitgui stagechanged
    As a good practice we only commit the files needed to be able to successfully make the build. The compiled classes .class files should not be under version control. You could create .gitignore file to let git ignore those files.
  7. Click Yes if given an additional prompt to stage the untracked files.

  8. You will now see the changes moved from Unstaged Changes to Staged Changes. Enter your commit message to be:

    Init Git repository
  9. Click the Commit button to commit your project.

    module7 lab 4 gitgui commit

    Now that you have initialized your Git repository, we are going to configure the Mule Maven Plugin.

Step 2: Configure the Mule Maven plugin

We saw in lab 1 that we can deploy using Studio. We can also export as a Jar file and deploy an application from the Anypoint Platform UI.

A third option is to use the Mule Maven Plugin. To do that we are going to update the file pom.xml

  1. Navigate back to the Package Explorer located on the right-side of your Anypoint Studio IDE. Open up the pom.xml file located in the root directory of the project.

  2. Move the cursor inside the <plugin> element, child element <configuration>. You will add a configuration for your CloudHub deployment inside the <configuration> element at the next step

  3. Copy & paste the following xml code on this new line within your <configuration> element:

    <cloudHubDeployment>
        <uri>https://anypoint.mulesoft.com</uri>
        <muleVersion>${mule.version}</muleVersion>
        <!-- Deploy User Parameter -->
        <username>${anypoint.username}</username>
        <password>${anypoint.password}</password>
        <!-- Environment Parameter -->
        <environment>${cloudhub.environment}</environment>
        <applicationName>${cloudhub.app}</applicationName>
        <businessGroup>${cloudhub.bg}</businessGroup>
        <workerType>${cloudhub.worker}</workerType>
    </cloudHubDeployment>
    You can reformat the XML code to look nicer by right-clicking anywhere on the pom.xml editor canvas and selecting Source > Format.

    You can see a lot of parameters.

    • muleVersion: it’s the Mule Runtime version.

    • username and password: These are the credentials to deploy into the platform.

    • environment: This is the environment were is going to be deployed (Sandbox, Production)

    • applicationName: Name of the Mule application.

    • businessGroup: Name of the business group where is going to be deployed.

    • workerType: Size of the worker where is going to by deployed.

      More info can be found here

      These parameters are going to be set later when we run the Maven command.

We have just added a configuration to deploy it to Cloudhub. In the next step we are going to configure the Jenkins pipeline.

Step 3: Create a Pipeline

Pipelines provides a number of immediate benefits:

  • Code review/iteration on the Pipeline

  • Audit trail for the Pipeline

  • Single source of truth for the Pipeline, which can be viewed and edited by multiple members of the project.

    1. On the left side of the window, you will find the Package Explorer. Right click on the root of your project and select New > File.

      module7 lab 4 createfile
    2. Name the file, Jenkinsfile. Make sure the new file has been created in the root level of the project.

      module7 lab4 jenkinsfile created
    3. Open the new Jenkinsfile. Copy & paste the following script into your Jenkinsfile:

      pipeline {
      
        agent any
        environment {
          //adding a comment for the commit test
          DEPLOY_CREDS = credentials('deploy-anypoint-user')
          MULE_VERSION = '4.1.4'
          BG = "<BUSINESS-GROUP>"
          WORKER = "Micro"
        }
        stages {
          stage('Build') {
            steps {
                  bat 'mvn -B -U -e -V clean -DskipTests package'
            }
          }
      
          stage('Test') {
            steps {
                bat "mvn test"
            }
          }
      
           stage('Deploy Development') {
            environment {
              ENVIRONMENT = 'Sandbox'
              APP_NAME = '<DEV-API-NAME>'
            }
            steps {
                  bat 'mvn -U -V -e -B -DskipTests deploy -DmuleDeploy -Dmule.version="%MULE_VERSION%" -Danypoint.username="%DEPLOY_CREDS_USR%" -Danypoint.password="%DEPLOY_CREDS_PSW%" -Dcloudhub.app="%APP_NAME%" -Dcloudhub.environment="%ENVIRONMENT%" -Dcloudhub.bg="%BG%" -Dcloudhub.worker="%WORKER%"'
            }
          }
          stage('Deploy Production') {
            environment {
              ENVIRONMENT = 'Production'
              APP_NAME = '<API-NAME>'
            }
            steps {
                  bat 'mvn -U -V -e -B -DskipTests deploy -DmuleDeploy -Dmule.version="%MULE_VERSION%" -Danypoint.username="%DEPLOY_CREDS_USR%" -Danypoint.password="%DEPLOY_CREDS_PSW%" -Dcloudhub.app="%APP_NAME%" -Dcloudhub.environment="%ENVIRONMENT%" -Dcloudhub.bg="%BG%" -Dcloudhub.worker="%WORKER%"'
            }
          }
        }
      
        tools {
          maven 'M3'
        }
      }

      The Jenkinsfile will execute 4 simple steps:

      • Build: We are going to build jar file.

      • Test: We are going to run MUnit tests configured into the project.

      • Deploy to Development: Deploy in the Sandbox of Development environment.

      • Deploy to Production: Finally if everything goes we well, we are going to deploy to Production environment.

        We won’t explain the Jenkinsfile syntax. More information can be found here

        Since workstations are windows instances there are syntax differences from linux based such as using bat and not sh.

    4. Within the Jenkinsfile search for the value <BUSINESS-GROUP>. It will be near the beginning of the file on line 8. Within the quotes, replace this value with the name of your Anypoint Platform business group name.

      You can see how the variables declared in the pom.xml file are being replaced when we do the deployment.

    5. Scroll down the Jenkinsfile to line 27. Within the quotes, change the value <DEV-API-NAME> to sandbox-omni-channel-api-{initials}. Replace the {initials} with your own initials.

    6. Scroll down the Jenkinsfile to line 36. Within the quotes, change the value <PROD-API-NAME> to prod-omni-channel-api-{initials}. Replace the {initials} with your own initials.

    7. Switch windows back to Git Gui

    8. Press Rescan button

    9. Stage those Changes

    10. Commit them. You can name the commit message as “add Jenkinsfile and update pom.xml”.

Step 4: Configure Jenkins

By now your Jenkins server should be started.

  1. Open Google Chrome from the Desktop and click the Jenkins bookmark located on the toolbar of the browser. Url should be http://localhost:8080

  2. login with:

    • Username: workshop

    • Password: Anypoint 1357!

  3. You are now on the Jenkins homepage. We need to configure the Anypoint Platform Credentials. We don’t want them to be part of our code, so we are going to add as Credentials.

    On the left-nav section, select “Credentials”.

    module7 lab 4 jenkins credentials
  4. Under “Stores scoped to Jenkins”, select “Jenkins”. On the next screen, select “Global credentials (unrestricted)”. Finally on the third screen, select “Add Credentials” which is found on the left-navigation column.

  5. Enter your Anypoint Platform username and password in the corresponding fields.

  6. In the “ID” text field enter: deploy-anypoint-user. This is the ID that must match the ID within your Jenkinsfile in your Mule application on line 6.

  7. Select ‘OK’ to finish creating the user.

    We recommend to use this practice on sensible credentials like Database Users, Salesforce Users, etc.

    Now we want to create the Jenkins job for our project.

  8. On the Jenkins homepage, select the link titled “create new jobs” under the “Welcome to Jenkins!” message.

  9. On the next screen, under “Enter an item name” enter the name of the pipeline. This can be omni-channel-api” or something similar. Then choose “Pipeline” and select “OK” on the pop-up window at the bottom left of the screen.

    module7 lab 4 jenkins menu
  10. Within the Jenkins setup page, scroll to the bottom of the screen to the section labeled “Pipeline”.

  11. On the “Definition” dropdown select the option that says “Pipeline script from SCM”.

  12. On the “SCM” dropdown select the option that says “Git”.

  13. Under “Repositories”, in the “Repository URL” text box enter the path of your repository. Make sure you add the prefix file:// to the directory path. It will look similar to the following:

    file://C:/Users/workshop/AnypointStudio/studio-workspace/omni-channel-api
    module7 lab 4 scm configuration
  14. Now click “Save” at the bottom of the screen. You will be redirected to the home screen of your build pipeline.

  15. From your current page, click the “Jenkins” nav link at the top-left of the screen. It will look like the following:

    module7 lab 4 jenkins nav
  16. Click on your Jenkins pipeline link.

  17. Single-click on “Build now”. You will soon see the job kickoff and status information will appear. The build pipeline will now build the Mule application, run the unit tests and then deploy the application into the Sandbox environment. After that completes, the pipeline will then run the unit tests and then deploy the application into Production.

  18. As the job is running, you can view the log information by clicking the build number and then clicking “Console Output” on the left-nav column.

    module7 lab4 pipeline
    Figure 1. Pipeline Progress view
  19. You can also view a cleaner UI through the Blue Ocean plugin by clicking “Open Blue Ocean” from the job homepage.

    module7 lab 4 blue ociean view
    Figure 2. Blue Ocean view
  20. You can also see the Mule applications getting deployed in the Anypoint Studio Runtime Manager by going logging in to https://anypoint.mulesoft.com and navigating to Runtime Manager by clicking the left navigation dropdown.

    module7 lab 4 anypoint nav
  21. Within Runtime manager you can view both of the applications being deployed in Sandbox and in Production. You can switch between Sandbox view and Production view by clicking on the Sandbox rectangle in the left navigation bar within Runtime Manager.

    module7 lab 4 env

That’s it, the CI/CD process is working and apps are being deployed.

In the next step we are going to automate the building job. So each time there is a change, the build process will start automatically.

Step 5: Automate Git to trigger Jenkins jobs

In our final step, we are going to create a post-commit file within Git so that every time we make a local commit, it will automatically kick off the Jenkins job without any manual intervention. The purpose of this lab is to demonstrate how to set up a fully automated CI/CD process. In production, you will want to have the jobs kickoff automatically after a Git push rather than just a commit.

  1. Within the Windows directory explorer, navigate to the root directory of your Mule application. It will most likely live at this path:

    C:/Users/workshop/AnypointStudio/studio-workspace/omni-channel-api
  2. Within the Mule application root directory navigate to .git > hooks. Next, create a new file called post-commit. (no file extension)

  3. Open the post-commit file with a text editor such as Text Edit by right-clicking on the post-commit file and selecting “Open with” and then selecting Notepad.

  4. Copy and paste the following code within the post-commit file then save:

    #!/bin/sh
    set PATH=%PATH%;"C:\Program Files\Git\bin"
    echo %PATH%
    sh.exe -x -c "curl 'http://workshop:Anypoint1357!@localhost:8080/job/{BUILD-NAME}/build?token=MULE_WORKSHOP'"
  5. Make sure to replace {BUILD-NAME} with the name of the project build (i.e. omni-channel-api-workshop)

  6. Navigate back to Jenkins management console via Google Chrome web browser.

  7. From the Jenkins job project homepage, select Configure from the left-nav menu.

    module7 lab 4 jenkins configure
  8. Scroll down to the section labeled “Build Triggers”.

  9. Select the checkbox for “Trigger builds remotely”.

  10. Within the “Authentication Token” text area enter: MULE_WORKSHOP.

    module7 lab4 build trigger

    Note that this value matches what we entered in the post-commit script.

  11. Within your omni-channel-api project in Anypoint Studio, scroll down to the flow labeled: get:\products\search

    module7 lab 4 get product flow1
  12. Drag in a Logger component from the Mule Palette on the right side of the IDE. Your flow should now look like the following:

    module7 lab 4 get product flow2
  13. Within the Logger component, add some text that you would like logged. (i.e. “Hello from the Mule Workshop!”).

  14. Switch to Git Gui. Select Rescan to find the changes to the directory, then Stage Changed to stage the commits. Finally, enter a commit message such as: Added Logger component and click the “Commit” button to commit the changes.

  15. After those changes have been committed, the Jenkins job will automatically trigger. If you navigate back to the Jenkins management console in Google Chrome, you can see the status of the build job.

Summary

In this lab, we learned how to configure our Mule applications to be managed by Git for version control and also how Mule fits within a Jenkins CI/CD pipeline. We completed the following steps:

  • Initialized a git repository for our Mule application

  • Created a Jenkinsfile

  • Updated the pom.xml of our Mule application

  • Configured Jenkins

  • Configured git with a post-commit action

  • Committed changes to our Mule application

Which resulted in:

  • Our application running through tests and being deployed to our Sandbox and Production environments. MULE If you’d like to learn more

Congratulations! You have completed Lab 4.