Skip to content

Instantly share code, notes, and snippets.

@ealebed
Created March 27, 2020 10:10
Show Gist options
  • Save ealebed/b83fa0f781713181e8f423320a56103b to your computer and use it in GitHub Desktop.
Save ealebed/b83fa0f781713181e8f423320a56103b to your computer and use it in GitHub Desktop.
Flyway migration validations (free, open-source version)
version: '3.4'
x-template: &flyway-template
image: flyway/flyway:6.3.2
depends_on:
- db
services:
db:
container_name: postgres
image: postgres:12.1-alpine
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- "5432:5432"
healthcheck:
test: psql -c'SELECT 1;' postgres postgres && echo 'cool, it works'
start_period: 10s
interval: 10s
timeout: 10s
retries: 5
volumes:
- ./init-user-db.sh:/docker-entrypoint-initdb.d/init-user-db.sh
flyway-migrate-dmp:
<<: *flyway-template
volumes:
- ./migrations/dmp:/flyway/sql
command: flyway -url=jdbc:postgresql://postgres:5432/dmp?ssl=require -user=postgres -password=postgres -connectRetries=60 migrate
flyway-migrate-lsm:
<<: *flyway-template
volumes:
- ./migrations/lsm:/flyway/sql
command: flyway -url=jdbc:postgresql://postgres:5432/lsm?ssl=require -user=postgres -password=postgres -connectRetries=60 migrate
#!/bin/bash
set -e
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER ro_user;
CREATE USER lsm;
CREATE USER dmp;
CREATE USER flyway;
CREATE USER test;
CREATE DATABASE dmp;
CREATE DATABASE lsm;
EOSQL
pipeline {
agent any
stages {
stage("Prepare environment") {
steps {
script {
sh "docker-compose -f docker-compose.yaml down --remove-orphans --volumes"
}
}
}
stage("Up postgres DB") {
steps {
script {
sh "docker-compose -f docker-compose.yaml up -d db"
}
}
}
stage("Validate migrations") {
steps {
script {
sh "docker-compose -f docker-compose.yaml up flyway-migrate-dmp"
sh "docker-compose -f docker-compose.yaml up flyway-migrate-lsm"
}
}
}
stage('Set build status') {
steps {
script {
env.EXIT_STATUS_COUNT = sh(script: '''docker-compose -f docker-compose.yaml ps -q | xargs docker inspect -f '{{ .State.ExitCode }}' | grep -v '^0' | wc -l''', returnStdout: true).trim()
if (0 == "${env.EXIT_STATUS_COUNT}".toInteger()) {
currentBuild.result = 'SUCCESS'
} else {
currentBuild.result = 'FAILURE'
}
currentBuild.description = "${currentBuild.result}".toLowerCase()
env.BUILD_STATUS = "${currentBuild.result}".toLowerCase()
}
}
post {
always {
slackSend (
channel: "#db-migrations", \
message: "Validation flyway migrations is *${env.BUILD_STATUS}* \n (${env.BUILD_URL}) \n"
)
sh '''
curl --silent -X POST \
-H "Authorization: token ${TOKEN}" \
https://api.github.com/repos/${ORG}/${REPO}/statuses/${GITHUB_PR_HEAD_SHA} \
-d '{
"state": "'${BUILD_STATUS}'",\
"context": "'${JOB_NAME}'", \
"target_url": "'${BUILD_URL}'", \
"description": "The build '${BUILD_NUMBER}' is '${BUILD_STATUS}'!"
}'
'''
}
}
}
stage("Cleanup environment") {
steps {
script {
sh "docker-compose -f docker-compose.yaml down --remove-orphans --volumes"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment