├── Dockerfile ├── Jenkinsfile ├── LICENSE ├── README.md ├── acceptance ├── Dockerfile ├── docker-compose-acceptance.yml └── test.sh ├── acceptance_test.sh ├── build.gradle ├── config └── checkstyle │ └── checkstyle.xml ├── docker-compose.yml ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── inventory ├── production └── staging ├── playbook.yml ├── redis.conf ├── smoke_test.sh └── src ├── main ├── java │ └── com │ │ └── leszko │ │ └── calculator │ │ ├── CacheConfig.java │ │ ├── Calculation.java │ │ ├── CalculationRepository.java │ │ ├── Calculator.java │ │ ├── CalculatorApplication.java │ │ └── CalculatorController.java └── resources │ ├── application.properties │ └── db │ └── migration │ ├── V1__Create_calculation_table.sql │ ├── V2__Add_created_at_column.sql │ ├── V3__Add_sum_column.sql │ ├── V4__Copy_result_into_sum_column.sql │ └── V5__Drop_result_column.sql └── test ├── java ├── acceptance │ ├── AcceptanceTest.java │ └── StepDefinitions.java ├── com │ └── leszko │ │ └── calculator │ │ └── CalculatorTest.java └── smoke │ └── SmokeTest.java └── resources ├── acceptance └── calculator.feature └── smoke └── calculator.feature /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM frolvlad/alpine-oraclejdk8:slim 2 | ADD build/libs/calculator-0.0.1-SNAPSHOT.jar app.jar 3 | ENTRYPOINT [ "java", "-jar", "app.jar" ] -------------------------------------------------------------------------------- /Jenkinsfile: -------------------------------------------------------------------------------- 1 | pipeline { 2 | agent any 3 | 4 | triggers { 5 | pollSCM('* * * * *') 6 | } 7 | 8 | stages { 9 | stage("Compile") { 10 | steps { 11 | sh "./gradlew compileJava" 12 | } 13 | } 14 | 15 | stage("Unit test") { 16 | steps { 17 | sh "./gradlew test" 18 | } 19 | } 20 | 21 | stage("Code coverage") { 22 | steps { 23 | sh "./gradlew jacocoTestReport" 24 | publishHTML (target: [ 25 | reportDir: 'build/reports/jacoco/test/html', 26 | reportFiles: 'index.html', 27 | reportName: "JaCoCo Report" ]) 28 | sh "./gradlew jacocoTestCoverageVerification" 29 | } 30 | } 31 | 32 | stage("Static code analysis") { 33 | steps { 34 | sh "./gradlew checkstyleMain" 35 | publishHTML (target: [ 36 | reportDir: 'build/reports/checkstyle/', 37 | reportFiles: 'main.html', 38 | reportName: "Checkstyle Report" ]) 39 | } 40 | } 41 | 42 | stage("Build") { 43 | steps { 44 | sh "./gradlew build" 45 | } 46 | } 47 | 48 | stage("Docker build") { 49 | steps { 50 | sh "docker build -t leszko/calculator:${BUILD_TIMESTAMP} ." 51 | } 52 | } 53 | 54 | stage("Docker login") { 55 | steps { 56 | withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'leszko', 57 | usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { 58 | sh "docker login --username $USERNAME --password $PASSWORD" 59 | } 60 | } 61 | } 62 | 63 | stage("Docker push") { 64 | steps { 65 | sh "docker push leszko/calculator:${BUILD_TIMESTAMP}" 66 | } 67 | } 68 | 69 | stage("Deploy to staging") { 70 | steps { 71 | sh "ansible-playbook playbook.yml -i inventory/staging" 72 | sleep 60 73 | } 74 | } 75 | 76 | stage("Acceptance test") { 77 | steps { 78 | sh "./acceptance_test.sh 192.168.0.166" 79 | } 80 | } 81 | 82 | // Performance test stages 83 | 84 | stage("Release") { 85 | steps { 86 | sh "ansible-playbook playbook.yml -i inventory/production" 87 | sleep 60 88 | } 89 | } 90 | 91 | stage("Smoke test") { 92 | steps { 93 | sh "./smoke_test.sh 192.168.0.115" 94 | } 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ## Continuous Delivery with Docker and Jenkins 5 | This is the code repository for [Continuous Delivery with Docker and Jenkins](https://www.packtpub.com/networking-and-servers/continuous-delivery-docker-and-jenkins?utm_source=github&utm_medium=repository&utm_campaign=9781787125230), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the book from start to finish. 6 | ## About the Book 7 | The combination of Docker and Jenkins improves your Continuous Delivery pipeline using fewer resources. It also helps you scale up your builds, automate tasks and speed up Jenkins performance with the benefits of Docker containerization. 8 | 9 | This book will explain the advantages of combining Jenkins and Docker to improve the continuous integration and delivery process of app development. It will start with setting up a Docker server and configuring Jenkins on it. It will then provide steps to build applications on Docker files and integrate them with Jenkins using continuous delivery processes such as continuous integration, automated acceptance testing, and configuration management. 10 | 11 | 12 | ## Instructions and Navigation 13 | All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter02. 14 | 15 | 16 | 17 | The code will look like the following: 18 | ``` 19 | pipeline { 20 | agent any 21 | stages { 22 | stage("Hello") { 23 | steps { 24 | echo 'Hello World' 25 | } 26 | } 27 | } 28 | } 29 | ``` 30 | 31 | Docker requires the 64-bit Linux operating system. All examples in this book have been 32 | developed using Ubuntu 16.04, but any other Linux system with the kernel version 3.10 or 33 | above is sufficient. 34 | 35 | ## Related Products 36 | * [Continuous Delivery and DevOps – A Quickstart Guide - Second Edition](https://www.packtpub.com/application-development/continuous-delivery-and-devops-–-quickstart-guide-second-edition?utm_source=github&utm_medium=repository&utm_campaign=9781784399313) 37 | 38 | * [Continuous Delivery and DevOps: A Quickstart guide](https://www.packtpub.com/virtualization-and-cloud/continuous-delivery-and-devops-quickstart-guide?utm_source=github&utm_medium=repository&utm_campaign=9781849693684) 39 | 40 | * [Learning Continuous Integration with Jenkins - Second Edition](https://www.packtpub.com/virtualization-and-cloud/learning-continuous-integration-jenkins-second-edition?utm_source=github&utm_medium=repository&utm_campaign=9781788479356) 41 | ### Download a free PDF 42 | 43 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
44 |

https://packt.link/free-ebook/9781838552183

-------------------------------------------------------------------------------- /acceptance/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:trusty 2 | RUN apt-get update && \ 3 | apt-get install -yq curl 4 | ADD test.sh . 5 | CMD ["bash", "test.sh"] 6 | 7 | -------------------------------------------------------------------------------- /acceptance/docker-compose-acceptance.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | services: 3 | acceptance_test: 4 | build: ./acceptance 5 | links: 6 | - calculator 7 | -------------------------------------------------------------------------------- /acceptance/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | sleep 60 3 | test $(curl calculator:8080/sum?a=1\&b=2) -eq 3 4 | -------------------------------------------------------------------------------- /acceptance_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Running acceptance test..." 3 | CALCULATOR_PORT=$(ssh -o StrictHostKeychecking=no ubuntu@$@ "docker-compose port calculator 8080 | cut -d: -f2") 4 | echo "Host: $@:$CALCULATOR_PORT" 5 | ./gradlew acceptanceTest -Dcalculator.url=http://$@:$CALCULATOR_PORT 6 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '1.5.1.RELEASE' 4 | } 5 | repositories { 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 | classpath('com.h2database:h2:1.4.191') 11 | } 12 | } 13 | 14 | plugins { 15 | id "org.flywaydb.flyway" version "4.2.0" 16 | } 17 | 18 | apply plugin: 'java' 19 | apply plugin: 'eclipse' 20 | apply plugin: 'org.springframework.boot' 21 | apply plugin: "jacoco" 22 | apply plugin: 'checkstyle' 23 | 24 | jar { 25 | baseName = 'calculator' 26 | version = '0.0.1-SNAPSHOT' 27 | } 28 | 29 | sourceCompatibility = 1.8 30 | 31 | repositories { 32 | mavenCentral() 33 | } 34 | 35 | 36 | dependencies { 37 | compile('org.springframework.boot:spring-boot-starter-web') 38 | compile("org.springframework.boot:spring-boot-starter-data-jpa") 39 | compile("com.h2database:h2") 40 | testCompile('org.springframework.boot:spring-boot-starter-test') 41 | compile "org.springframework.data:spring-data-redis:1.8.0.RELEASE" 42 | compile "redis.clients:jedis:2.9.0" 43 | 44 | testCompile("info.cukes:cucumber-java:1.2.4") 45 | testCompile("info.cukes:cucumber-junit:1.2.4") 46 | } 47 | 48 | jacocoTestCoverageVerification { 49 | violationRules { 50 | rule { 51 | limit { 52 | minimum = 0.2 53 | } 54 | } 55 | } 56 | } 57 | 58 | task acceptanceTest(type: Test) { 59 | include '**/acceptance/**' 60 | systemProperties System.getProperties() 61 | } 62 | 63 | task smokeTest(type: Test) { 64 | include '**/smoke/**' 65 | systemProperties System.getProperties() 66 | } 67 | 68 | test { 69 | exclude '**/acceptance/**' 70 | exclude '**/smoke/**' 71 | } 72 | 73 | bootRepackage { 74 | executable = true 75 | } 76 | 77 | flyway { 78 | url = 'jdbc:h2:file:/tmp/calculator' 79 | user = 'sa' 80 | } -------------------------------------------------------------------------------- /config/checkstyle/checkstyle.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "2" 2 | services: 3 | calculator: 4 | image: leszko/calculator:latest 5 | ports: 6 | - 8080 7 | redis: 8 | image: redis:latest 9 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Continuous-Delivery-with-Docker-and-Jenkins/6caf074fd421cf5f13155dc7621889bb0c2239f4/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | zipStoreBase=GRADLE_USER_HOME 4 | zipStorePath=wrapper/dists 5 | distributionUrl=https://services.gradle.org/distributions/gradle-3.4.1-all.zip 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /inventory/production: -------------------------------------------------------------------------------- 1 | [webservers] 2 | web2 ansible_host=192.168.0.115 ansible_user=ubuntu 3 | -------------------------------------------------------------------------------- /inventory/staging: -------------------------------------------------------------------------------- 1 | [webservers] 2 | web1 ansible_host=192.168.0.166 ansible_user=ubuntu 3 | -------------------------------------------------------------------------------- /playbook.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: webservers 3 | become: yes 4 | become_method: sudo 5 | tasks: 6 | - name: add docker apt keys 7 | apt_key: 8 | keyserver: hkp://p80.pool.sks-keyservers.net:80 9 | id: 58118E89F3A912897C070ADBF76221572C52609D 10 | - name: update apt 11 | apt_repository: 12 | repo: deb https://apt.dockerproject.org/repo ubuntu-xenial main 13 | state: present 14 | - name: install Docker 15 | apt: 16 | name: docker-engine 17 | update_cache: yes 18 | state: present 19 | - name: add ubuntu to docker group 20 | user: 21 | name: ubuntu 22 | groups: docker 23 | append: yes 24 | - name: install python-pip 25 | apt: 26 | name: python-pip 27 | state: present 28 | - name: install docker-py 29 | pip: 30 | name: docker-py 31 | - name: install Docker Compose 32 | pip: 33 | name: docker-compose 34 | version: 1.9.0 35 | - name: copy docker-compose.yml 36 | copy: 37 | src: ./docker-compose.yml 38 | dest: ./docker-compose.yml 39 | - name: run docker-compose 40 | environment: 41 | BUILD_TIMESTAMP: "{{ lookup('env', 'BUILD_TIMESTAMP') }}" 42 | docker_service: 43 | project_src: . 44 | state: present 45 | restarted: yes 46 | -------------------------------------------------------------------------------- /redis.conf: -------------------------------------------------------------------------------- 1 | daemonize yes 2 | pidfile /var/run/redis/redis-server.pid 3 | port 6379 4 | bind 0.0.0.0 -------------------------------------------------------------------------------- /smoke_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Running smoke test..." 3 | CALCULATOR_PORT=$(ssh -o StrictHostKeychecking=no ubuntu@$@ "docker-compose port calculator 8080 | cut -d: -f2") 4 | echo "Host: $@:$CALCULATOR_PORT" 5 | ./gradlew smokeTest -Dcalculator.url=http://$@:$CALCULATOR_PORT 6 | -------------------------------------------------------------------------------- /src/main/java/com/leszko/calculator/CacheConfig.java: -------------------------------------------------------------------------------- 1 | package com.leszko.calculator; 2 | import org.springframework.cache.CacheManager; 3 | import org.springframework.cache.annotation.CachingConfigurerSupport; 4 | import org.springframework.cache.annotation.EnableCaching; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.data.redis.cache.RedisCacheManager; 8 | import org.springframework.data.redis.connection.RedisConnectionFactory; 9 | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 10 | import org.springframework.data.redis.core.RedisTemplate; 11 | 12 | /** Cache config with Redis. */ 13 | @Configuration 14 | @EnableCaching 15 | public class CacheConfig extends CachingConfigurerSupport { 16 | private final static String REDIS_HOST = "redis"; 17 | 18 | @Bean 19 | public JedisConnectionFactory redisConnectionFactory() { 20 | JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(); 21 | redisConnectionFactory.setHostName(REDIS_HOST); 22 | redisConnectionFactory.setPort(6379); 23 | return redisConnectionFactory; 24 | } 25 | 26 | @Bean 27 | public RedisTemplate redisTemplate(RedisConnectionFactory cf) { 28 | RedisTemplate redisTemplate = 29 | new RedisTemplate(); 30 | redisTemplate.setConnectionFactory(cf); 31 | return redisTemplate; 32 | } 33 | 34 | @Bean 35 | public CacheManager cacheManager(RedisTemplate redisTemplate) { 36 | return new RedisCacheManager(redisTemplate); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/leszko/calculator/Calculation.java: -------------------------------------------------------------------------------- 1 | package com.leszko.calculator; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | import java.sql.Timestamp; 8 | 9 | /** 10 | * Calculation entity. 11 | */ 12 | @Entity 13 | public class Calculation { 14 | @Id 15 | @GeneratedValue(strategy= GenerationType.AUTO) 16 | private Integer id; 17 | private String a; 18 | private String b; 19 | private String sum; 20 | private Timestamp createdAt; 21 | 22 | protected Calculation() {} 23 | 24 | public Calculation(String a, String b, String sum, Timestamp createdAt) { 25 | this.a = a; 26 | this.b = b; 27 | this.sum = sum; 28 | this.createdAt = createdAt; 29 | } 30 | 31 | public String getSum() { 32 | return sum; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/leszko/calculator/CalculationRepository.java: -------------------------------------------------------------------------------- 1 | package com.leszko.calculator; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | 5 | /** 6 | * Calculation repository. 7 | */ 8 | public interface CalculationRepository extends CrudRepository{ 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/leszko/calculator/Calculator.java: -------------------------------------------------------------------------------- 1 | package com.leszko.calculator; 2 | import org.springframework.cache.annotation.Cacheable; 3 | import org.springframework.stereotype.Service; 4 | 5 | /** Calculator logic */ 6 | @Service 7 | public class Calculator { 8 | @Cacheable("sum") 9 | public int sum(int a, int b) { 10 | return a + b; 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/com/leszko/calculator/CalculatorApplication.java: -------------------------------------------------------------------------------- 1 | package com.leszko.calculator; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * Main Spring Application. 8 | */ 9 | @SpringBootApplication 10 | public class CalculatorApplication { 11 | 12 | public static void main(String[] args) { 13 | SpringApplication.run(CalculatorApplication.class, args); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/leszko/calculator/CalculatorController.java: -------------------------------------------------------------------------------- 1 | package com.leszko.calculator; 2 | import org.springframework.beans.factory.annotation.Autowired; 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RequestParam; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.sql.Timestamp; 8 | import java.time.Instant; 9 | 10 | @RestController 11 | class CalculatorController { 12 | @Autowired 13 | private Calculator calculator; 14 | 15 | @Autowired 16 | private CalculationRepository calculationRepository; 17 | 18 | @RequestMapping("/sum") 19 | String sum(@RequestParam("a") Integer a, @RequestParam("b") Integer b) { 20 | String result = String.valueOf(calculator.sum(a, b)); 21 | Calculation calculation = new Calculation(a.toString(), b.toString(), result, Timestamp.from(Instant.now())); 22 | calculationRepository.save(calculation); 23 | System.out.println("Count: " + calculationRepository.count()); 24 | return result; 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:file:/tmp/calculator;DB_CLOSE_ON_EXIT=FALSE 2 | spring.jpa.hibernate.ddl-auto=validate 3 | spring.h2.console.enabled=true -------------------------------------------------------------------------------- /src/main/resources/db/migration/V1__Create_calculation_table.sql: -------------------------------------------------------------------------------- 1 | create table CALCULATION ( 2 | ID int not null auto_increment, 3 | A varchar(100), 4 | B varchar(100), 5 | RESULT varchar(100), 6 | primary key (ID) 7 | ); -------------------------------------------------------------------------------- /src/main/resources/db/migration/V2__Add_created_at_column.sql: -------------------------------------------------------------------------------- 1 | alter table CALCULATION 2 | add CREATED_AT timestamp; -------------------------------------------------------------------------------- /src/main/resources/db/migration/V3__Add_sum_column.sql: -------------------------------------------------------------------------------- 1 | alter table CALCULATION 2 | add SUM varchar(100); -------------------------------------------------------------------------------- /src/main/resources/db/migration/V4__Copy_result_into_sum_column.sql: -------------------------------------------------------------------------------- 1 | update CALCULATION 2 | set CALCULATION.sum = CALCULATION.result 3 | where CALCULATION.sum is null; -------------------------------------------------------------------------------- /src/main/resources/db/migration/V5__Drop_result_column.sql: -------------------------------------------------------------------------------- 1 | alter table CALCULATION 2 | drop column RESULT; -------------------------------------------------------------------------------- /src/test/java/acceptance/AcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package acceptance; 2 | 3 | import cucumber.api.CucumberOptions; 4 | import cucumber.api.junit.Cucumber; 5 | import org.junit.runner.RunWith; 6 | 7 | /** Runner for the acceptance test. */ 8 | @RunWith(Cucumber.class) 9 | @CucumberOptions(features = "classpath:acceptance") 10 | public class AcceptanceTest { 11 | } 12 | -------------------------------------------------------------------------------- /src/test/java/acceptance/StepDefinitions.java: -------------------------------------------------------------------------------- 1 | package acceptance; 2 | 3 | import cucumber.api.java.en.Given; 4 | import cucumber.api.java.en.Then; 5 | import cucumber.api.java.en.When; 6 | import org.springframework.web.client.RestTemplate; 7 | 8 | import static org.junit.Assert.assertEquals; 9 | 10 | /** Steps definitions for calculator.acceptance */ 11 | public class StepDefinitions { 12 | private String server = System.getProperty("calculator.url"); 13 | 14 | private RestTemplate restTemplate = new RestTemplate(); 15 | 16 | private String a; 17 | private String b; 18 | private String result; 19 | 20 | @Given("^I have two numbers: (.*) and (.*)$") 21 | public void i_have_two_numbers(String a, String b) throws Throwable { 22 | this.a = a; 23 | this.b = b; 24 | } 25 | 26 | @When("^the calculator sums them$") 27 | public void the_calculator_sums_them() throws Throwable { 28 | String url = String.format("%s/sum?a=%s&b=%s", server, a, b); 29 | result = restTemplate.getForObject(url, String.class); 30 | } 31 | 32 | @Then("^I receive (.*) as a result$") 33 | public void i_receive_as_a_result(String expectedResult) throws Throwable { 34 | assertEquals(expectedResult, result); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/com/leszko/calculator/CalculatorTest.java: -------------------------------------------------------------------------------- 1 | package com.leszko.calculator; 2 | import org.junit.Test; 3 | import static org.junit.Assert.assertEquals; 4 | 5 | /** 6 | * Calculator test. 7 | */ 8 | public class CalculatorTest { 9 | private Calculator calculator = new Calculator(); 10 | 11 | @Test 12 | public void testSum() { 13 | assertEquals(5, calculator.sum(2, 3)); 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /src/test/java/smoke/SmokeTest.java: -------------------------------------------------------------------------------- 1 | package smoke; 2 | 3 | import cucumber.api.CucumberOptions; 4 | import cucumber.api.junit.Cucumber; 5 | import org.junit.runner.RunWith; 6 | 7 | /** Runner for the acceptance test. */ 8 | @RunWith(Cucumber.class) 9 | @CucumberOptions(features = "classpath:smoke") 10 | public class SmokeTest { 11 | } 12 | -------------------------------------------------------------------------------- /src/test/resources/acceptance/calculator.feature: -------------------------------------------------------------------------------- 1 | Feature: Calculator 2 | Scenario: Sum two numbers 3 | Given I have two numbers: 1 and 2 4 | When the calculator sums them 5 | Then I receive 3 as a result -------------------------------------------------------------------------------- /src/test/resources/smoke/calculator.feature: -------------------------------------------------------------------------------- 1 | Feature: Calculator 2 | Scenario: Sum two numbers 3 | Given I have two numbers: 1 and 2 4 | When the calculator sums them 5 | Then I receive 3 as a result --------------------------------------------------------------------------------