Create pipeline using Jenkins GitHub and AWS for Java Project
Published: 2023-01-31 07:01:02

---
title: Create pipeline using Jenkins GitHub and AWS for Java Project
published: true
date: 2023-01-31 06:49:02 UTC
tags: 
canonical_url: https://palak-bhawsar.hashnode.dev/create-pipeline-using-jenkins-github-and-aws-for-java-project
---

In this article, we are going to create a continuous integration pipeline for a Maven project. This pipeline will fetch the code from GitHub, build the code, run the test cases, and will store the generated artifacts in the AWS S3 bucket.

**Prerequisite:**

- AWS account with user permission to upload files in S3

- AWS access key and secret access key for a user

- GitHub account

- Understanding of Jenkins

- Familiarity with Maven commands to build and run Java project

**Let's first understand what Jenkins, GitHub, Webhook, S3, and Maven are:**

**_Jenkins_** is an open-source automation tool written in Java with plugins built for Continuous Integration and Continuous deployment/delivery purposes.

**_GitHub_** is a code hosting platform for collaboration and version control. It lets you and others work together on projects.

**_Webhook_** is a mechanism to automatically trigger the build of a Jenkins project in response to a commit pushed to a Git repository.

**_Maven_** is a powerful project management tool that is based on POM (project object model). It is used for project build, dependency, and documentation.

**_JUnit_** is an open-source Unit Testing Framework for JAVA. It is useful for Java Developers to write and run repeatable tests.

**_Amazon S3_** (Simple Storage Service) is an object storage service that offers industry-leading scalability, data availability, security, and performance.

## 1. Launch an EC2 instance and Install Jenkins

First set up the Jenkins server to create a pipeline. Launch an EC2 instance and install Java and Jenkins into it.

[Follow this article to set up Jenkins server](https://dev.to/palakbhawsar98/install-jenkins-in-ec2-instance-using-user-data-script-3neg)

You need to install Maven as well to build the project. SSH into the instance and install maven using the below command.

```
sudo apt-get install maven -y

```

Set JAVA\_HOME and MAVEN\_HOME using the below commands. To set the variable permanently, you should add it to the bashrc file in your home directory.

```
echo "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64" >> ~/.bashrc

echo "export MAVEN_HOME=/usr/share/maven" >> ~/.bashrc

```

Check if the variables are exported correctly.

```
tail -3 ~/.bashrc

```

## 2. Install Plugins

Install plugins to integrate Jenkins with GitHub, Maven, and AWS S3. Go to **Manage Jenkins** , and select **Manage plugins.** Under available plugins search for the below plugins and **Install without restart**

[**GitHub**](https://plugins.jenkins.io/github)

[**Maven Integration**](https://plugins.jenkins.io/maven-plugin)

[**S3 publisher**](https://plugins.jenkins.io/s3)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675081168869/fe1a4758-596f-4a1a-97af-71cb0a6aadac.png)

## 3. Configure Java and Maven in Jenkins

Go to **Manage Jenkins,** select **Global tool configuration** , and scroll down to add JDK and Maven path that we exported in the above steps as shown below. Uncheck the **Install automatically** checkbox and click **Save**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675069763572/ff4a57f4-9933-40a9-af4e-3e20340e7fd1.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675069801710/a4ed94c7-ec59-4fd5-8452-674b3d47db99.png)

## 4. Create S3 bucket to store artifacts

Search for S3 in AWS services and click **Create bucket.** Give a unique name to the bucket and choose the region in which you want your bucket to reside. Leave all the setting default and click **Create bucket** at last.

Go to Jenkins dashboard, select **Manage Jenkins** then **configure System.** Scroll to the end to add **Amazon S3 profiles.** Give S3 bucket name in the profile name and access key and secret access key as shown below and click **Save**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675082412233/b1b666da-4ba3-4a83-b7f9-2b379bf0880e.png)

## 5. Create Webhook

Webhook in Jenkins triggers the pipeline automatically when any changes are done in the GitHub repository like commit and push. Go to Jenkins dashboard and copy the URL in the browser. Now go to GitHub repository settings. In the left pane select **Webhooks.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675071024012/0222fbfa-6a5d-40ce-9b52-78188cd52946.png)

Click **Add webhook** and paste the Jenkins URL in **the Payload URL** by appending the URL with **/github-webhook/** as shown below. Select the events when you want to trigger the pipeline, I have selected **Just the push event.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675071481032/0263bc55-52ac-4f6a-8f50-437253f5fc0e.png)

## 6. Create Jenkins pipeline

Go to Jenkins dashboard and click **New item**. Enter an item name and select **Freestyle project** and click **Ok**. Give a small description of your project and select GitHub project, and enter the URL of your **GitHub project**. Scroll down and select **Git** as source code management and enter the repository URL of your project. If your repository is private then add credentials. Enter your branch name under **Branches to build** section.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675073247225/ee8154dc-2eef-47ca-952a-57f4cabf5df7.png)

Select the **GitHub hook trigger for GITScm polling** under build triggers. In **Build Steps** select Invoke top-level Maven targets. Select MAVEN\_HOME which we configured in the maven version and enter the **clean install test** in Goals.

In **Post-build Actions,** select **Publish artifacts to S3 Bucket**. Select your AWS profile name and in the source enter the artifact path that you want to store in the S3 bucket. Give the name of your S3 bucket in the Destination bucket. Select the bucket region in which you have created your bucket. Keep all other settings default and click apply save.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675073995599/28080b29-26fa-42b4-946b-08373ec535f7.png)

Finally, click **Build now** in the left panel, and you will see the pipeline run successfully.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675074356600/a145c104-d5c8-4523-8bf8-092edc1ee1c3.png)

Check the artifact stored in the S3 bucket after the successful build.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675074419909/cafc7e10-f485-4508-8ac4-16ce5b0afd22.png)

**Congratulations 🥳, the pipeline ran successfully and artifacts got stored in S3**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675082940030/f8e0b950-d705-428e-975a-1b96b0dc7678.gif)

**If you face any issues contact me on my socials** 👉 [**Contact m**](https://linkfree.eddiehub.io/palakbhawsar98) **e**