Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flyway migrate, validate 용도의 Jenkinsfile 구분 #52

Merged
merged 5 commits into from
May 20, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 88 additions & 0 deletions Jenkinsfile-db-migration
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
enum Stage {
ALL, INFO, MIGRATE, VALIDATE;
Stage() {}
}

def executeConditionAllOr(stage) {
return params.FLYWAY_COMMAND == Stage.ALL.name() || params.FLYWAY_COMMAND == stage.name()
}

// Run flyway runner Jenkins Plugin
def executeFlywayRunner(stage) {
if (stage == Stage.ALL) {
return
}

flywayrunner installationName: 'flywaytool-jenkins',
flywayCommand: "$stage".toLowerCase(),
commandLineArgs: "-configFiles=${MIGRATION_WORKSPACE}/flyway-${PROJECT_PROFILE}.conf",
credentialsId: "${DB_CONNECTION_CREDENTIAL}",
url: "jdbc:mysql://${DATABASE_HOST}:3306/dongne",
locations: "filesystem:${MIGRATION_WORKSPACE}/migration"
}

pipeline {
agent any

// Disallow concurrent executions of the Pipeline.
options { disableConcurrentBuilds() }

parameters {
// Choice of flyway target command
choice(
name: 'FLYWAY_COMMAND',
choices: ["${Stage.ALL}", "${Stage.INFO}", "${Stage.MIGRATE}", "${Stage.VALIDATE}"],
description: 'target flyway command'
)
}

stages {
stage('Init') {
steps {
script {
sh 'whoami'
sh 'printenv'

PROJECT_PROFILE = 'prod'

// DB Connection Credential
DB_CONNECTION_CREDENTIAL = 'b873f9bf-03cc-4daf-be4f-7e00194aa2a0'
withCredentials([
usernamePassword(
credentialsId: "${DB_CONNECTION_CREDENTIAL}",
usernameVariable: 'username',
passwordVariable: 'password'
)
]) {
DATABASE_USERNAME = "${username}"
DATABASE_PASSWORD = "${password}"
}

// flyway migration directory
MIGRATION_WORKSPACE = "${WORKSPACE}/db"
}
}
}

stage('DB Version Info') {
when {
expression { executeConditionAllOr(Stage.INFO) }
}
steps { executeFlywayRunner(Stage.INFO) }
}

stage('DB Migrate') {
when {
expression { executeConditionAllOr(Stage.MIGRATE) }
}
steps { executeFlywayRunner(Stage.MIGRATE) }
}

stage('DB Validate') {
when {
expression { executeConditionAllOr(Stage.VALIDATE) }
}
steps { executeFlywayRunner(Stage.VALIDATE) }
}
}
}
39 changes: 18 additions & 21 deletions Jenkinsfile-service
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
enum Stage {
ALL, DB_VALIDATE, TEST, BUILD, RUN_APP;
Stage() {}
}

def executeConditionAllOr(stage) {
return params.PIPELINE_STAGE_TARGET == Stage.ALL.name() || params.PIPELINE_STAGE_TARGET == stage.name()
}

pipeline {
agent any

Expand All @@ -8,7 +17,7 @@ pipeline {
// Choice of pipeline target stages
choice(
name: 'PIPELINE_STAGE_TARGET',
choices: ['ALL', 'DB_VALIDATE', 'TEST', 'BUILD', 'RUN_APPLICATION'],
choices: ["${Stage.ALL}", "${Stage.DB_VALIDATE}", "${Stage.TEST}", "${Stage.BUILD}", "${Stage.RUN_APP}"],
description: 'set target pipeline stages'
)
}
Expand All @@ -20,7 +29,7 @@ pipeline {
sh 'whoami'
sh 'printenv'

SPRING_PROFILE = 'prod'
PROJECT_PROFILE = 'prod'
MODULE_NAME = 'dongne-service-api'

// DB Connection Credential
Expand Down Expand Up @@ -48,15 +57,12 @@ pipeline {

stage('DB Validate') {
when {
anyOf {
expression { params.PIPELINE_STAGE_TARGET == 'ALL' }
expression { params.PIPELINE_STAGE_TARGET == 'DB_VALIDATE' }
}
expression { executeConditionAllOr(Stage.DB_VALIDATE) }
}
steps {
flywayrunner installationName: 'flywaytool-jenkins',
flywayCommand: 'info validate',
commandLineArgs: "-configFiles=${MIGRATION_WORKSPACE}/flyway-${SPRING_PROFILE}.conf",
commandLineArgs: "-configFiles=${MIGRATION_WORKSPACE}/flyway-${PROJECT_PROFILE}.conf",
credentialsId: "${DB_CONNECTION_CREDENTIAL}",
url: "jdbc:mysql://${DATABASE_HOST}:3306/dongne",
locations: "filesystem:${MIGRATION_WORKSPACE}/migration"
Expand All @@ -65,10 +71,7 @@ pipeline {

stage('Test') {
when {
anyOf {
expression { params.PIPELINE_STAGE_TARGET == 'ALL' }
expression { params.PIPELINE_STAGE_TARGET == 'TEST' }
}
expression { executeConditionAllOr(Stage.TEST) }
}
steps {
sh "./gradlew clean :${MODULE_NAME}:test"
Expand All @@ -77,10 +80,7 @@ pipeline {

stage('Build') {
when {
anyOf {
expression { params.PIPELINE_STAGE_TARGET == 'ALL' }
expression { params.PIPELINE_STAGE_TARGET == 'BUILD' }
}
expression { executeConditionAllOr(Stage.BUILD) }
}
steps {
sh "./gradlew clean :${MODULE_NAME}:build -x test"
Expand All @@ -89,7 +89,7 @@ pipeline {

stage('Deploy') {
when {
expression { params.PIPELINE_STAGE_TARGET == 'ALL' }
expression { executeConditionAllOr(Stage.ALL) }
}
steps {
echo 'Deploy JAR file'
Expand Down Expand Up @@ -122,18 +122,15 @@ pipeline {

stage('Run Application with Ansible') {
when {
anyOf {
expression { params.PIPELINE_STAGE_TARGET == 'ALL' }
expression { params.PIPELINE_STAGE_TARGET == 'RUN_APPLICATION' }
}
expression { executeConditionAllOr(Stage.RUN_APP) }
}
steps {
ansiblePlaybook inventory: "${ANSIBLE_INVENTORY}",
playbook: "${ANSIBLE_PLAYBOOK}",
extraVars: [
module_name: "${MODULE_NAME}",
jenkins_user_home: "${HOME}",
spring_profile: "${SPRING_PROFILE}",
spring_profile: "${PROJECT_PROFILE}",
database_host: "${DATABASE_HOST}",
database_username: "${DATABASE_USERNAME}",
database_password: "${DATABASE_PASSWORD}"
Expand Down
2 changes: 1 addition & 1 deletion scripts/deploy/playbook/templates/service.sh.j2
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

ENV_LOCATION={{ remote_env_path }}
ENV_LOCATION={{ remote_env_path }}