Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ COPY infrastructure/src infrastructure/src
COPY presentation/src presentation/src

RUN --mount=type=cache,target=/root/.gradle \
./gradlew --no-daemon :bootstrap:bootJar && \
JAR_PATH="$(find /workspace/bootstrap/build/libs -maxdepth 1 -type f -name '*.jar' ! -name '*-plain.jar' | head -n 1)" && \
./gradlew --no-daemon :bootstrap:bootJar :bootstrap:migrationBootJar && \
JAR_PATH="$(find /workspace/bootstrap/build/libs -maxdepth 1 -type f -name '*.jar' ! -name '*-plain.jar' ! -name '*-migration.jar' | head -n 1)" && \
test -n "$JAR_PATH" && \
cp "$JAR_PATH" /workspace/application.jar && \
MIGRATION_JAR_PATH="$(find /workspace/bootstrap/build/libs -maxdepth 1 -type f -name '*-migration.jar' | head -n 1)" && \
test -n "$MIGRATION_JAR_PATH" && \
cp "$MIGRATION_JAR_PATH" /workspace/migration.jar && \
java -Djarmode=tools -jar /workspace/application.jar extract --layers --destination /workspace/extracted

FROM eclipse-temurin:21-jre-jammy@sha256:fcf98f8a669c2778b2a1a145c7dac92a1f8fc71e967734bd2a749d2f42572db1
Expand All @@ -46,6 +49,7 @@ COPY --chown=spring:spring --from=builder /workspace/extracted/dependencies/ ./
COPY --chown=spring:spring --from=builder /workspace/extracted/spring-boot-loader/ ./
COPY --chown=spring:spring --from=builder /workspace/extracted/snapshot-dependencies/ ./
COPY --chown=spring:spring --from=builder /workspace/extracted/application/ ./
COPY --chown=spring:spring --from=builder /workspace/migration.jar /app/migration.jar

EXPOSE 8080

Expand Down
11 changes: 11 additions & 0 deletions bootstrap/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import org.springframework.boot.gradle.tasks.bundling.BootJar
import org.gradle.api.JavaVersion

dependencies {
implementation project(':application')
implementation project(':common')
Expand All @@ -21,3 +24,11 @@ dependencies {
testImplementation project(':domain')
testRuntimeOnly 'com.h2database:h2'
}

tasks.register("migrationBootJar", BootJar) {
group = "build"
archiveClassifier = "migration"
mainClass = "com.project.authmigration.MigrationApplication"
classpath = sourceSets.main.runtimeClasspath
targetJavaVersion = JavaVersion.VERSION_21
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.project.authmigration;

import com.project.auth.config.persistence.MigrationProperties;
import jakarta.annotation.PostConstruct;
import javax.sql.DataSource;
import org.flywaydb.core.Flyway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@SpringBootConfiguration
@EnableAutoConfiguration
@EnableConfigurationProperties(MigrationProperties.class)
@Import(MigrationApplication.MigrationConfiguration.class)
public class MigrationApplication {

public static void main(String[] args) {
SpringApplicationBuilder builder =
new SpringApplicationBuilder(MigrationApplication.class);
builder
.web(WebApplicationType.NONE)
.properties(
"spring.autoconfigure.exclude="
+ "org.springframework.boot.hibernate.autoconfigure.HibernateJpaAutoConfiguration,"
+ "org.springframework.boot.data.jpa.autoconfigure.DataJpaRepositoriesAutoConfiguration"
)
.run(args);
}

@Configuration
static class MigrationConfiguration {

@Bean
Flyway flyway(DataSource dataSource, MigrationProperties migrationProperties) {
return Flyway.configure()
.dataSource(dataSource)
.locations(migrationProperties.location())
.load();
}

@Bean
MigrationRunner migrationRunner(
Flyway flyway,
MigrationProperties migrationProperties,
ConfigurableApplicationContext applicationContext
) {
return new MigrationRunner(flyway, migrationProperties, applicationContext);
}
}

static class MigrationRunner {

private final Flyway flyway;
private final MigrationProperties migrationProperties;
private final ConfigurableApplicationContext applicationContext;

MigrationRunner(
Flyway flyway,
MigrationProperties migrationProperties,
ConfigurableApplicationContext applicationContext
) {
this.flyway = flyway;
this.migrationProperties = migrationProperties;
this.applicationContext = applicationContext;
}

@PostConstruct
void runMigration() {
if (migrationProperties.runOnStartup()) {
flyway.migrate();
}

int exitCode = SpringApplication.exit(applicationContext, () -> 0);
System.exit(exitCode);
}
}
}
28 changes: 23 additions & 5 deletions k8s/dev/db-migration-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,34 @@ spec:
- name: auth-db-migration
image: ghcr.io/donghyeonka/project-auth-server
imagePullPolicy: IfNotPresent
envFrom:
- configMapRef:
name: auth-server-config
- secretRef:
name: auth-server-secret
command:
- java
- -jar
- /app/migration.jar
env:
- name: SPRING_MAIN_WEB_APPLICATION_TYPE
value: none
- name: APP_PERSISTENCE_MIGRATION_RUN_ON_STARTUP
value: "true"
- name: APP_PERSISTENCE_MIGRATION_LOCATION
value: classpath:db/migration
- name: APP_DATASOURCE_URL
valueFrom:
configMapKeyRef:
name: auth-server-config
key: APP_DATASOURCE_URL
- name: APP_DATASOURCE_USERNAME
valueFrom:
secretKeyRef:
name: auth-server-secret
key: APP_DATASOURCE_USERNAME
- name: APP_DATASOURCE_PASSWORD
valueFrom:
secretKeyRef:
name: auth-server-secret
key: APP_DATASOURCE_PASSWORD
- name: APP_DATASOURCE_DRIVER_CLASS_NAME
value: org.postgresql.Driver
securityContext:
allowPrivilegeEscalation: false
capabilities:
Expand Down