Managed Services

Build Automation and Deployment of AEM Using Jenkins

1. Introduction

In this Blog, we are going to explain Build Automation using Jenkins. And deploying the code to AEM Instance(s)
Below is the screen grab of the basic Jenkins console.

2. Creating Build Job with Jenkins

Step 1: Login Jenkins with an Admin/ a user who has permissions to run a build.

Step 2: Click on the Manage Jenkins and go to Manage Plugins. verify what are the plugins are installed on Jenkins on the Installed tab as shown in below and confirm ANT, MAVEN and SUBVERSION are installed or now. Basically, these are the default plugins installed while installing the Jenkins.

Step 3: Now create a new job by clicking on New Item. Please enter Job Name and Project type as shown below.

Note: Make sure MAVEN(mvn) is installed on Jenkins server. If not Install maven by using below command.
yum install mvn (in Linux)
apt-get install mvn (in Ubuntu)

Step 4: And then click on OK button, it will open the configuration of Demo project i.e, JOB.

With this we have created a job, now we need to configure the job based on the requirement.

Source Code Management

Open the job which you created in above step
Step 1: Scroll down the page to Source Code Management and then click on GIT.

Step 2: Now Provide the details of Repository path/URL and if any credentials are required to access the repository please click on the Add button and it will open the console like below, enter Credentials to access the git repository and click on Add to save the credentials.

Step 3: Now Enter the Git Repository URL and select the credentials to access provided URL(Repository URL)

Build Configurations

Scroll down the page and you will find out the BUILD section. And configure them as shown in below.

Root POM : pom.xml” ( this will taking the root pom file of repository by default) If we have any specific pom file, we have to mention that pom file path. Goals and Options: “clean install” (Here we need to mention the Maven build action for the JOB.)

 

And click on Apply and Save button to save the job.

Types of Build

  • Manual Build
  • Build Periodically
  • Build Automatically when Code commits in Repository
    Manual Build:

    Once you are done with above steps, you will get the below window, from here we can initiate the Build by clicking on Build Now. Once we clicked on the Build Now, the build will start, we can track it by clicking on the Console output.

    Build Periodically:

    Scroll down the JOB page until you will find the Build Triggers. Fill the Scheduled field based on your requirements.
    You can find out the last run and next run time right below the schedule field.

    Build Automatically when Code commits in Repository
    • Configurations Required at GIT Level:
    • Configurations at Jenkins Side:
       i) Configurations Required at GIT Level:

      Step 1: Open your GIT repository with credentials.
      Step 2: 
      Click on your project for which you want to setup auto build.
      Step 3: Click on the project then you can see the below screen.

      Step 4: Click on the settings then Integration to configure the WebHooks. Below is the screenshot of configurations.

      Step 5: Click on Save once you are done with the Configurations.
      Step 6: We can test the connection by click on the TEST. If everything is good then it will display the connection successful message. Below is the Screenshot.

      ii) Configurations Required at Jenkins Level:

      Step 1: Open the Jenkins and log in with Admin credentials.
      Step 2: Select the JOB which is required to configure the continuous deployment.
      Step 3: Scroll down the JOB page until you will find the Source Code Management, provide the Git repository and Credentials.

      Step 4: Scroll down to Build Triggers and configure as shown below.

      Step 5: Configure the remaining the sections as below.

      Now everything should be set! If you push a commit to the repository, you should see the Jenkins job start running.

      Configuring the Continuous deployment for a Job

      Whatever the scenario you choose to build, we need to configure post-build steps as below to deploy the code to AEM instance(s) automatically.Step 1: Open the Jenkins and log in with Admin credentials.
      Step 2: Select the JOB which is required to configure the continuous deployment.
      Step 3: Scroll down the JOB page until you will find the POST STEPS. Click on the Add post-build step, then Execute shell. and configure as shown below.

      Step 4: Now we need to mention below command to execute the script, which will help us to auto-deploy the code to AEM instance./bin/bash -x /scripts/buildscript.sh

      Note: Here, script(buildscript.sh) is available under /scripts directory.
      Below is the Scritsnippet for reference.

      TIME=`date +%d%m%Y_%H%M`
                                 cd /var/lib/jenkins/workspace/Test_1
                                 mkdir zipfiles
                                 find . -name "*.zip" -not -path "./zipfiles/*" -exec cp -rf {} zipfiles \;
                                 cd zipfiles
                                 find . -name "*.zip" -exec basename {} .zip \; >> filenames
                                 file=filenames
                                 #Dev Environment Details
                                 Author_Host="172.16.1.149"
                                 Author_Port="4502"
                                 User="admin"
                                 Passwd="admin"
                                 # Package Manager URL of the AEM.
                                 URL="/crx/packmgr/service.jsp"
                                 echo
                                 echo " Installing Pakage on Author"
                                 echo
                                 while IFS= read -r LINE; do
                                 curl -u $User:$Passwd -F file=@$LINE.zip -F name=$LINE -F force=true -F install=true http://$Author_Host:$Author_Port$URL
                                 if [ $? -eq 0 ] ; then
                                       echo "Installing Package on Author is Sucess" >> /tmp/jenkinslog
                                 else
                                       echo "Installing Package on Author Failed" 1>&2 >> /tmp/jenkinslog
                                 fi
                                 done < "$file"
      

      Step 6: Click on Save and Apply.
      With this we are done with the configurations, now code should be deployed to the AEM instance(s) for every successful build.

3. Validation

Step 1: Open the AEM and log in with your credentials.

Step 2: After successful login, Goto the Package Manager Console(/crx/packmgr/index.jsp) here we can find all the installed packages.


If you find your package here that means code successfully deployed to AEM instance.

About The Author

Leave a Reply

*