Streamlining Continuous Integration and Deployment for Java with Jenkins
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:
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.
In Jenkins, create a new Freestyle project. Configure the project to checkout code from your Git repository.
In the Jenkins dashboard, click on New Item
Enter a name for your project (e.g. MyJavaApp) and select Freestyle project. Click OK.
In the Source Code Management section, select Git.
Enter the URL of your Git repository (e.g.https://github.com/eddzaa/NCPL.git)
Specify credentials if your repository is private.
You can optionally specify the branch to build (e.g. master).
In Build Triggers section, select how you want to trigger builds (e.g. GitHub hook trigger for GITScm polling)
Add webhooks in GitHub
Under Build, add a build step to execute Maven to compile the code and run tests:
Under Build, add a build step to execute Maven to compile the code and run tests:
clean install
Add a post-build action to archive the webapp/target/*.war file as an artifact.
Go to Post-build Actions section.
Click Add post-build action and select Archive the artifacts.
In Files to archive, enter: webapp/target/*.war
Save the configuration and build it.
Create two more Freestyle projects for QA deployment. Configure to download the artifact from the build job.
Create a new Freestyle project called 'Deploy_QA'
Under General section, check 'This project is parameterized' and add a String Parameter called 'BUILD_NUMBER'
Under Source Code Management, skip config since we don't need to checkout any code
Under Build Triggers, check 'Build after other projects are built' and specify your main build job
Under Build, add a Copy artifact from another project step:
- 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:)
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
Steps to create a 'Deploy_Prod' Jenkins job that downloads artifacts from the main build job and deploys to production:
Create a new Freestyle project called 'Deploy_Prod'
Under General section, check 'This project is parameterized' and add a String Parameter called 'BUILD_NUMBER'
Under Source Code Management, skip config
Under Build Triggers, check 'Build after other projects are built' and enter your build job name.
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 ๐