Skip to content

Commit

Permalink
Dockerfile (#14)
Browse files Browse the repository at this point in the history
* fix: Dockerfile

* feat: Docker compose env variables

* docs: Added how to add env variables

* fix: Controller error

* docs: Added command to run application from terminal

* feat: modified dockerfile

* added variables to env file

* modified docker workflow with new Dockerfile

* Modififed Dockerfile and Dockercompose

* fix: removed db call

* format: readme

* build java app in container
  • Loading branch information
miguel-merlin authored Nov 23, 2024
1 parent a25217d commit b8f3f80
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 29 deletions.
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
POSTGRES_USER=blueprint_admin_backend
POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres
SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/postgres
SPRING_DATASOURCE_USERNAME=blueprint_admin_backend
SPRING_DATASOURCE_PASSWORD=postgres
SPRING_PROFILES_ACTIVE=dev
26 changes: 21 additions & 5 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,33 @@ jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'temurin'
- name: Cache Gradle packages
uses: actions/cache@v3
with:
path: ~/.gradle/caches
key: gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
gradle-
- name: Build with gradle
run: ./gradlew clean bootJar
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: "jli198"
password: ${{ secrets.BLUEPRINT_ADMIN_BACKEND }}

- name: Build and Push Docker Image
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: |
ghcr.io/stevensblueprint/blueprint_admin_backend:${{ github.sha }}
ghcr.io/stevensblueprint/blueprint_admin_backend:latest
ghcr.io/stevensblueprint/blueprint_admin_backend:latest
7 changes: 0 additions & 7 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ jobs:
sudo apt-get install -y docker-compose
- name: Start PostgreSQL with Docker Compose
run: docker-compose up -d
- name: Perform Database Operation
run: |
echo "Waiting for PostgreSQL to be ready..."
until docker exec $(docker-compose ps -q postgres) pg_isready; do
sleep 1;
done
echo "PostgreSQL is ready."
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
Expand Down
20 changes: 9 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
FROM gradle:8.7.0-jdk17 AS build
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle build --no-daemon -x test
FROM gradle:8.5-jdk17 AS builder

FROM openjdk:8-jre-slim
WORKDIR /app
COPY build.gradle settings.gradle ./
COPY src ./src
RUN gradle clean build --no-daemon

FROM openjdk:17
WORKDIR /app
COPY --from=builder /app/build/libs/*.jar app.jar
EXPOSE 8080

RUN mkdir /app

COPY --from=build /home/gradle/src/build/libs/*.jar /app/spring-boot-application.jar

ENTRYPOINT ["java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Djava.security.egd=file:/dev/./urandom","-jar","/app/spring-boot-application.jar"]
ENTRYPOINT ["java", "-jar", "app.jar"]
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@
Spring Java application that hosts the backend for our Blueprint Admin application. This project will help us manage Blueprint members.

## Running the service
Copy the environment variables from .env.example
```
cp .env.example .env
```
Initialize docker container with postgres service. If you don't have docker installed you can install [here](https://docs.docker.com/engine/install/).
```
docker-compose up -d
```
Run the application. (You have to run the ```admin/BlueprintAdmin.java``` this is the entry point of the Spring application).
Else, you can use the following command to run the Spring application:
```
./gradlew bootRun
```
You should see the following output in your terminal
```
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path ''
com.sitblueprint.admin.BlueprintAdmin : Started BlueprintAdmin in 2.255 seconds (process running for 2.392)
```

You can also run the server directly from the terminal

Expand Down
32 changes: 32 additions & 0 deletions docker-compose.prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
services:
blueprint_admin_backend:
env_file: ".env"
build:
context: .
dockerfile: Dockerfile
environment:
- DATABASE_URL=jdbc:postgresql://postgres:5432/${POSTGRES_DB}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
ports:
- '8080:8080'
depends_on:
- postgres

postgres:
env_file: ".env"
image: postgres:latest
restart: always
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
logging:
options:
max-size: "10m"
max-file: "3"
ports:
- '5432:5432'
volumes:
- ./postgres-data:/var/lib/postgresql/data
- ./initdb_prod:/docker-entrypoint-initdb.d
6 changes: 3 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ services:
image: postgres:latest
restart: always
environment:
- POSTGRES_USER=blueprint_admin_backend
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
logging:
options:
max-size: 10m
Expand Down
75 changes: 75 additions & 0 deletions initdb_prod/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
-- Create roles table
CREATE TABLE roles (
id BIGINT GENERATED ALWAYS AS IDENTITY NOT NULL,
name VARCHAR(255) NOT NULL,
PRIMARY key(id)
);

-- Insert roles into roles table
INSERT INTO roles (name) VALUES
('E-BOARD'),
('TEAM_LEAD'),
('DEVELOPER'),
('TECH_TEAM'),
('PROJECT_MANAGER');

-- Create members table
CREATE TABLE members (
id BIGINT GENERATED ALWAYS AS IDENTITY,
team_id BIGINT,
name VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE,
password VARCHAR(255) NOT NULL,
is_active BOOLEAN,
date_joined TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id)
);

-- Create organizations table
CREATE TABLE organizations (
id BIGINT GENERATED ALWAYS AS IDENTITY NOT NULL,
name VARCHAR(255) NOT NULL,
team_lead_id BIGINT,
project_manager_id BIGINT,
PRIMARY KEY (id),
FOREIGN KEY (team_lead_id) REFERENCES members(id),
FOREIGN KEY (project_manager_id) REFERENCES members(id)
);

-- Create teams table
CREATE TABLE teams (
id BIGINT GENERATED ALWAYS AS IDENTITY NOT NULL,
organization_id BIGINT NOT NULL,
name VARCHAR(255),
team_lead_id BIGINT,
project_manager_id BIGINT,
designer_id BIGINT,
date_created TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (organization_id) REFERENCES organizations(id),
FOREIGN KEY (team_lead_id) REFERENCES members(id),
FOREIGN KEY (project_manager_id) REFERENCES members(id),
FOREIGN KEY (designer_id) REFERENCES members(id)
);

-- Create member_roles table (to handle many-to-many relationship between members and roles)
CREATE TABLE member_roles (
member_id BIGINT NOT NULL,
role_id BIGINT NOT NULL,
PRIMARY KEY (member_id, role_id),
FOREIGN KEY (member_id) REFERENCES members(id),
FOREIGN KEY (role_id) REFERENCES roles(id)
);

--Create blogs table
CREATE TABLE blogs (
id BIGINT GENERATED ALWAYS AS IDENTITY,
author VARCHAR(255) NOT NULL,
title VARCHAR(255) NOT NULL,
date_created TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id)
);

ALTER TABLE members
ADD CONSTRAINT fk_team_id FOREIGN KEY (team_id) REFERENCES teams(id);
8 changes: 5 additions & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ spring.jpa.hibernate.ddl-auto=update

spring.jpa.properties.hibernate.format_sql=true

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=blueprint_admin_backend
spring.datasource.password=postgres
spring.datasource.url=${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/postgres}
spring.datasource.username=${SPRING_DATASOURCE_USERNAME:blueprint_admin_backend}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD:postgres}

spring.profiles.active=${SPRING_PROFILES_ACTIVE:dev}

blueprint_yaml.api.baseurl = https://auth.api.sitblueprint.com/api/v1

0 comments on commit b8f3f80

Please sign in to comment.