Skip to content

Commit

Permalink
Merge pull request #118 from JewelleryManagement/feature-ci-cd
Browse files Browse the repository at this point in the history
Feature 88/ Add env specific configurations. Add AWS deployment files and workflow
  • Loading branch information
VladoKat authored May 11, 2024
2 parents 849feb4 + 5a7357d commit db30c4e
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .github/dockerfiles/Dockerfile_jms_be
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
FROM amazoncorretto:17-alpine-jdk
COPY ./app.jar .
ENTRYPOINT ["java", "-jar", "app.jar"]
ENV SPRING_PROFILE=local
ENTRYPOINT java -jar -Dspring.profiles.active=$SPRING_PROFILE app.jar
40 changes: 40 additions & 0 deletions .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Build and copy to S3

on:
push:
branches: [ "release*" ]

env:
AWS_REGION : eu-central-1

permissions:
id-token: write
contents: write

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
name: Checkout repository

- uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: ${{ secrets.IAMROLE_GITHUB }}
role-session-name: GithubAction
aws-region: ${{ env.AWS_REGION }}

- name: Push jar and docker files to AWS S3
run: |
zip -r backend_codedeploy.zip aws
aws s3 cp backend_codedeploy.zip s3://${{ secrets.S3BUCKET }}/
1 change: 0 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
name: build
on:
push:
pull_request:
types: [opened, synchronize, reopened]

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ build/
.gradle/

# Spring Boot properties
aws/docker/.env
.env
/src/main/resources/data.sql

Expand Down
18 changes: 18 additions & 0 deletions aws/appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 0.0
os: linux
files:
- source: /docker/
destination: /tmp/be
hooks:
BeforeInstall:
- location: scripts/install_docker.sh
timeout: 180
runas: root
ApplicationStart:
- location: scripts/start_docker.sh
timeout: 180
runas: root
ValidateService:
- location: scripts/check_health.sh
timeout: 180
runas: root
8 changes: 8 additions & 0 deletions aws/docker/Dockerfile_DB
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM postgres:13
ARG POSTGRES_DB
ARG POSTGRES_USER
ARG POSTGRES_PASSWORD

ENV POSTGRES_DB=${POSTGRES_DB}
ENV POSTGRES_USER=${POSTGRES_USER}
ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
50 changes: 50 additions & 0 deletions aws/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
version: '3.8'
name: jms-docker-be
services:
jms-be:
image: vladokat/jms-be:893b8dc
# build:
# context: .
# dockerfile: Dockerfile_app
container_name: jms-be
depends_on:
- db
environment:
SPRING_PROFILE: cloud
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/${JMS_DATABASE_NAME}?autoreconnect=true&createDatabaseIfNotExist=true&characterEncoding=utf8
JMS_DATABASE_NAME: ${JMS_DATABASE_NAME}
JMS_DATABASE_USER: ${JMS_DATABASE_USER}
JMS_DATABASE_PASSWORD: ${JMS_DATABASE_PASSWORD}
SECRET_KEY: ${SECRET_KEY}
ports:
- "8080:8080"
# volumes:
# - ./jewellery-inventory-0.0.1-SNAPSHOT.jar:/jar/app.jar
networks:
- jms-network
db:
# container_name: postgres
image: jms-db
build:
context: .
dockerfile: Dockerfile_DB
restart: always
environment:
POSTGRES_DB: ${JMS_DATABASE_NAME}
POSTGRES_USER: ${JMS_DATABASE_USER}
POSTGRES_PASSWORD: ${JMS_DATABASE_PASSWORD}
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
networks:
- jms-network

volumes:
postgres_data:

networks:
jms-network:
name: jms-network
driver: bridge

13 changes: 13 additions & 0 deletions aws/scripts/check_health.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Wait for the application to start (you may need to adjust this based on your app startup time)
sleep 20

# Perform the health check
if curl -s -o /dev/null -w "%{http_code}" http://3.123.158.214:8080/health | grep -q "200"; then
echo "Application is healthy."
exit 0 # Success
else
echo "Application health check failed."
exit 1 # Failure
fi
5 changes: 5 additions & 0 deletions aws/scripts/install_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

yum install -y docker #> /logs/install.log
curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
16 changes: 16 additions & 0 deletions aws/scripts/start_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# Set the environment variables for your application
JMS_DATABASE_NAME=$(aws ssm get-parameter --name JMS_DATABASE_NAME --query "Parameter.Value" --with-decryption --output text)
JMS_DATABASE_USER=$(aws ssm get-parameter --name JMS_DATABASE_USER --query "Parameter.Value" --with-decryption --output text)
JMS_DATABASE_PASSWORD=$(aws ssm get-parameter --name JMS_DATABASE_PASSWORD --query "Parameter.Value" --with-decryption --output text)
SECRET_KEY=$(aws ssm get-parameter --name SECRET_KEY --query "Parameter.Value" --with-decryption --output text)

export JMS_DATABASE_NAME="$JMS_DATABASE_NAME"
export JMS_DATABASE_USER="$JMS_DATABASE_USER"
export JMS_DATABASE_PASSWORD="$JMS_DATABASE_PASSWORD"
export SECRET_KEY="$SECRET_KEY"

service docker start
docker-compose build --no-cache --file /tmp/be/docker-compose.yml
docker-compose --file /tmp/be/docker-compose.yml up -d --build
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
build:
context: .
dockerfile: Dockerfile
restart: always
restart: on-failure
environment:
POSTGRES_DB: ${JMS_DATABASE_NAME}
POSTGRES_USER: ${JMS_DATABASE_USER}
Expand All @@ -18,4 +18,3 @@ services:

volumes:
postgres_data:

Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package jewellery.inventory.config;

import javax.sql.DataSource;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class FlywayMigrationConfig {

private static final String LOCATION = "db/migration/dev";

@Autowired
FlywayMigrationConfig(DataSource dataSource) {
public FlywayMigrationConfig(
DataSource dataSource,
@Value("${flyway.migration.location}") String flywayMigrationLocation) {
Flyway.configure()
.baselineOnMigrate(true)
.validateMigrationNaming(true)
.dataSource(dataSource)
.locations(LOCATION)
.locations(flywayMigrationLocation)
.load()
.migrate();
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application-cloud.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cors.allowedOrigins=*
spring.datasource.url=jdbc:postgresql://db:5432/${JMS_DATABASE_NAME}?autoreconnect=true&createDatabaseIfNotExist=true&characterEncoding=utf8
flyway.migration.location=classpath:db/migration/prod
4 changes: 1 addition & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ spring.datasource.username=${JMS_DATABASE_USER}
spring.datasource.password=${JMS_DATABASE_PASSWORD}
spring.sql.init.mode=always

spring.flyway.url=jdbc:postgresql://localhost:5432/${JMS_DATABASE_NAME}
spring.flyway.user=${JMS_DATABASE_USER}
spring.flyway.password=${JMS_DATABASE_PASSWORD}
spring.flyway.enabled=false
flyway.migration.location=classpath:db/migration/dev

# Specify the DB platform for Hibernate
spring.jpa.hibernate.ddl-auto=none
Expand Down
11 changes: 6 additions & 5 deletions src/main/resources/db/migration/prod/V1_1__init_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ CREATE TABLE IF NOT EXISTS public.product_author (

-- public.system_event definition
CREATE TABLE IF NOT EXISTS public.system_event (
id uuid NOT NULL,
"data" jsonb NULL,
"timestamp" timestamp(6) NULL,
"type" varchar(255) NULL,
CONSTRAINT system_event_pkey PRIMARY KEY (id)
id uuid NOT NULL,
timestamp timestamp with time zone,
type varchar(255),
executor jsonb,
payload jsonb,
PRIMARY KEY (id)
);
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest(webEnvironment = RANDOM_PORT)
@ActiveProfiles("test")
class JewelleryInventoryApplicationTests {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@ActiveProfiles("test")
abstract class AuthenticatedIntegrationTestBase {

@Autowired protected ObjectMapper objectMapper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
# Specify the DB platform for Hibernate
spring.datasource.url=jdbc:tc:postgresql:13:///
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver

# Show all queries Hibernate executes in console
spring.jpa.show-sql=true
cors.allowedOrigins=http://localhost:3000
cors.allowedMethods=GET,POST,PUT,DELETE
cors.allowedHeaders=Authorization,Cache-Control,Content-Type

jwt.secret.key=QdzigVY4XWNItestqpRdNuCGXx+FXok5e++GeMm1OlE=
jwt.token.expiration=#{1000 * 60 * 24}

spring.flyway.enabled=false
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false

## File Storage Properties
# All files uploaded through the API will be stored in this directory
image.folder.path=/tmp/Jms-test/Images/

# Max file size
spring.servlet.multipart.max-file-size=12MB
# Max Request Size
spring.servlet.multipart.max-request-size=12MB
multipart.file.max.size=8
image.folder.path=/tmp/Jms-test/Images/

0 comments on commit db30c4e

Please sign in to comment.