Skip to content

Rohit1024/jib-run-deploy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jib Run Deploy

This project demonstrates a Spring Boot application containerized using Jib, connecting to Google Cloud SQL for PostgreSQL with IAM (passwordless) authentication.

Features

  • Spring Boot Application: A RESTful API built with Spring Boot.
  • Containerization with Jib: Easily build optimized Docker images for the Spring Boot application without a Dockerfile.
  • Cloud SQL for PostgreSQL Integration: Connects to a PostgreSQL database hosted on Google Cloud SQL.
  • IAM (Passwordless) Authentication: Utilizes Google Cloud IAM for secure, passwordless authentication to Cloud SQL, enhancing security and simplifying credential management.
  • RESTful API: Provides endpoints for managing employee data.

Technologies Used

  • Java 21
  • Spring Boot (version 4.0.6)
  • Spring Cloud GCP (version 8.0.2)
  • Jib Maven Plugin
  • PostgreSQL
  • Lombok
  • ModelMapper

Getting Started

Prerequisites

  • Java Development Kit (JDK) 21
  • Maven
  • Google Cloud SDK (gcloud CLI) configured with appropriate permissions to access Cloud SQL.
  • A Google Cloud Project with Cloud SQL for PostgreSQL instance enabled.
  • Ensure the service account or user running the application has the Cloud SQL Client role.

Building the Application

To build the Spring Boot application, run:

mvn clean install

Containerizing with Jib

Jib is configured in the pom.xml to build a Docker image. To build the image and push it to a container registry (e.g., Google Container Registry or Docker Hub), use the following command:

mvn compile jib:build

Before running this command, ensure you are authenticated to your container registry. For Google Container Registry (GCR) or Artifact Registry, you can authenticate using:

gcloud auth configure-docker

The pom.xml specifies gcr.io/distroless/java21-debian12 as the base image for a minimal and secure runtime environment.

Running the Application

Local Development

You can run the application locally using Maven:

mvn spring-boot:run

For local testing with a PostgreSQL database, you might need to configure application.properties or application.yaml with local database credentials.

Connecting to Cloud SQL (IAM Authentication)

The application uses spring-cloud-gcp-starter-sql-postgresql for seamless integration with Cloud SQL. For IAM authentication, ensure your application's environment (e.g., Google Kubernetes Engine, Cloud Run, or a VM with a service account) has the necessary permissions (Cloud SQL Client role).

The connection string in application.properties typically looks like this for IAM authentication:

spring.datasource.url=jdbc:postgresql:///database-name?cloudSqlInstance=project-id:region:instance-name&socketFactory=com.google.cloud.sql.postgres.SocketFactory&enableIamAuth=true
spring.datasource.username=service-account-email-or-user-email

Replace database-name, project-id, region, instance-name, and service-account-email-or-user-email with your actual Cloud SQL details.

API Endpoints

The application exposes the following RESTful endpoints for managing employees:

Employee Management (/employees)

  • GET /employees/

    • Description: Retrieves a list of all employees.
    • Response: 200 OK with a list of EmployeeDTO objects.
  • GET /employees/find/{id}

    • Description: Retrieves a single employee by their ID.
    • Parameters:
      • id (Path Variable, Long): The ID of the employee to retrieve.
    • Response: 200 OK with an EmployeeDTO object if found, 404 Not Found otherwise.
  • POST /employees/add

    • Description: Adds a new employee to the database.
    • Request Body: EmployeeDTO object containing employee details.
    • Response: 201 Created with the newly added EmployeeDTO object.
  • PUT /employees/update/{id}

    • Description: Updates an existing employee's details.
    • Parameters:
      • id (Path Variable, Long): The ID of the employee to update.
    • Request Body: EmployeeDTO object with updated employee details.
    • Response: 200 OK with the updated EmployeeDTO object.
  • DELETE /employees/delete/{id}

    • Description: Deletes an employee from the database by their ID.
    • Parameters:
      • id (Path Variable, Long): The ID of the employee to delete.
    • Response: 204 No Content if successful.

Project Structure

.
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── run
│   │   │           └── deploy
│   │   │               ├── controllers
│   │   │               │   └── EmployeeController.java
│   │   │               ├── dto
│   │   │               ├── exceptions
│   │   │               ├── models
│   │   │               ├── repository
│   │   │               ├── service
│   │   │               └── JibRunDeployApplication.java
│   │   └── resources
│   │       └── application.properties
│   └── test
├── pom.xml
└── README.md

About

Spring Boot application containerized using Jib, connecting to Google Cloud SQL for PostgreSQL with IAM (passwordless) authentication.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages