Streamlining Continuous Integration and Deployment for Java with Jenkins

ยท

4 min read

Here are the steps to build Java code, deploy it to QA and Prod using Jenkins, publish artifacts and pull from artifacts on an EC2 Ubuntu instance:

  1. Install Java, Maven, Jenkins on an EC2 Ubuntu instance.

     sudo apt update
     sudo apt install default-jdk
     java -version
    

    Install Maven:

     sudo apt install maven
    

    Install Jenkins:

     curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
       /usr/share/keyrings/jenkins-keyring.asc > /dev/null
     echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
       https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
       /etc/apt/sources.list.d/jenkins.list > /dev/null
     sudo apt-get update
     sudo apt-get install jenkins
    

     sudo cat /var/lib/jenkins/secrets/initialAdminPassword
    

    Now you have a Ubuntu EC2 instance ready with Java, Maven and Jenkins installed to start building Java applications.

  2. In Jenkins, create a new Freestyle project. Configure the project to checkout code from your Git repository.

    1. In the Jenkins dashboard, click on New Item

    2. Enter a name for your project (e.g. MyJavaApp) and select Freestyle project. Click OK.

    3. In the Source Code Management section, select Git.

    4. Enter the URL of your Git repository (e.g.https://github.com/eddzaa/NCPL.git)

    5. Specify credentials if your repository is private.

    6. You can optionally specify the branch to build (e.g. master).

    7. In Build Triggers section, select how you want to trigger builds (e.g. GitHub hook trigger for GITScm polling)

      Add webhooks in GitHub

  3. Under Build, add a build step to execute Maven to compile the code and run tests:

    1. Under Build, add a build step to execute Maven to compile the code and run tests:

       clean install
      

    2. Add a post-build action to archive the webapp/target/*.war file as an artifact.

      1. Go to Post-build Actions section.

      2. Click Add post-build action and select Archive the artifacts.

      3. In Files to archive, enter: webapp/target/*.war

        Save the configuration and build it.

  1. Create two more Freestyle projects for QA deployment. Configure to download the artifact from the build job.

    1. Create a new Freestyle project called 'Deploy_QA'

    2. Under General section, check 'This project is parameterized' and add a String Parameter called 'BUILD_NUMBER'

    3. Under Source Code Management, skip config since we don't need to checkout any code

    4. Under Build Triggers, check 'Build after other projects are built' and specify your main build job

    5. Under Build, add a Copy artifact from another project step:

      1. If you don't see the "Copy artifact from another project" option under Add Build Step in Jenkins, the Copy Artifact plugin is not installed - This is an optional plugin that needs to be installed to enable copying artifacts between jobs. Go to Manage Jenkins > Manage Plugins and search for "Copy Artifact Plugin" and install it.

  • Click Add build step and select Copy artifact from another project.

  • In Project name field, enter the name of your main build job that publishes the artifacts. For example, 'MyJavaApp'.

  • In Artifact to copy, enter 'webapp/target/*.war' to copy the JAR files from the target folder.

  • In Target directory, specify the local directory where you want to copy the artifacts. For example, '/opt/jenkins/downloads'

  • Set Project name to your main build job

  • Set Artifact to download as 'webapp/target/*.war'

  • In Target directory, specify the local directory where you want to copy the artifacts. (For example, '/opt/jenkins/downloads' .You first need to create the directory and provide ownership and permissions:)

    1.  sudo mkdir -p /opt/jenkins
       sudo chown -R jenkins:jenkins /opt/jenkins
       sudo chmod -R 775 /opt/jenkins
      
  • Save it and Build it to check

  1. Steps to create a 'Deploy_Prod' Jenkins job that downloads artifacts from the main build job and deploys to production:

    1. Create a new Freestyle project called 'Deploy_Prod'

    2. Under General section, check 'This project is parameterized' and add a String Parameter called 'BUILD_NUMBER'

    3. Under Source Code Management, skip config

    4. Under Build Triggers, check 'Build after other projects are built' and enter your build job name.

    5. Under Build, add a Copy artifact from another project step:

  • Project name: MyJavaApp

  • Artifact to copy: webapp/target/*.war

  • Target directory: /opt/jenkins/prod_deploy

Save it and Build it to check

It's essential to carefully plan and configure each step to ensure a smooth pipeline. Also, use appropriate security measures for credentials, access control, and artifact storage.Happy Coding ๐Ÿš€

Did you find this article valuable?

Support Edvin Dsouza by becoming a sponsor. Any amount is appreciated!

ย