You are on page 1of 12

5/29/23, 11:57 PM Efficient CI/CD for Java Applications with GitHub Actions

TABLE OF CONTENTS

DeployToCloud
Follow

Efficient CI/CD for Java Applications with


GitHub Actions
Suman Prasad
May 29, 2023 · 3 min read
10

https://sumanprasad.hashnode.dev/efficient-cicd-for-java-applications-with-github-actions 1/12
5/29/23, 11:57 PM Efficient CI/CD for Java Applications with GitHub Actions

PLAY THIS ARTICLE


TABLE OF CONTENTS
0:00 / 2:36

📍 Introduction:
Continuous Integration and Continuous Deployment (CI/CD) are essential practices
in modern software development. They help streamline the development process,
reduce errors, and ensure that your application is always up-to-date. In this blog,
we'll walk you through setting up a CI/CD pipeline for a Java application using
GitHub Actions. We'll cover everything from creating a simple Java application to
deploying it using GitHub Actions.
📝 Pre-requisites:
https://sumanprasad.hashnode.dev/efficient-cicd-for-java-applications-with-github-actions 2/12
5/29/23, 11:57 PM Efficient CI/CD for Java Applications with GitHub Actions

ATABLE
GitHubOFaccount
CONTENTS
Basic knowledge of Java and Maven

🚀 Step 1: Create a Java application


First, let's create a simple Java application using Maven. Open your terminal and
run the following command:
COPY

mvn archetype:generate -DgroupId=com.example -DartifactId=my-java-app -Darchet

This command generates a basic Java application with the following structure:
COPY

my-java-app
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── example
│ └── App.java
└── test
└── java
└── com
└── example
└── AppTest.java

https://sumanprasad.hashnode.dev/efficient-cicd-for-java-applications-with-github-actions 3/12
5/29/23, 11:57 PM Efficient CI/CD for Java Applications with GitHub Actions

🚀 Step 2: Initialize a Git repository


TABLE OF CONTENTS
Navigate to the my-java-app directory and initialize a Git repository:
COPY

cd my-java-app
git init

Create a .gitignore file to exclude unnecessary files from the repository:


COPY

echo "target/" > .gitignore

Commit the initial project files:


COPY

git add .
git commit -m "Initial commit"

🚀 Step 3: Create a GitHub repository


Create a new repository on GitHub and push your local repository to it:
COPY

git remote add origin https://github.com/your-username/my-java-app.git


git branch -M main
git push -u origin main

https://sumanprasad.hashnode.dev/efficient-cicd-for-java-applications-with-github-actions 4/12
5/29/23, 11:57 PM Efficient CI/CD for Java Applications with GitHub Actions

🚀 Step 4: Set up GitHub Actions


TABLE OF CONTENTS
Create a new directory called .github/workflows in your project:
COPY

mkdir -p .github/workflows

Create a new file called ci-cd.yml inside the .github/workflows directory:


COPY

touch .github/workflows/ci-cd.yml

Open the ci-cd.yml file and add the following content:


COPY

name: Java CI/CD

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

https://sumanprasad.hashnode.dev/efficient-cicd-for-java-applications-with-github-actions 5/12
5/29/23, 11:57 PM Efficient CI/CD for Java Applications with GitHub Actions

- name: Set up JDK 11


TABLE OFactions/setup-java@v2
uses: CONTENTS
with:
java-version: '11'
distribution: 'adopt'

- name: Cache Maven packages


uses: actions/cache@v2
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2

- name: Build and test with Maven


run: mvn clean verify

- name: Package application


run: mvn package

- name: Deploy
if: github.ref == 'refs/heads/main'
run: echo "Deploy your application here"

This GitHub Actions workflow does the following:


Triggers on push and pull request events to the main branch
Checks out the repository
Sets up JDK 11
Caches Maven packages for faster builds
Builds and tests the application using Maven
Packages the application
https://sumanprasad.hashnode.dev/efficient-cicd-for-java-applications-with-github-actions 6/12
5/29/23, 11:57 PM Efficient CI/CD for Java Applications with GitHub Actions

Deploys the application (replace the echo command with your deployment
TABLE OF CONTENTS
script)

🚀 Step 5: Commit and push the GitHub Actions workflow


Add the new GitHub Actions workflow to your repository:
COPY

git add .github/workflows/ci-cd.yml


git commit -m "Add GitHub Actions CI/CD workflow"
git push

Now, whenever you push changes to your main branch or create a pull request, the
GitHub Actions workflow will automatically build, test, and deploy your Java
application.
📍 Conclusion:
In this blog, we've demonstrated how to set up a CI/CD pipeline for a Java
application using GitHub Actions. By following these steps, you can automate the
build, test, and deployment processes for your Java projects, ensuring that your
application is always up-to-date and error-free. Happy coding!

🔹 Checkout GitHub Repository for projects:


🔗 github.com/sumanprasad007

https://sumanprasad.hashnode.dev/efficient-cicd-for-java-applications-with-github-actions 7/12
5/29/23, 11:57 PM Efficient CI/CD for Java Applications with GitHub Actions

TABLE OF CONTENTS

Join the community of learners and be a part of the conversation! Your feedback is
valuable to us, so please share your thoughts in the comments section. Help us
make this blog even better for everyone. And if you found this post helpful, spread
the word! Share it with those who could benefit from the information. And don't
forget to follow along and subscribe to our newsletter for instant updates on our
latest content. Thank you for taking the time to read and engage with us!

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

https://sumanprasad.hashnode.dev/efficient-cicd-for-java-applications-with-github-actions 8/12
5/29/23, 11:57 PM Efficient CI/CD for Java Applications with GitHub Actions

Enter your email address SUBSCRIBE


TABLE OF CONTENTS

Did you find this article valuable?


Support Suman Prasad by becoming a sponsor.
Any amount is appreciated!
Sponsor
See recent sponsors | Learn more about Hashnode
Sponsors

Web Development Devops Developer Beginner Developers


software development

WRITTEN BY
Suman Prasad
Follow

I'm Proficient in a variety of DevOps technologies, including AWS, Linux, Python,


Shell Scripting, Docker, Terraform, Jenkins, Git/GitHub, and Computer
Networking. I have a strong ability to troubleshoot and resolve issues, and are
consistently motivated to expand the knowledge and skills through exploration
of new technologies.

https://sumanprasad.hashnode.dev/efficient-cicd-for-java-applications-with-github-actions 9/12
5/29/23, 11:57 PM Efficient CI/CD for Java Applications with GitHub Actions

MORE ARTICLES
TABLE OF CONTENTS
Suman Prasad

Building a Django Digital Clock App with CI/CD using


GitHub Actions and Deploying to AWS
📍 Introduction: In this tutorial, we will create a simple Django digital clock application,
set up …

Suman Prasad

https://sumanprasad.hashnode.dev/efficient-cicd-for-java-applications-with-github-actions 10/12
5/29/23, 11:57 PM Efficient CI/CD for Java Applications with GitHub Actions

TABLE OF CONTENTS

Suman Prasad

Real-time Log Visualization using Grafana, Loki, and


Promtail on EC2 Instances
📍 Introduction: In this blog, we will explore the concept of observability and how it can
be achiev…

Escaping the Matrix: Learnings from Ishan Sharma's Videos


📍 Introduction: Are you tired of feeling like you're stuck in a rut and not making
progress towards…

©2023 DeployToCloud
https://sumanprasad.hashnode.dev/efficient-cicd-for-java-applications-with-github-actions 11/12
5/29/23, 11:57 PM Efficient CI/CD for Java Applications with GitHub Actions

Archive · Privacy policy · Terms


TABLE OF CONTENTS
Publish with Hashnode

Powered by Hashnode - Home for tech writers and readers

https://sumanprasad.hashnode.dev/efficient-cicd-for-java-applications-with-github-actions 12/12

You might also like