mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-19 14:05:50 +00:00
Blue/Green deploy stage
This commit is contained in:
parent
ec58bcca8c
commit
151fbe7ff2
4 changed files with 124 additions and 22 deletions
71
Jenkinsfile
vendored
71
Jenkinsfile
vendored
|
@ -1,10 +1,16 @@
|
||||||
#!/bin/env groovy
|
#!/bin/env groovy
|
||||||
|
|
||||||
|
@Library('ldop-shared-library@ad8185bec5a1999d144ec6c8fdadf9a62ab0506e') _
|
||||||
|
|
||||||
pipeline {
|
pipeline {
|
||||||
agent none
|
agent none
|
||||||
|
|
||||||
environment {
|
environment {
|
||||||
IMAGE = "liatrio/petclinic-tomcat"
|
IMAGE = "liatrio/petclinic-tomcat"
|
||||||
}
|
}
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
|
|
||||||
stage('Build') {
|
stage('Build') {
|
||||||
agent {
|
agent {
|
||||||
docker {
|
docker {
|
||||||
|
@ -13,8 +19,7 @@ pipeline {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
steps {
|
steps {
|
||||||
configFileProvider(
|
configFileProvider([configFile(fileId: 'nexus', variable: 'MAVEN_SETTINGS')]) {
|
||||||
[configFile(fileId: 'nexus', variable: 'MAVEN_SETTINGS')]) {
|
|
||||||
sh 'mvn -s $MAVEN_SETTINGS clean deploy -DskipTests=true -B'
|
sh 'mvn -s $MAVEN_SETTINGS clean deploy -DskipTests=true -B'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,6 +35,7 @@ pipeline {
|
||||||
sh '/opt/sonar-runner-2.4/bin/sonar-runner -e -D sonar.login=${SONAR_ACCOUNT_LOGIN} -D sonar.password=${SONAR_ACCOUNT_PASSWORD} -D sonar.jdbc.url=${SONAR_DB_URL} -D sonar.jdbc.username=${SONAR_DB_LOGIN} -D sonar.jdbc.password=${SONAR_DB_PASSWORD}'
|
sh '/opt/sonar-runner-2.4/bin/sonar-runner -e -D sonar.login=${SONAR_ACCOUNT_LOGIN} -D sonar.password=${SONAR_ACCOUNT_PASSWORD} -D sonar.jdbc.url=${SONAR_DB_URL} -D sonar.jdbc.username=${SONAR_DB_LOGIN} -D sonar.jdbc.password=${SONAR_DB_PASSWORD}'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Get Artifact') {
|
stage('Get Artifact') {
|
||||||
agent {
|
agent {
|
||||||
docker {
|
docker {
|
||||||
|
@ -41,30 +47,34 @@ pipeline {
|
||||||
sh 'mvn clean'
|
sh 'mvn clean'
|
||||||
script {
|
script {
|
||||||
pom = readMavenPom file: 'pom.xml'
|
pom = readMavenPom file: 'pom.xml'
|
||||||
getArtifact(pom.groupId, pom.artifactId, pom.version, "petclinic")
|
getArtifact(pom.groupId, pom.artifactId, pom.version, 'petclinic')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Build container') {
|
stage('Build container') {
|
||||||
agent any
|
agent any
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
sh "docker build -t liatrio/petclinic-tomcat:${env.BRANCH_NAME} ."
|
|
||||||
if ( env.BRANCH_NAME == 'master' ) {
|
if ( env.BRANCH_NAME == 'master' ) {
|
||||||
pom = readMavenPom file: 'pom.xml'
|
pom = readMavenPom file: 'pom.xml'
|
||||||
containerVersion = pom.version
|
TAG = pom.version
|
||||||
sh "docker build -t liatrio/petclinic-tomcat:${containerVersion} ."
|
} else {
|
||||||
}
|
TAG = env.BRANCH_NAME
|
||||||
|
}
|
||||||
|
sh "docker build -t ${env.IMAGE}:${TAG} ."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Run local container') {
|
stage('Run local container') {
|
||||||
agent any
|
agent any
|
||||||
steps {
|
steps {
|
||||||
sh 'docker rm -f petclinic-tomcat-temp || true'
|
sh 'docker rm -f petclinic-tomcat-temp || true'
|
||||||
sh "docker run -d --network=${LDOP_NETWORK_NAME} --name petclinic-tomcat-temp liatrio/petclinic-tomcat:${env.BRANCH_NAME}"
|
sh "docker run -d --network=${LDOP_NETWORK_NAME} --name petclinic-tomcat-temp ${env.IMAGE}:${TAG}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Smoke-Test & OWASP Security Scan') {
|
stage('Smoke-Test & OWASP Security Scan') {
|
||||||
agent {
|
agent {
|
||||||
docker {
|
docker {
|
||||||
|
@ -82,35 +92,28 @@ pipeline {
|
||||||
sh 'docker rm -f petclinic-tomcat-temp || true'
|
sh 'docker rm -f petclinic-tomcat-temp || true'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Push to dockerhub') {
|
stage('Push to dockerhub') {
|
||||||
agent any
|
agent any
|
||||||
steps {
|
steps {
|
||||||
withCredentials([usernamePassword(credentialsId: 'dockerhub', passwordVariable: 'dockerPassword', usernameVariable: 'dockerUsername')]){
|
withCredentials([usernamePassword(credentialsId: 'dockerhub', passwordVariable: 'dockerPassword', usernameVariable: 'dockerUsername')]){
|
||||||
script {
|
script {
|
||||||
sh "docker login -u ${env.dockerUsername} -p ${env.dockerPassword}"
|
sh "docker login -u ${env.dockerUsername} -p ${env.dockerPassword}"
|
||||||
if ( env.BRANCH_NAME == 'master' ) {
|
sh "docker push ${env.IMAGE}:${TAG}"
|
||||||
sh "docker push liatrio/petclinic-tomcat:${containerVersion}"
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
sh "docker push liatrio/petclinic-tomcat:${env.BRANCH_NAME}"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Deploy to dev') {
|
stage('Deploy to dev') {
|
||||||
agent any
|
agent any
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
if ( env.BRANCH_NAME == 'master' ) {
|
deployToEnvironment("ec2-user", "dev.petclinic.liatr.io", "petclinic-deploy-key", env.IMAGE, TAG, "spring-petclinic", "dev.petclinic.liatr.io")
|
||||||
deployToEnvironment("ec2-user", "dev.petclinic.liatr.io", "petclinic-deploy-key", "${env.IMAGE}", "${containerVersion}", "spring-petclinic", "dev.petclinic.liatr.io")
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
deployToEnvironment("ec2-user", "dev.petclinic.liatr.io", "petclinic-deploy-key", "${env.IMAGE}", "${env.BRANCH_NAME}", "spring-petclinic", "dev.petclinic.liatr.io")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Smoke test dev') {
|
stage('Smoke test dev') {
|
||||||
agent {
|
agent {
|
||||||
docker {
|
docker {
|
||||||
|
@ -123,15 +126,17 @@ pipeline {
|
||||||
echo "Should be accessible at https://dev.petclinic.liatr.io/petclinic"
|
echo "Should be accessible at https://dev.petclinic.liatr.io/petclinic"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Deploy to qa') {
|
stage('Deploy to qa') {
|
||||||
when {
|
when {
|
||||||
branch 'master'
|
branch 'master'
|
||||||
}
|
}
|
||||||
agent any
|
agent any
|
||||||
steps {
|
steps {
|
||||||
deployToEnvironment("ec2-user", "qa.petclinic.liatr.io", "petclinic-deploy-key", "${env.IMAGE}", "${containerVersion}", "spring-petclinic", "qa.petclinic.liatr.io")
|
deployToEnvironment("ec2-user", "qa.petclinic.liatr.io", "petclinic-deploy-key", env.IMAGE, TAG, "spring-petclinic", "qa.petclinic.liatr.io")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Smoke test qa') {
|
stage('Smoke test qa') {
|
||||||
when {
|
when {
|
||||||
branch 'master'
|
branch 'master'
|
||||||
|
@ -145,6 +150,30 @@ pipeline {
|
||||||
steps {
|
steps {
|
||||||
sh "cd regression-suite && mvn clean -B test -DPETCLINIC_URL=https://qa.petclinic.liatr.io/petclinic"
|
sh "cd regression-suite && mvn clean -B test -DPETCLINIC_URL=https://qa.petclinic.liatr.io/petclinic"
|
||||||
echo "Should be accessible at https://qa.petclinic.liatr.io/petclinic"
|
echo "Should be accessible at https://qa.petclinic.liatr.io/petclinic"
|
||||||
|
input 'Deploy to Prod?'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Blue/Green deploy to prod') {
|
||||||
|
/*
|
||||||
|
when {
|
||||||
|
branch 'master'
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
agent {
|
||||||
|
dockerfile {
|
||||||
|
filename "blue-green/Dockerfile"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
steps {
|
||||||
|
withCredentials([
|
||||||
|
usernamePassword(credentialsId: 'aws', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY'),
|
||||||
|
file(credentialsId: 'petclinic-deploy-key', variable: 'DEPLOY_KEY_PATH')
|
||||||
|
]) {
|
||||||
|
script {
|
||||||
|
sh "TAG=${TAG} blue-green/blue-green-deploy"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
7
blue-green/Dockerfile
Normal file
7
blue-green/Dockerfile
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
FROM maven:3.5.0
|
||||||
|
|
||||||
|
RUN apt-get update -y && apt-get install -y awscli jq
|
||||||
|
|
||||||
|
# Jenkins workspace runs with UID 1000
|
||||||
|
RUN useradd -ms /bin/bash -u 1000 jenkins
|
||||||
|
USER jenkins
|
66
blue-green/blue-green-deploy
Executable file
66
blue-green/blue-green-deploy
Executable file
|
@ -0,0 +1,66 @@
|
||||||
|
#!/bin/bash -xe
|
||||||
|
|
||||||
|
# This script executes a blue/green deployment on the petclinic production infrastructure.
|
||||||
|
#
|
||||||
|
# Production infrastructure consists of:
|
||||||
|
# - An application load balancer (prod-petclinic)
|
||||||
|
# - Two target groups (proda-petclinic, prodb-petclinic)
|
||||||
|
# - A single t2.micro ec2 instance in each target group (proda.petclinic.liatr.io, prodb.petclinic.liatr.io)
|
||||||
|
#
|
||||||
|
# One of the two target groups should be attached to the ALB at any given time via a Listener
|
||||||
|
#
|
||||||
|
# To perform a blue/green deployment, we:
|
||||||
|
# - Figure out which target group is detached
|
||||||
|
# - Deploy the application to each instance in the detached target group (only 1 instance in this case)
|
||||||
|
# - Run smoke tests on the newly deployed applications
|
||||||
|
# - Delete the current attached listener
|
||||||
|
# - Attach a new listener that forwards to the target group we deployed to.
|
||||||
|
#
|
||||||
|
# Expected environment variables:
|
||||||
|
# - DEPLOY_KEY_PATH Private key used to shell deploy to petclinic prod instances
|
||||||
|
# - AWS_ACCESS_ID_ID
|
||||||
|
# - AWS_SECRET_ACCESS_KEY For modifying load balancer resources
|
||||||
|
# - IMAGE Dockerhub repo to deploy from
|
||||||
|
# - TAG Version of image to deploy
|
||||||
|
#
|
||||||
|
|
||||||
|
export AWS_DEFAULT_REGION='us-west-2'
|
||||||
|
|
||||||
|
deploy() {
|
||||||
|
FQDN="$1"
|
||||||
|
ssh -i ${DEPLOY_KEY_PATH} -o StrictHostKeyChecking=no ec2-user@${FQDN} "docker rm -f petclinic || true"
|
||||||
|
ssh -i ${DEPLOY_KEY_PATH} -o StrictHostKeyChecking=no ec2-user@${FQDN} "docker run -d -p 80:8080 --name petclinic ${IMAGE}:${TAG}"
|
||||||
|
}
|
||||||
|
|
||||||
|
smoke_test() {
|
||||||
|
FQDN="$1"
|
||||||
|
cd ./regression-suite && mvn clean -B test -DPETCLINIC_URL="${FQDN}" && cd -
|
||||||
|
}
|
||||||
|
|
||||||
|
LOAD_BALANCER_ARN=$(aws elbv2 describe-load-balancers --names prod-petclinic | jq -r '.LoadBalancers|.[0]|.LoadBalancerArn')
|
||||||
|
ATTACHED_LISTENER_ARN=$(aws elbv2 describe-listeners --load-balancer-arn "${LOAD_BALANCER_ARN}" | jq -r '.Listeners|.[0]|.ListenerArn')
|
||||||
|
ATTACHED_LISTENER_TARGET_GROUP_ARN=$(aws elbv2 describe-listeners --load-balancer-arn "${LOAD_BALANCER_ARN}" | jq -r '.Listeners|.[0]|.DefaultActions|.[0]|.TargetGroupArn')
|
||||||
|
PRODA_TARGET_GROUP_ARN=$(aws elbv2 describe-target-groups --names proda-petclinic | jq -r '.TargetGroups|.[0]|.TargetGroupArn')
|
||||||
|
PRODB_TARGET_GROUP_ARN=$(aws elbv2 describe-target-groups --names prodb-petclinic | jq -r '.TargetGroups|.[0]|.TargetGroupArn')
|
||||||
|
|
||||||
|
if [[ "${ATTACHED_LISTENER_TARGET_GROUP_ARN}" == "${PRODA_TARGET_GROUP_ARN}" ]]; then
|
||||||
|
NEW_TARGET_GROUP_ARN=${PRODB_TARGET_GROUP_ARN}
|
||||||
|
deploy "prodb.petclinic.liatr.io"
|
||||||
|
smoke_test "http://prodb.petclinic.liatr.io/petclinic"
|
||||||
|
else
|
||||||
|
NEW_TARGET_GROUP_ARN=${PRODA_TARGET_GROUP_ARN}
|
||||||
|
deploy "proda.petclinic.liatr.io"
|
||||||
|
smoke_test "http://proda.petclinic.liatr.io/petclinic"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "${ATTACHED_LISTENER_ARN}" != "null" ]]; then
|
||||||
|
echo "Deleting previous listener: ${ATTACHED_LISTENER_ARN}"
|
||||||
|
aws elbv2 delete-listener --listener-arn "${ATTACHED_LISTENER_ARN}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Attaching listener with target group: ${NEW_TARGET_GROUP_ARN}"
|
||||||
|
aws elbv2 create-listener \
|
||||||
|
--load-balancer-arn "${LOAD_BALANCER_ARN}" \
|
||||||
|
--protocol "HTTP" \
|
||||||
|
--port "80" \
|
||||||
|
--default-actions "Type=forward,TargetGroupArn=${NEW_TARGET_GROUP_ARN}"
|
|
@ -12,7 +12,7 @@ input[type="text"] {
|
||||||
}
|
}
|
||||||
|
|
||||||
.navbar .nav > li > a {
|
.navbar .nav > li > a {
|
||||||
color: #000000;
|
color: #000000
|
||||||
}
|
}
|
||||||
|
|
||||||
.form-horizontal .control-label {
|
.form-horizontal .control-label {
|
||||||
|
|
Loading…
Reference in a new issue