├── .gitignore ├── Jenkinsfile ├── devops-101.key ├── devops-101.pdf ├── docker-compose.yml ├── readme.asciidoc └── webapp ├── Dockerfile ├── pom.xml └── src ├── main └── java │ └── org │ └── example │ └── pipeline │ ├── Database.java │ └── Main.java └── test └── java └── org └── example └── pipeline └── MainTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | **/target/ 3 | -------------------------------------------------------------------------------- /Jenkinsfile: -------------------------------------------------------------------------------- 1 | node { 2 | checkout scm 3 | env.PATH = "${tool 'Maven3'}/bin:${env.PATH}" 4 | stage('Package') { 5 | dir('webapp') { 6 | sh 'mvn clean package -DskipTests' 7 | } 8 | } 9 | 10 | stage('Create Docker Image') { 11 | dir('webapp') { 12 | docker.build("arungupta/docker-jenkins-pipeline:${env.BUILD_NUMBER}") 13 | } 14 | } 15 | 16 | stage ('Run Application') { 17 | try { 18 | // Start database container here 19 | // sh 'docker run -d --name db -p 8091-8093:8091-8093 -p 11210:11210 arungupta/oreilly-couchbase:latest' 20 | 21 | // Run application using Docker image 22 | sh "DB=`docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db`" 23 | sh "docker run -e DB_URI=$DB arungupta/docker-jenkins-pipeline:${env.BUILD_NUMBER}" 24 | 25 | // Run tests using Maven 26 | //dir ('webapp') { 27 | // sh 'mvn exec:java -DskipTests' 28 | //} 29 | } catch (error) { 30 | } finally { 31 | // Stop and remove database container here 32 | //sh 'docker-compose stop db' 33 | //sh 'docker-compose rm db' 34 | } 35 | } 36 | 37 | stage('Run Tests') { 38 | try { 39 | dir('webapp') { 40 | sh "mvn test" 41 | docker.build("arungupta/docker-jenkins-pipeline:${env.BUILD_NUMBER}").push() 42 | } 43 | } catch (error) { 44 | 45 | } finally { 46 | junit '**/target/surefire-reports/*.xml' 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /devops-101.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arun-gupta/docker-jenkins-pipeline/4cc009b7c8bc4ca85f8555cdaba620a1414c17a6/devops-101.key -------------------------------------------------------------------------------- /devops-101.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arun-gupta/docker-jenkins-pipeline/4cc009b7c8bc4ca85f8555cdaba620a1414c17a6/devops-101.pdf -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | db: 5 | image: arungupta/oreilly-couchbase 6 | ports: 7 | - 8091:8091 8 | - 8092:8092 9 | - 8093:8093 10 | - 11210:11210 11 | 12 | app: 13 | build: ./webapp 14 | image: arungupta/docker-jenkins-app 15 | 16 | app2: 17 | image: arungupta/docker-jenkins-pipeline:${BUILD_NUMBER} 18 | 19 | unit: 20 | image: maven 21 | volumes: 22 | - .:/usr/src/myapp 23 | - /tmp/go:/go 24 | command: bash -c "mvn test" 25 | 26 | -------------------------------------------------------------------------------- /readme.asciidoc: -------------------------------------------------------------------------------- 1 | = Docker + Java + Jenkins Pipeline 2 | 3 | == Manual Steps 4 | 5 | . *Start database*: `docker run -d --name db -p 8091-8093:8091-8093 -p 11210:11210 arungupta/oreilly-couchbase:latest` 6 | . *Run app* 7 | .. Using Maven 8 | ... *Build app*: `mvn -f webapp/pom.xml clean package` 9 | ... *Run app*: `mvn -f webapp/pom.xml exec:java -DskipTests` 10 | ... *Run test*: `mvn -f webapp/pom.xml test` 11 | .. Using Docker 12 | ... *Build app*: `docker-compose build app` 13 | ... *Run app*: 14 | + 15 | ``` 16 | docker-compose run -e DB_URI=`docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db` app 17 | ``` 18 | + 19 | ... *Run test*: `mvn test` 20 | 21 | == Jenkins 22 | 23 | === Configure 24 | 25 | . Download Jenkins, this was tested with 2.21[http://mirrors.jenkins-ci.org/war/2.21/jenkins.war]. 26 | . Start Jenkins: `JENKINS_HOME=~/.jenkins java -jar ~/Downloads/jenkins-2.21.war --httpPort=9090` 27 | . Create First Admin User, `Save and Finish`. 28 | . Install suggested plugins 29 | . `Manage Jenkins`, `Global Tool Configuration`, configure Maven, use name `Maven3` (this name is used in `Jenkinsfile`) 30 | . `Manage Jenkins`, `Manage Plugins`, `Available`, install `CloudBees Docker Pipeline` plugin, `Install without restart`, select `Restart Jenkins` 31 | 32 | === Create Project 33 | 34 | . Create a new project of the type `Pipeline` 35 | . Configure git repo 36 | . `Build Now` 37 | 38 | == Bintray Configuration 39 | 40 | This setup is required if you want to push to bintray repository instead. 41 | 42 | === Setup Bintray repository 43 | 44 | . Create Bintray account 45 | . Find the following credentials from Profile. These will be used in Jenkins credentials later. 46 | .. Username 47 | .. API key 48 | 49 | === Configure Bintray in Jenkins 50 | 51 | . Add your credentials using `Username and Password`, use username and API key from Bintray. Use the name `bintray`, this is used in `Jenkinsfile`. 52 | . Add Maven installation directory, call it `Maven3`. This name is used in `Jenkinsfile`. 53 | . Create a new project, use `Pipeline Script from SCM` 54 | . Build the project pushes a Docker image to bintray repo 55 | 56 | -------------------------------------------------------------------------------- /webapp/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM openjdk 2 | 3 | COPY target/webapp-1.0-SNAPSHOT-jar-with-dependencies.jar /opt/webapp-1.0-SNAPSHOT-jar-with-dependencies.jar 4 | 5 | CMD java -jar /opt/webapp-1.0-SNAPSHOT-jar-with-dependencies.jar 6 | -------------------------------------------------------------------------------- /webapp/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | org.example.pipeline 5 | webapp 6 | jar 7 | 1.0-SNAPSHOT 8 | webapp 9 | http://maven.apache.org 10 | 11 | 12 | 13 | maven-assembly-plugin 14 | 15 | 16 | 17 | org.example.pipeline.Main 18 | 19 | 20 | 21 | jar-with-dependencies 22 | 23 | 24 | 25 | 26 | make-assembly 27 | package 28 | 29 | single 30 | 31 | 32 | 33 | 34 | 35 | org.codehaus.mojo 36 | exec-maven-plugin 37 | 1.5.0 38 | 39 | 40 | 41 | java 42 | 43 | 44 | 45 | 46 | org.example.pipeline.Main 47 | 48 | localhost 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | com.couchbase.client 57 | java-client 58 | 2.3.1 59 | 60 | 61 | junit 62 | junit 63 | 3.8.1 64 | test 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /webapp/src/main/java/org/example/pipeline/Database.java: -------------------------------------------------------------------------------- 1 | package org.example.pipeline; 2 | 3 | import com.couchbase.client.java.Bucket; 4 | import com.couchbase.client.java.CouchbaseCluster; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | /** 8 | * @author arungupta 9 | */ 10 | public class Database { 11 | 12 | static CouchbaseCluster cluster; 13 | static Bucket bucket; 14 | 15 | public static final CouchbaseCluster getCluster() { 16 | if (null == cluster) { 17 | System.out.println(System.getenv()); 18 | String host = System.getenv("DB_URI"); 19 | if (null == host) { 20 | host = "localhost"; 21 | System.out.println("Invalid host, setting to " + host); 22 | } 23 | System.out.println("Using host: " + host); 24 | cluster = CouchbaseCluster.create(host); 25 | } 26 | return cluster; 27 | } 28 | 29 | public static Bucket getBucket(String bucketName) { 30 | if (null == bucket) { 31 | bucket = getCluster().openBucket(bucketName, 30, TimeUnit.SECONDS); 32 | } 33 | return bucket; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /webapp/src/main/java/org/example/pipeline/Main.java: -------------------------------------------------------------------------------- 1 | package org.example.pipeline; 2 | 3 | import com.couchbase.client.java.Bucket; 4 | import com.couchbase.client.java.document.JsonDocument; 5 | import com.couchbase.client.java.document.json.JsonObject; 6 | 7 | /** 8 | * @arungupta 9 | */ 10 | public class Main { 11 | 12 | public static void main(String[] args) { 13 | Bucket bucket = Database.getBucket("books"); 14 | 15 | JsonObject jsonObject = JsonObject.create(); 16 | jsonObject.put("isbn", "978-1-4919-1889-0"); 17 | jsonObject.put("name", "Minecraft Modding with Forge"); 18 | jsonObject.put("cost", "29.99"); 19 | JsonDocument document = JsonDocument.create("minecraft", jsonObject); 20 | bucket.upsert(document); 21 | 22 | JsonDocument doc = bucket.get("minecraft"); 23 | System.out.println(doc.content()); 24 | 25 | System.out.println("Hello World!"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /webapp/src/test/java/org/example/pipeline/MainTest.java: -------------------------------------------------------------------------------- 1 | package org.example.pipeline; 2 | 3 | import com.couchbase.client.java.Bucket; 4 | import com.couchbase.client.java.document.JsonDocument; 5 | import com.couchbase.client.java.document.json.JsonObject; 6 | import junit.framework.Test; 7 | import junit.framework.TestCase; 8 | import junit.framework.TestSuite; 9 | 10 | /** 11 | * Unit test for simple App. 12 | */ 13 | public class MainTest 14 | extends TestCase { 15 | 16 | /** 17 | * Create the test case 18 | * 19 | * @param testName name of the test case 20 | */ 21 | public MainTest(String testName) { 22 | super(testName); 23 | } 24 | 25 | /** 26 | * @return the suite of tests being tested 27 | */ 28 | public static Test suite() { 29 | return new TestSuite(MainTest.class); 30 | } 31 | 32 | /** 33 | * Check if the JSON document was correctly added 34 | */ 35 | public void testDocument() { 36 | Bucket bucket = Database.getBucket("books"); 37 | 38 | JsonDocument doc = bucket.get("minecraft"); 39 | assertNotNull(doc); 40 | 41 | JsonObject jsonObject = doc.content(); 42 | assertNotNull(jsonObject); 43 | 44 | System.out.println(jsonObject); 45 | 46 | assertTrue(jsonObject.containsKey("isbn")); 47 | assertTrue(jsonObject.containsKey("name")); 48 | assertTrue(jsonObject.containsKey("cost")); 49 | 50 | assertTrue(jsonObject.getString("isbn").equals("978-1-4919-1889-0")); 51 | assertTrue(jsonObject.getString("name").equals("Minecraft Modding with Forge")); 52 | assertTrue(jsonObject.getString("cost").equals("29.99")); 53 | } 54 | } 55 | --------------------------------------------------------------------------------