From afa5ecb8a62da9efb6e45ec7ee4c33ba73c4347f Mon Sep 17 00:00:00 2001 From: Shane MacBride Date: Tue, 8 Aug 2017 15:19:58 -0700 Subject: [PATCH] ECS Deployments LDOP-101 (#13) * LDOP-218 created ECS Jenkinsfile * LDOP-218 checking if ecs-cli image works properly * LDOP-218 create skeleton for Jenkinsfile * LDOP-118 add warning * complete docker-compose for ECS deployments LDOP-218 * LDOP-218 specify different services per env * LDOP-218 remove previous cluster auto * LDOP-218 add URL output * LDOP-218 typo syntax * LDOP-219 syntax error * LDOP-219 syntax error * LDOP-219 typo * LDOP-219 url testing * LDOP-219 limited duplicates * LDOP-219 add build step * LDOP-219 testing agent scoping * LDOP-219 scoping of agents fix * LDOP-219 add sonar tests add docker build and push * Correct Pivotal Jenkinsfile Build Step to Utilize Nexus * LDOP-219 add docker cred specifications * LDOP-219 add requirements to Jenkinsfile * Add requirements to pivotal Jenkinsfile * small typo * LDOP-219 Remove triple quotes as unneeded --- ecs-compose.yml | 7 +++ jenkinsfiles/ecs | 135 +++++++++++++++++++++++++++++++++++++++++++ jenkinsfiles/pivotal | 98 +++++++++++++++++-------------- 3 files changed, 196 insertions(+), 44 deletions(-) create mode 100644 ecs-compose.yml create mode 100644 jenkinsfiles/ecs diff --git a/ecs-compose.yml b/ecs-compose.yml new file mode 100644 index 000000000..30a813558 --- /dev/null +++ b/ecs-compose.yml @@ -0,0 +1,7 @@ +# Does not support build derivative +spring-petclinic: + image: liatrio/ecs-spring-petclinic:latest + cpu_shares: 100 + mem_limit: 262144000 + ports: + - "80:8080" diff --git a/jenkinsfiles/ecs b/jenkinsfiles/ecs new file mode 100644 index 000000000..f73bf49c9 --- /dev/null +++ b/jenkinsfiles/ecs @@ -0,0 +1,135 @@ +#!groovy +/* + * Author: + * + * Shane MacBride @shanemacbride + * + * Requirements: + * + * "DockerHub" Jenkins Credentials + * "ECS" Jenkins Credentials + * "ecs" AWS EC2 Keypair + * +*/ +pipeline { + agent none + stages { + stage('Build') { + agent { + docker { + image 'maven:3.5.0' + args '-e INITIAL_ADMIN_USER -e INITIAL_ADMIN_PASSWORD --network=${LDOP_NETWORK_NAME}' + } + } + steps { + configFileProvider( + [configFile(fileId: 'nexus', variable: 'MAVEN_SETTINGS')]) { + sh 'mvn -s $MAVEN_SETTINGS clean deploy -DskipTests=true -B' + } + } + } + stage('Sonar') { + agent { + docker { + image 'sebp/sonar-runner' + args '-e SONAR_ACCOUNT_LOGIN -e SONAR_ACCOUNT_PASSWORD -e SONAR_DB_URL -e SONAR_DB_LOGIN -e SONAR_DB_PASSWORD --network=${LDOP_NETWORK_NAME}' + } + } + steps { + 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('Docker Build') { + agent any + steps { + sh 'docker build -t liatrio/ecs-spring-petclinic:latest .' + } + } + stage('Docker Push') { + agent any + steps { + withCredentials([usernamePassword(credentialsId: 'DockerHub', passwordVariable: 'dockerPassword', usernameVariable: 'dockerUsername')]){ + sh "docker login -u ${env.dockerUsername} -p ${env.dockerPassword}" + sh 'docker push liatrio/ecs-spring-petclinic:latest' + } + } + } + stage('Create ECS Cluster') { + agent { + docker { + image 'liatrio/ecs-cli' + args '-u 0:0' + } + } + steps { + withCredentials([usernamePassword(credentialsId: 'ECS', passwordVariable: 'awsSecret', usernameVariable: 'awsAccess')]){ + sh "ecs-cli configure --region us-west-2 --access-key ${env.awsAccess} --secret-key ${env.awsSecret} --cluster petclinic-cluster" + sh "ecs-cli up --force --keypair ecs --capability-iam --size 3 --instance-type t2.micro" + } + } + } + stage('Deploy to ECS DEV') { + agent { + docker { + image 'liatrio/ecs-cli' + args '-u 0:0' + } + } + steps { + withCredentials([usernamePassword(credentialsId: 'ECS', passwordVariable: 'awsSecret', usernameVariable: 'awsAccess')]){ + sh "ecs-cli configure --region us-west-2 --access-key ${env.awsAccess} --secret-key ${env.awsSecret} --cluster petclinic-cluster" + sh "ecs-cli compose --project-name spring-petclinic-dev --file ecs-compose.yml up" + sh "ecs-cli ps | grep -m 1 dev | awk -F'RUNNING |:' '{print \"DEV URL: http://\"\$2\"/petclinic\"}'" + } + } + } + stage('Deploy to ECS QA') { + agent { + docker { + image 'liatrio/ecs-cli' + args '-u 0:0' + } + } + steps { + withCredentials([usernamePassword(credentialsId: 'ECS', passwordVariable: 'awsSecret', usernameVariable: 'awsAccess')]){ + sh "ecs-cli configure --region us-west-2 --access-key ${env.awsAccess} --secret-key ${env.awsSecret} --cluster petclinic-cluster" + sh "ecs-cli compose --project-name spring-petclinic-qa --file ecs-compose.yml up" + sh "ecs-cli ps | grep -m 1 qa | awk -F'RUNNING |:' '{print \"QA URL: http://\"\$2\"/petclinic\"}'" + input 'Deploy to ECS Production?' + } + } + } + stage('Deploy to ECS PROD') { + agent { + docker { + image 'liatrio/ecs-cli' + args '-u 0:0' + } + } + steps { + withCredentials([usernamePassword(credentialsId: 'ECS', passwordVariable: 'awsSecret', usernameVariable: 'awsAccess')]){ + sh "ecs-cli configure --region us-west-2 --access-key ${env.awsAccess} --secret-key ${env.awsSecret} --cluster petclinic-cluster" + sh "ecs-cli compose --project-name spring-petclinic-prod --file ecs-compose.yml up" + sh "ecs-cli ps | grep -m 1 prod | awk -F'RUNNING |:' '{print \"PROD URL: http://\"\$2\"/petclinic\"}'" + } + } + } + stage('Output Results') { + agent { + docker { + image 'liatrio/ecs-cli' + args '-u 0:0' + } + } + steps { + withCredentials([usernamePassword(credentialsId: 'ECS', passwordVariable: 'awsSecret', usernameVariable: 'awsAccess')]){ + sh "ecs-cli configure --region us-west-2 --access-key ${env.awsAccess} --secret-key ${env.awsSecret} --cluster petclinic-cluster" + sh "ecs-cli ps | grep -m 1 dev | awk -F'RUNNING |:' '{print \"DEV URL: http://\"\$2\"/petclinic\"}'" + sh "ecs-cli ps | grep -m 1 qa | awk -F'RUNNING |:' '{print \"QA URL: http://\"\$2\"/petclinic\"}'" + sh "ecs-cli ps | grep -m 1 prod | awk -F'RUNNING |:' '{print \"PROD URL: http://\"\$2\"/petclinic\"}'" + echo "To delete resources allocated run: ecs-cli down --force" + } + } + } + } +} diff --git a/jenkinsfiles/pivotal b/jenkinsfiles/pivotal index e62e061db..df179dccf 100644 --- a/jenkinsfiles/pivotal +++ b/jenkinsfiles/pivotal @@ -1,49 +1,59 @@ #!groovy +/* + * Author: + * + * Shane MacBride @shanemacbride + * + * Requirements: + * + * "pivotal" Jenkins Credentials + * +*/ pipeline { - agent none - stages { - stage('Build') { - agent { - docker { - image 'maven:3.5.0' - args '-e INITIAL_ADMIN_USER -e INITIAL_ADMIN_PASSWORD --network=${LDOP_NETWORK_NAME}' - } - } - steps { - configFileProvider( - [configFile(fileId: 'nexus', variable: 'MAVEN_SETTINGS')]) { - sh 'mvn -s $MAVEN_SETTINGS clean install -DskipTests=true -B' - } - } - } - stage('Deploy to Pivotal Development') { - agent { - docker { - image 'liatrio/cf-cli' - args '-u 0:0' - } - } - steps { - withCredentials([usernamePassword(credentialsId: 'pivotal', passwordVariable: 'pivotalPASSWORD', usernameVariable: 'pivotalUSERNAME')]){ - sh "cf api https://api.run.pivotal.io && cf login -u ${env.pivotalUSERNAME} -p ${env.pivotalPASSWORD}" - sh 'cf push -f ./base-manifest.yml' - echo "Should be accessible at http://pivotal-dev.liatr.io" - input 'Deploy to Pivotal Prod?' - } - } - } - stage('Deploy to Pivotal Prod') { - agent { - docker { - image 'liatrio/cf-cli' - args '-u 0:0' - } - } - steps { - withCredentials([usernamePassword(credentialsId: 'pivotal', passwordVariable: 'pivotalPASSWORD', usernameVariable: 'pivotalUSERNAME')]){ - sh "cf api https://api.run.pivotal.io && cf login -u ${env.pivotalUSERNAME} -p ${env.pivotalPASSWORD}" - sh 'cf push -f ./prod-manifest.yml' - echo "Should be accessible at http://pivotal-prod.liatr.io" + agent none + stages { + stage('Build') { + agent { + docker { + image 'maven:3.5.0' + args '-e INITIAL_ADMIN_USER -e INITIAL_ADMIN_PASSWORD --network=${LDOP_NETWORK_NAME}' + } + } + steps { + configFileProvider( + [configFile(fileId: 'nexus', variable: 'MAVEN_SETTINGS')]) { + sh 'mvn -s $MAVEN_SETTINGS clean deploy -DskipTests=true -B' + } + } + } + stage('Deploy to Pivotal Development') { + agent { + docker { + image 'liatrio/cf-cli' + args '-u 0:0' + } + } + steps { + withCredentials([usernamePassword(credentialsId: 'pivotal', passwordVariable: 'pivotalPASSWORD', usernameVariable: 'pivotalUSERNAME')]){ + sh "cf api https://api.run.pivotal.io && cf login -u ${env.pivotalUSERNAME} -p ${env.pivotalPASSWORD}" + sh 'cf push -f ./base-manifest.yml' + echo "Should be accessible at http://pivotal-dev.liatr.io" + input 'Deploy to Pivotal Prod?' + } + } + } + stage('Deploy to Pivotal Prod') { + agent { + docker { + image 'liatrio/cf-cli' + args '-u 0:0' + } + } + steps { + withCredentials([usernamePassword(credentialsId: 'pivotal', passwordVariable: 'pivotalPASSWORD', usernameVariable: 'pivotalUSERNAME')]){ + sh "cf api https://api.run.pivotal.io && cf login -u ${env.pivotalUSERNAME} -p ${env.pivotalPASSWORD}" + sh 'cf push -f ./prod-manifest.yml' + echo "Should be accessible at http://pivotal-prod.liatr.io" } } }