Efficient CI/CD for Java Applications with GitHub Actions

Efficient CI/CD for Java Applications with GitHub Actions


3 min read

๐Ÿ“ 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:

โ”œโ”€โ”€ 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

    branches: [ main ]
    branches: [ main ]

    runs-on: ubuntu-latest

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

    - name: Set up JDK 11
      uses: actions/setup-java@v2
        java-version: '11'
        distribution: 'adopt'

    - name: Cache Maven packages
      uses: actions/cache@v2
        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

Did you find this article valuable?

Support Prasad Suman Mohan by becoming a sponsor. Any amount is appreciated!