Efficient CI/CD for Java Applications with GitHub Actions
Table of contents
๐ 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:
A GitHub account
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:
mvn archetype:generate -DgroupId=com.example -DartifactId=my-java-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command generates a basic Java application with the following structure:
my-java-app
โโโ pom.xml
โโโ src
โโโ main
โ โโโ java
โ โโโ com
โ โโโ example
โ โโโ App.java
โโโ test
โโโ java
โโโ com
โโโ example
โโโ AppTest.java
๐ Step 2: Initialize a Git repository
Navigate to the my-java-app directory and initialize a Git repository:
cd my-java-app
git init
Create a .gitignore file to exclude unnecessary files from the repository:
echo "target/" > .gitignore
Commit the initial project files:
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:
git remote add origin https://github.com/your-username/my-java-app.git
git branch -M main
git push -u origin main
๐ Step 4: Set up GitHub Actions
Create a new directory called .github/workflows in your project:
mkdir -p .github/workflows
Create a new file called ci-cd.yml inside the .github/workflows directory:
touch .github/workflows/ci-cd.yml
Open the ci-cd.yml file and add the following content:
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
- name: Set up JDK 11
uses: actions/setup-java@v2
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
Deploys the application (replace the echo command with your deployment script)
๐ Step 5: Commit and push the GitHub Actions workflow
Add the new GitHub Actions workflow to your repository:
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