Skip to content

Commit

Permalink
Feat: add database (#129)
Browse files Browse the repository at this point in the history
* fix: merge main workflow name

* feat: add postgres blank database

jira task 221

* test: add application properties for test

* ci: add files and workflows for databse deploy

jira issue 221

* ci: fix workflow parameters

* ci: fix matrix name vars
  • Loading branch information
Ricardo Campos authored Nov 6, 2023
1 parent 9839c8d commit e679b74
Show file tree
Hide file tree
Showing 11 changed files with 456 additions and 20 deletions.
62 changes: 55 additions & 7 deletions .github/workflows/merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,43 @@ jobs:
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2

init-test:
name: TEST Init
environment: test
runs-on: ubuntu-22.04
steps:
- name: OpenShift Init
uses: bcgov-nr/[email protected]
with:
oc_namespace: ${{ vars.OC_NAMESPACE }}
oc_server: ${{ vars.OC_SERVER }}
oc_token: ${{ secrets.OC_TOKEN }}
file: common/openshift.init.yml
overwrite: false
parameters:
-p ZONE=test -p NAME=${{ github.event.repository.name }}

deploys-test:
name: TEST Deployments
needs: [init-test]
environment: test
runs-on: ubuntu-22.04
permissions:
issues: write
strategy:
matrix:
name: [backend, frontend]
name: [database, backend, frontend]
include:
- name: database
- name: backend
parameters:
-p PROMOTE=${{ github.repository }}/backend:test
verification_path: "actuator/health"
- name: frontend
parameters:
-p VITE_USER_POOLS_ID=${{ vars.VITE_USER_POOLS_ID }}
-p VITE_USER_POOLS_WEB_CLIENT_ID=${{ vars.VITE_USER_POOLS_WEB_CLIENT_ID }}
-p PROMOTE=${{ github.repository }}/frontend:test
steps:
- uses: bcgov-nr/[email protected]
with:
Expand All @@ -58,29 +80,51 @@ jobs:
oc_token: ${{ secrets.OC_TOKEN }}
overwrite: true
parameters:
-p ZONE=test -p PROMOTE=${{ github.repository }}/${{ matrix.name }}:test
-p ZONE=test
-p NAME=${{ github.event.repository.name }}
${{ matrix.parameters }}
penetration_test: true
penetration_test_artifact: ${{ matrix.name }}
penetration_test_issue: ${{ matrix.name }}
penetration_test_token: ${{ secrets.GITHUB_SECRET }}

init-prod:
name: PROD Init
needs: [deploys-test]
environment: prod
runs-on: ubuntu-22.04
steps:
- name: OpenShift Init
uses: bcgov-nr/[email protected]
with:
oc_namespace: ${{ vars.OC_NAMESPACE }}
oc_server: ${{ vars.OC_SERVER }}
oc_token: ${{ secrets.OC_TOKEN }}
file: common/openshift.init.yml
overwrite: false
parameters:
-p ZONE=prod -p NAME=${{ github.event.repository.name }}

deploys-prod:
name: PROD Deployments
needs: [deploys-test]
needs: [init-prod]
environment: prod
runs-on: ubuntu-22.04
strategy:
matrix:
name: [backend, frontend]
name: [database, backend, frontend]
include:
- name: database
- name: backend
parameters:
-p PROMOTE=${{ github.repository }}/backend:test
- name: frontend
parameters:
-p PROMOTE=${{ github.repository }}/frontend:test
paratemeters:
-p VITE_USER_POOLS_ID=${{ vars.VITE_USER_POOLS_ID }}
-p VITE_USER_POOLS_WEB_CLIENT_ID=${{ vars.VITE_USER_POOLS_WEB_CLIENT_ID }}
-p VITE_REDIRECT_SIGN_OUT="${{ vars.VITE_REDIRECT_SIGN_OUT }}"
- name: frontend
steps:
- uses: bcgov-nr/[email protected]
with:
Expand All @@ -90,19 +134,23 @@ jobs:
oc_token: ${{ secrets.OC_TOKEN }}
overwrite: true
parameters:
-p ZONE=prod -p PROMOTE=${{ github.repository }}/${{ matrix.name }}:test
-p ZONE=prod
-p NAME=${{ github.event.repository.name }}
${{ matrix.parameters }}

image-promotions:
name: Promote images to PROD
needs: [deploys-prod]
runs-on: ubuntu-22.04
permissions:
packages: write
strategy:
matrix:
component: [database, backend, frontend]
steps:
- uses: shrink/actions-docker-registry-tag@v3
with:
registry: ghcr.io
repository: ${{ github.repository }}/frontend
repository: ${{ github.repository }}/${{ matrix.component }}
target: test
tags: prod
44 changes: 36 additions & 8 deletions .github/workflows/pr-open.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,32 @@ jobs:
Once merged, code will be promoted and handed off to following workflow run.
[Main Merge Workflow](https://github.com/${{ github.repository }}/actions/workflows/merge-main.yml)
- name: OpenShift Init
uses: bcgov-nr/[email protected]
with:
oc_namespace: ${{ vars.OC_NAMESPACE }}
oc_server: ${{ vars.OC_SERVER }}
oc_token: ${{ secrets.OC_TOKEN }}
file: common/openshift.init.yml
overwrite: false
parameters:
-p ZONE=${{ github.event.number }}
-p NAME=${{ github.event.repository.name }}
triggers: ('common/' 'database/' 'backend/' 'frontend/')

builds:
name: Builds
needs: [pr-greeting]
if: "!github.event.pull_request.head.repo.fork"
runs-on: ubuntu-22.04
permissions:
packages: write
strategy:
matrix:
name: [backend, frontend]
name: [database, backend, frontend]
include:
- package: database
triggers: ('database/')
- name: backend
triggers: ('backend/')
- name: frontend
Expand All @@ -64,22 +80,35 @@ jobs:
deploys:
name: Deploys
if: "!github.event.pull_request.head.repo.fork"
needs: [builds]
needs: [builds, pr-greeting]
runs-on: ubuntu-22.04
strategy:
matrix:
name: [backend, frontend]
name: [database, backend, frontend]
include:
- name: database
file: database/openshift.deploy.yml
parameters:
-p DB_PVC_SIZE=128Mi
overwrite: false
triggers: ('common/' 'database/' 'backend/' 'frontend/')
- name: backend
file: backend/openshift.deploy.yml
triggers: ('backend/' 'frontend/')
triggers: ('common/' 'database/' 'backend/' 'frontend/')
verification_path: /actuator/health
parameters:
-p PROMOTE=${{ github.repository }}/backend:${{ github.event.number }}
-p MIN_REPLICAS=1
-p MAX_REPLICAS=2
- name: frontend
file: frontend/openshift.deploy.yml
triggers: ('backend/' 'frontend/')
triggers: ('common/' 'database/' 'backend/' 'frontend/')
parameters:
-p VITE_USER_POOLS_ID=${{ vars.VITE_USER_POOLS_ID }}
-p VITE_USER_POOLS_WEB_CLIENT_ID=${{ vars.VITE_USER_POOLS_WEB_CLIENT_ID }}
-p PROMOTE=${{ github.repository }}/frontend:${{ github.event.number }}
-p MIN_REPLICAS=1
-p MAX_REPLICAS=2
steps:
- uses: bcgov-nr/[email protected]
with:
Expand All @@ -89,9 +118,8 @@ jobs:
oc_token: ${{ secrets.OC_TOKEN }}
overwrite: true
parameters:
-p ZONE=${{ github.event.number }} -p NAME=${{ github.event.repository.name }}
-p PROMOTE=${{ github.repository }}/${{ matrix.name }}:${{ github.event.number }}
-p MIN_REPLICAS=1 -p MAX_REPLICAS=2
-p ZONE=${{ github.event.number }}
-p NAME=${{ github.event.repository.name }}
${{ matrix.parameters }}
triggers: ${{ matrix.triggers }}
verification_path: ${{ matrix.verification_path }}
43 changes: 38 additions & 5 deletions backend/openshift.deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: Template
parameters:
- name: NAME
description: Module name
value: quickstart-openshift-backends
value: nr-silva
- name: COMPONENT
description: Component name
value: backend
Expand Down Expand Up @@ -38,6 +38,15 @@ parameters:
- name: PROMOTE
description: Image (namespace/name:tag) to promote/import
value: bcgov/quickstart-openshift-backends/backend:test
- name: DB_POOL_CONN_TIMEOUT
description: Maximum number of milliseconds that a client will wait for a connection from the pool.
value: "90000"
- name: DB_POOL_IDLE_TIMEOUT
description: Maximum amount of milliseconds that a connection is allowed to sit idle in the pool.
value: "0"
- name: DB_POOL_MAX_LIFETIME
description: Maximum lifetime of a connection in the pool.
value: "1800000"
objects:
- apiVersion: v1
kind: ImageStream
Expand Down Expand Up @@ -87,6 +96,30 @@ objects:
- image: "${NAME}-${ZONE}-${COMPONENT}:${IMAGE_TAG}"
imagePullPolicy: Always
name: "${NAME}"
env:
- name: POSTGRES_HOST
value: ${NAME}-${ZONE}-database
- name: POSTGRES_DB
valueFrom:
secretKeyRef:
name: ${NAME}-${ZONE}-database
key: database-name
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: ${NAME}-${ZONE}-database
key: database-password
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: ${NAME}-${ZONE}-database
key: database-user
- name: DB_POOL_CONN_TIMEOUT
value: ${DB_POOL_CONN_TIMEOUT}
- name: DB_POOL_IDLE_TIMEOUT
value: ${DB_POOL_IDLE_TIMEOUT}
- name: DB_POOL_MAX_LIFETIME
value: ${DB_POOL_MAX_LIFETIME}
ports:
- containerPort: 8080
protocol: TCP
Expand All @@ -103,13 +136,13 @@ objects:
port: 8080
scheme: HTTP
initialDelaySeconds: 5
periodSeconds: 2
timeoutSeconds: 2
periodSeconds: 5
timeoutSeconds: 10
successThreshold: 1
failureThreshold: 30
failureThreshold: 5
livenessProbe:
successThreshold: 1
failureThreshold: 3
failureThreshold: 5
httpGet:
path: /actuator/health
port: 8080
Expand Down
10 changes: 10 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- Database -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>

<!-- Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
19 changes: 19 additions & 0 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,22 @@ springdoc.enable-native-support = true

# https://github.com/quarkusio/quarkus/wiki/Migration-Guide-3.2
quarkus.native.additional-build-args=-march=x86-64-v2

# Database, and JPA
spring.datasource.driver-class-name = org.postgresql.Driver
spring.datasource.url = jdbc:postgresql://${POSTGRES_HOST:localhost}:5432/${POSTGRES_DB:postgres}
spring.datasource.username = ${POSTGRES_USER:postgres}
spring.datasource.password = ${POSTGRES_PASSWORD:default}
spring.datasource.hikari.connectionTimeout = ${DB_POOL_CONN_TIMEOUT:90000}
spring.datasource.hikari.idleTimeout = ${DB_POOL_IDLE_TIMEOUT:45000}
spring.datasource.hikari.maxLifetime = ${DB_POOL_MAX_LIFETIME:60000}
spring.datasource.hikari.keepaliveTime = 30000
spring.datasource.hikari.poolName = NrSparDbPool
spring.datasource.hikari.minimumIdle = 1
spring.datasource.hikari.maximumPoolSize = 3
spring.jpa.properties.hibernate.default_schema = silva
spring.jpa.database-platform = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.defer-datasource-initialization=true
spring.sql.init.mode=always
11 changes: 11 additions & 0 deletions backend/src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE SCHEMA IF NOT EXISTS silva;

-- Example
CREATE TABLE IF NOT EXISTS silva.person (
id INT NOT NULL,
name VARCHAR(30) NOT NULL,
CONSTRAINT person_pk
PRIMARY KEY(id)
);

DROP TABLE silva.person;
13 changes: 13 additions & 0 deletions backend/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Server and application
spring.profiles.active = dev

# Database, datasource and JPA
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:silvadb;DB_CLOSE_DELAY=-1;NON_KEYWORDS=USER;DB_CLOSE_ON_EXIT=true
spring.datasource.username=result
spring.datasource.password=password
spring.datasource.initialization-mode=always
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.generate-ddl = true
spring.jpa.hibernate.ddl-auto = create-drop
Loading

0 comments on commit e679b74

Please sign in to comment.