diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..f6ad863 --- /dev/null +++ b/.env.example @@ -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 diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 988ce4c..d11fbf8 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -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 \ No newline at end of file + ghcr.io/stevensblueprint/blueprint_admin_backend:latest diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index bd8daf8..459db57 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -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: diff --git a/Dockerfile b/Dockerfile index 5b60a28..1f67f0a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] \ No newline at end of file +ENTRYPOINT ["java", "-jar", "app.jar"] diff --git a/README.md b/README.md index 409485c..cb4977e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docker-compose.prod.yaml b/docker-compose.prod.yaml new file mode 100644 index 0000000..9c22425 --- /dev/null +++ b/docker-compose.prod.yaml @@ -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 \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index daa42cb..7ceb8b1 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -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 diff --git a/initdb_prod/init.sql b/initdb_prod/init.sql new file mode 100644 index 0000000..d24328e --- /dev/null +++ b/initdb_prod/init.sql @@ -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); \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 85d3825..ebfa2ff 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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 \ No newline at end of file