Integrating Jenkins with AEM Deployments

Ashish MishraAshish Mishra
4 min read

In this article, let's dive in and understand how Jenkins pulls the code from Git, builds it, and then pushes it to AEM, deploying the artifact.

Steps to follow

Step 1: We need to have a running AEM Author and Publish instance. You can also use local author and publish instances for simplicity. If you are deploying to a server, the steps are mostly the same.

Step 2: We need to install Jenkins using Docker. You can install Docker by following this article.

Once Docker is installed, run this command in the terminal -

docker run -d -p 8080:8080 -p 50000:50000 --name jenkins jenkins/jenkins:lts

This command will install Jenkins using Docker on port 8080, and you can access it from your browser. If it's on your local machine, try using localhost:8080, or use serverIP:8080 if it's on a server.

Step 3:

When you open localhost:8080, you'll see a message like the image below, prompting you to access the admin password to continue.

To get the root password, run the following command in the terminal.

docker exec -it jenkins cat /var/jenkins_home/secrets/initialAdminPassword

It will output the admin password, use it to proceed in jenkins.

Next, install the suggested plugins option. It will take few minutes, and all the plugins will get installed. These include plugins like git, dark mode, credentials manager, etc.

It will now ask you to fill username and password. Create a user and proceed.

Step 4:

Once done go to the http://localhost:8080/manage/ and click on Plugins, we have to search and select “Maven Integration plugin” from this list (refer below image)

Step 5:

Once done with installation, go to http://localhost:8080/manage/configureTools/ and scroll below, and go to “Maven Installations”, and select the maven version from the list and save.

Step 6:

Now, we also need to securely pass our credentials to the pipeline. To do this, go to http://localhost:8080/manage/credentials/store/system/domain/_/ and add new credentials there. Make sure to note the ID, as we will use this ID in our pipeline syntax, allowing Jenkins to pass the credentials securely. In my case i have saved my bitbucket(git) credentials to checkout the code, and then aem credentials to upload the package. These two are required for this script execution.

Step 7:

Now, come back to main page http://localhost:8080/ , click on “Create Job” and then provide a name to this item “Local AEM Build and Deploy” and select “Pipeline

Pipeline is the simplest option, but we can choose other options too, depending on the use case, such as "Multi Branch Project." For simplicity, we have selected "Pipeline" for this topic.

Now, you have several options available, and most of them are easy to understand. For this tutorial, scroll down to the end and add the script below in the pipeline script option.

You might need to make some changes based on your use case, such as replacing the correct Maven name you provided in the tools section in Step 5.

Update the AEM_HOST IP or set it to “localhost” if AEM is running locally.

In case jenkins is running on docker, and aem is running on mac/windows, we have to use “host.docker.internal” to access the aem machine from docker jenkins

Depending on the publisher status, you can comment out or remove the “Deploy to Publisher” stage from the script.

Update the correct Git URL of your repository in the “Checkout” step.

pipeline {
    agent any
    tools {
        maven 'maven-3.9.11'
    }
    environment {
        AEM_HOST = "172.104.206.123"
        AUTHOR_AEM_PORT = "4502"
        PUBLISHER_AEM_PORT = "4503"
        AEM_PACKAGE = "all/target/colorful.all-1.0.0-SNAPSHOT.zip"
    }

    stages {
        stage('Checkout') {
            steps {
                git branch: 'master',
                        credentialsId: 'bitbucket-credentials',
                        url: 'https://the-colorful-slate@bitbucket.org/the-colorful-slate/colorful-aem.git'
            }
        }

        stage('Build Package') {
            steps {
                sh """
                mvn clean install
                """
            }
        }

        stage('Archive Package') {
            steps {
                archiveArtifacts artifacts: "${AEM_PACKAGE}", fingerprint: true
            }
        }

        stage('Deploy to AEM Author') {
            steps {
                // Inject AEM credentials securely
                withCredentials([usernamePassword(
                        credentialsId: 'aem-credentials',
                        usernameVariable: 'AEM_USER',
                        passwordVariable: 'AEM_PASS'
                )]) {
                    sh """
                        mvn com.day.jcr.vault:content-package-maven-plugin:1.0.2:install \
                          -Dvault.file=${AEM_PACKAGE} \
                          -Daem.host=$AEM_HOST \
                          -Daem.port=$AUTHOR_AEM_PORT \
                          -Dvault.user=$AEM_USER \
                          -Dvault.password=$AEM_PASS
                    """
                }
            }
        }

        stage('Deploy to AEM Publisher') {
            steps {
                // Inject AEM credentials securely
                withCredentials([usernamePassword(
                        credentialsId: 'aem-credentials',
                        usernameVariable: 'AEM_USER',
                        passwordVariable: 'AEM_PASS'
                )]) {
                    sh """
                        mvn com.day.jcr.vault:content-package-maven-plugin:1.0.2:install \
                          -Dvault.file=${AEM_PACKAGE} \
                          -Daem.host=$AEM_HOST \
                          -Daem.port=$PUBLISHER_AEM_PORT \
                          -Dvault.user=$AEM_USER \
                          -Dvault.password=$AEM_PASS
                    """
                }
            }
        }
    }

    post {
        success {
            echo '✅ Build & Deploy successful!'
        }
        failure {
            echo '❌ Build failed!'
        }
    }
}

Once finished, you can click on "Build Now" to start running the Jenkins pipeline. You will see a similar page once it is completed.

That's it! We successfully set up Jenkins on Docker, checked out the AEM code from Git, and deployed it to running AEM instances.

Do comments, if you face any issue while implementing this. 🚀🚀🚀

0
Subscribe to my newsletter

Read articles from Ashish Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ashish Mishra
Ashish Mishra

I am an AEM Full Stack Developer. I like to work on different technologies, and to build different Apps, Bots. Have Created this blog to write down and share all my works.