Automate Deployment to CloudHub using CloudHub Deployer Plugin Jenkins
Learn how to deploy Mule applications to CloudHub using Cloudhub deployer Jenkins plugin, and achieve continuous delivery and deployment.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
We live in an age, Where DevOps and automation are becoming more and more necessary and important in projects. So uploading packages manually to servers or platforms is not feasible and salable when you work with architecture like micro-services. So to tackle this problem we need to implement Continuous Delivery and Deployment cycle in our project. In this post I will be showing you how to do exactly that with Mule applications.
After creating a basic Mule App, you might be wondering how to automate the process of deploying a Mule App to CloudHub. In this post, I will be introducing a Jenkins plugin(Github Repository) that I published recently that enables this use case.
How it is compared to other solution/tools available with Jenkins:
Mule-Maven plugin - With this approach you are tight coupling you build and deploy process and most of time its not good. And its hard to scale this approach when you have multi environment deployment and many applications to manage. This approach will not work if you just want to do deployment.
This approach will take time and effort to get working automation that meets your project requirement. The CloudHub Deployer plugin itself is built using same API why re-invent the wheel.
What we will accomplish here:
Jenkins release pipeline using both free style and pipeline script that automates your mule application deployment to CloudHub.
Prerequisites:
- You will need to have Jenkins instance up and running.
- A CloudHub account.
- You need to have a already built package to follow along. Since I am not covering CI(Continuous Integration) for mule apps, there are plenty article on internet for that.
Deploying Using Freestyle Job
Step 1: Plugin Installation
Go to Manage Jenkins -> Manage Plugins -> Click on "Available" tab -> search for "cloudhub deployer"(just cloudhub will also do) -> select the plugin and click on install.
Step 2: Create Jenkins Job (freestyle)
Step 3: Plugin Initialization
You can find the Cloudhub deployer under "Build" section of job configuration.
Step 4: Plugin Configuration
For a few of the repetitive configurations, you can set them at plugin global configuration under Cloudhub Settings in "Configure System". The global settings will be overridden if you also configure them in job configuration.
Build Step Config
If you want to inherit these configuration from global configuration check the "Use global settings"
Or else configure them here by unchecking the "Use global settings" checkbox.
Here you need to provide the organization id, environment id (you can get these from Access management console in CloudHub) and login credentials(make sure they have right amount of permission to perform actions on CloudHub like create, update delete, etc). Set the request and response timeout default is 2 minutes. And if something goes in deployment or job fails enable debug mode print all info.
In this section enter the application name, select the type of request you want to perform against application.
Check the autostart checkbox if you want your application to get started after deployment. Enter mule runtime version to use in cloudhub and region. Use help section of plugin if not clear with any field.
Type of Requests:
- CREATE - Deploy/Create new application
- UPDATE - Update application with new package and settings provided.
- UPDATE_FILE - Update just the zip/jar file of application, rest all settings ignored.
- CREATE_OR_UPDATE - Deploy/Create new application(if not already present) or update the existing application with settings provided.
- RESTART - Restart the application already present.
- DELETE - Delete the application.
Enter the package location relative to Jenkins workspace. Can be both zip and jar file. Regex can also be used here but it should resolve to a single file path other-wise job will throw error.
In this we need to provide configurations related to worker/instance. which is number of worker to run, type of each worker, worker CPU and RAM size (check MuleSoft Documentation for more details on this).
Settings here are all optional can be enabled based on requirement. Click on plugin help section for additional detail on each option.
Settings here are also optional can be set if needed. You can add key value list to application environment and additional log levels with categories to include in logs.
Application takes time to start in CloudHub after deployment or update operation, It can vary from seconds to minutes before you can access your deployed changes. If you check "Verify Deployment" then the jenkins job will wait for application to start in cloudhub before showing success. Jenkins job will ping the cloudhub for application status at interval which is specified in verify interval field.
After all the configurations are done apply and save the changes.
Step 3: Trigger the Job
You can login to CloudHub and check your application will be successfully deployed there.
Deploying Using Jenkins Pipeline Script
Step 1: Create the Pipeline Job
Step 2: Write script
Usually in a real scenario you check in the script along with code in source code repository but for this demo will add the script directly in pipeline job.
x
import org.jenkinsci.plugins.cloudhubdeployer.common.*;
import org.jenkinsci.plugins.cloudhubdeployer.data.*;
pipeline {
agent any
stages {
stage('Deploy') {
steps {
cloudhubDeployer(environmentId :'<insert env id here>', orgId :'<insert org id here>', requestMode : RequestMode.UPDATE , appName :'jenkins-deployplugin-api', credentialsId :'CLOUDHUB_ACCESS', muleVersion :'4.3.0', region :'us-east-1', filePath :'target/*.jar', timeoutConnection : 90000, timeoutResponse : 90000, debugMode : DebugMode.DISABLED, ignoreGlobalSettings : true,autoStart : true, workerAmount : 1, workerType : 'Small',workerWeight : '0.2',workerCpu : '0.2', workerMemory : '1 GB', monitoringEnabled : true, monitoringAutoRestart : true, loggingNgEnabled : true, persistentQueues : false, persistentQueuesEncrypted : false, objectStoreV1 : false, envVars : [envVars(key : 'env' , value : 'sandbox')], logLevels : [logLevels(levelCategory : LogLevelCategory.DEBUG ,packageName : 'some.package.name')], verifyDeployments : true)
}
}
}
}
If you get an error in deployment saying
Scripts not permitted to use staticField org.jenkinsci.plugins.cloudhubdeployer.common.RequestMode UPDATE.
then you need to approve the script signature in Script Approval under Manage Jenkins. Do this for all similar error (this is one-time process).
If you want to use global setting set the 'ignoreGlobalSettings' to false and remove the unnecessary fields from code. More examples script for all other request can be found here
Step 3: Trigger the Pipeline
You can login to CloudHub and check your application will be successfully deployed there.
Congratulation you have successfully deployed your application to CloudHub.
Features that will be coming soon to plugin:
- Ability to deploy using env and organization name instead of id.
- Support for Autoscale policies
- Support for Load balancer mapping
- Support for API manger
Thanks for reading!
Opinions expressed by DZone contributors are their own.
Trending
-
Boosting Application Performance With MicroStream and Redis Integration
-
How To Design Reliable IIoT Architecture
-
Hyperion Essbase Technical Functionality
-
Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers
Comments