This project automates the process of building, validation, testing, and deploying a Java application using Docker and Azure. The pipeline, powered by GitHub Actions, ensures that when changes are pushed to the main branch, the application is built, tested, and deployed to an Azure server with Docker. It also allows easy access to monitor the server's CPU and uptime metrics.
Before using this project, ensure the following prerequisites are set up:
- Install Docker on your local machine and on the Azure server where the application will be deployed.
- Follow the official Docker installation guide: Install Docker.
- Set up an Azure virtual machine (VM) or server to deploy the application.
- Ensure SSH access is enabled with a valid user who has the necessary privileges to run Docker commands.
You must configure the following secrets in your GitHub repository to securely store credentials:
DOCKERHUB_USERNAME: Your DockerHub username.DOCKERHUB_TOKEN: Your DockerHub access token/password.AZURE_SERVER_USERNAME: The username for your Azure server.AZURE_SERVER_PASSWORD: The password for SSH access to the Azure server.AZURE_SERVER_IP: The IP address of your Azure server.DOCKER_USER: This is the docker reponame.
The repository includes a Makefile that automates common tasks. Ensure it includes:
make install: Installs the required dependencies for the java project.make test: Runs unit and integration tests to ensure the application functions correctly.make docker-build: Build the docker image(artifact) using the Dockerfile and tag it.make docker-push: Push the docker image (artifact) in dockerHub.make docker-run: Run the application locally.
The project uses GitHub Actions to automate the CI/CD pipeline. The pipeline is defined in .github/workflows/java-CI-Pipeline.yml.
The pipeline consists of the following steps:
- Checkout code: Pulls the latest code from the repository.
- Set up jdk environment: Sets up Jdk 21.x using the
actions/setup-java@v3action. - Install dependencies: Executes
make installto install project dependencies. - Run validate: Executes
make validateto run validation checks. - Run tests: Executes
make testto run the test suite. - Docker login: Logs into DockerHub using credentials stored as GitHub secrets.
- Docker build and push: Builds the Docker image and pushes it to DockerHub with a dynamic tag (
1.0.0by default). - Deploy to Azure: SSHs into the Azure server, installs Docker if necessary, pulls the latest Docker image, and restarts the Docker container with the updated image.
The CI pipeline deploys the application to Azure by SSH-ing into the server and performing the following:
- Installing Docker (if it's not already installed).
- Pulling the latest Docker image from DockerHub (
$DOCKER_USER/$DOCKER_IMAGE:$TAG). - Running the Docker container with the latest image on port
8080.
If Docker is not installed on the Azure server, the pipeline automatically installs it using a set of commands in the workflow.
To run the java application locally, follow these steps:
-
Clone the repository:
git clone https://github.com/rajeshecb70/java-project.git cd java-project/sysinfo -
Install dependencies:
make install
-
Build docker image:
make docker-build
-
Run the application using the docker images:
make docker-run
The application will be available at
http://localhost:8080/api/system/uptimehttp://localhost:8080/api/system/cpu-load
To update the Docker image version, modify the TAG environment variable in the .github/workflows/java-CI-Pipeline.yml file. For example, change the TAG value from 1.0.0 to a new version like 2.0.0:
env:
TAG: 3.2.0# CPU Metrics:
http://<Server_IP>:8080/api/system/uptime
# Uptime Metrics
http://<Server_IP>:8080/api/system/cpu-load


