Skip to content

Commit

Permalink
Merge pull request #158 from dario-rodriguez/cicd-and-deployment
Browse files Browse the repository at this point in the history
Cicd and deployment
  • Loading branch information
sjimenez77 authored Dec 11, 2018
2 parents cf995b3 + fdad556 commit 2a53458
Show file tree
Hide file tree
Showing 31 changed files with 615 additions and 43 deletions.
12 changes: 12 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Use them at your own risk.

== Deployment

=== From code

This application can be easily deployed using **Docker** and **docker-compose**.

`$ docker-compose up`
Expand All @@ -64,3 +66,13 @@ fdb63e26d299 mythaistar_angular "nginx -g 'daemon of…" 3 minu
```

The usage of the `reverse-proxy` only uses 1 port of the Docker host (where this is deployed), the `8080`. All internal communication of containers is done using docker alias of services.

=== From artifact

If we are using a CICD pipeline and we store the artifact on nexus, we can also deploy it without recompile all code. For this purpose there are three deployment pipelines:

- deployment: deploy all application with a reserve-proxy
- java: deploy only the java application
- angular: deploy angular application + reverse-proxy (you must run the java deployment at least once before running this deployment)

The result of this deployment will be the same as in the deployment from code, but instead of compiling the artifact again downloads it from nexus. All resources (docker-compose.yml, Dockerfiles and nginx.conf) are stored in the reverse-proxy folder.
8 changes: 4 additions & 4 deletions angular/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ COPY . /app
RUN npm i -g @angular/cli
RUN apk update && apk add yarn
RUN yarn
RUN ng build --configuration=docker --prod --build-optimizer
RUN yarn build --configuration=docker

# 2. Deploy
FROM nginx:latest
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
apache2-utils \
&& rm -rf /var/lib/apt/lists/*
&& apt-get install -y --no-install-recommends \
apache2-utils \
&& rm -rf /var/lib/apt/lists/*
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/dist/. /usr/share/nginx/html
EXPOSE 80 443
Expand Down
2 changes: 1 addition & 1 deletion angular/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mythaistar-restaurant",
"version": "1.12.0",
"version": "1.12.2",
"main": "main.js",
"scripts": {
"postinstall": "npm run postinstall:electron && npx electron-builder install-app-deps",
Expand Down
7 changes: 5 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
version: '2'
version: '3'

services:
reverse-proxy:
build: 'reverse-proxy/'
restart: always
container_name: 'mts_reverse_proxy'
image: my-thai-star/reverse-proxy:latest
networks:
- my-thai-star
ports:
Expand All @@ -13,14 +14,16 @@ services:
build: 'angular/'
restart: always
container_name: 'mts_angular'
image: my-thai-star/angular:latest
networks:
- my-thai-star
java:
build: 'java/'
restart: always
container_name: 'mts_java'
image: my-thai-star/java:latest
networks:
- my-thai-star
networks:
my-thai-star:
driver: bridge
driver: bridge
2 changes: 1 addition & 1 deletion java/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 1. Build
FROM maven:3.5-jdk-8-alpine AS build
WORKDIR /app
COPY mtsj/. /app
COPY mtsj/ /app
RUN ls -l
RUN mvn install

Expand Down
11 changes: 6 additions & 5 deletions java/mtsj/api/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.devonfw.java.mtsj</groupId>
<artifactId>mtsj</artifactId>
<version>0.1-SNAPSHOT</version>
<version>1.12.2-SNAPSHOT</version>
</parent>
<artifactId>mtsj-api</artifactId>
<packaging>jar</packaging>
Expand All @@ -31,7 +32,7 @@
<artifactId>devon4j-jpa-envers</artifactId>
</dependency>

<!-- <dependency>
<!-- <dependency>
<groupId>com.devonfw.java.modules</groupId>
<artifactId>devon4j-jpa</artifactId>
</dependency> -->
Expand All @@ -40,7 +41,7 @@
<groupId>com.devonfw.java.modules</groupId>
<artifactId>devon4j-security</artifactId>
</dependency>
<dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
Expand Down
6 changes: 3 additions & 3 deletions java/mtsj/batch/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.devonfw.java.mtsj</groupId>
<artifactId>mtsj</artifactId>
<version>0.1-SNAPSHOT</version>
<version>1.12.2-SNAPSHOT</version>
</parent>
<artifactId>mtsj-batch</artifactId>
<packaging>jar</packaging>
Expand Down
8 changes: 4 additions & 4 deletions java/mtsj/core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.devonfw.java.mtsj</groupId>
<artifactId>mtsj</artifactId>
<version>0.1-SNAPSHOT</version>
<version>1.12.2-SNAPSHOT</version>
</parent>
<artifactId>mtsj-core</artifactId>
<packaging>jar</packaging>
Expand All @@ -15,7 +15,7 @@
<properties>
<java.version>1.8</java.version>
<mts.database.name>h2</mts.database.name>
<mts.database.profile>h2mem</mts.database.profile> <!-- profile for spring -->
<mts.database.profile>h2mem</mts.database.profile> <!-- profile for spring -->
<mts.database.groupId>com.h2database</mts.database.groupId>
<mts.database.artifactId>h2</mts.database.artifactId>
<mts.database.version>1.4.194</mts.database.version>
Expand Down
2 changes: 1 addition & 1 deletion java/mtsj/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<artifactId>mtsj</artifactId>
<groupId>com.devonfw.java.mtsj</groupId>
<version>0.1-SNAPSHOT</version>
<version>1.12.2-SNAPSHOT</version>
<packaging>pom</packaging>
<name>${project.artifactId}</name>
<description>Application based on the Devonfw framework for Java (devon4j).</description>
Expand Down
8 changes: 4 additions & 4 deletions java/mtsj/server/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.devonfw.java.mtsj</groupId>
<artifactId>mtsj</artifactId>
<version>0.1-SNAPSHOT</version>
<version>1.12.2-SNAPSHOT</version>
</parent>
<artifactId>mtsj-server</artifactId>
<packaging>war</packaging>
Expand Down Expand Up @@ -122,7 +122,7 @@

<build>
<finalName>${server.war.name}</finalName>
<resources> <!-- Same as above - this is dead stuff that can be removed. -->
<resources> <!-- Same as above - this is dead stuff that can be removed. -->
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
Expand Down
17 changes: 17 additions & 0 deletions jenkins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ In order to run all pipelines, we have installed the following plugins (maybe no
- Xvnc plugin (xvnc)
- Dashboard for Blue Ocean (blueocean-dashboard)
- Script Security Plugin (script-security)
- **HTTP Request Plugin (http_request)**

Most of them come pre-installed with the production line instance. The ones we need are the ones related to the pipeline and the ones in bold.

Expand All @@ -169,6 +170,22 @@ Most of them come pre-installed with the production line instance. The ones we n
You only need to create a new pipeline and modify the configuration following the image:
![](./pipeline-config.png)

Or you can execute the create-pipelines.sh as follows:

```bash
$ ./create-pipelines.sh <jenkinsUrl> <username> <password>
```

example:

```bash
$ ./create-pipelines.sh devon.s2-eu.capgemini.com/jenkins devonfw capgemini
```

**Important:** you need a bash shell in order to execute the command. If you are using windows, you can open a new bash shell in the devonfw console:
![console](./console.png)


**TIP**: All environment variables used on both Jenkinsfiles should be declared in the correspondant Jenkins Pipeline configuration more or less like this:

![](./jenkins-pipelines-params.png)
2 changes: 1 addition & 1 deletion jenkins/angular/cicd/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pipeline{

environment {
// Script for build the application. Defined at package.json
buildScript = 'build:prodcompose'
buildScript = 'build --configuration=docker'
// Script for lint the application. Defined at package.json
lintScript = 'lint'
// Script for test the application. Defined at package.json
Expand Down
28 changes: 28 additions & 0 deletions jenkins/angular/cicd/pipeline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version='1.1' encoding='UTF-8'?>
<flow-definition plugin="[email protected]">
<description></description>
<keepDependencies>false</keepDependencies>
<properties/>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin="[email protected]">
<scm class="hudson.plugins.git.GitSCM" plugin="[email protected]">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>https://github.com/dario-rodriguez/my-thai-star.git</url>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>*/cicd-and-deployment</name>
</hudson.plugins.git.BranchSpec>
</branches>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<submoduleCfg class="list"/>
<extensions/>
</scm>
<scriptPath>jenkins/angular/cicd/Jenkinsfile</scriptPath>
<lightweight>true</lightweight>
</definition>
<triggers/>
<disabled>false</disabled>
</flow-definition>
92 changes: 92 additions & 0 deletions jenkins/angular/deployment/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
pipeline{
agent any

options {
buildDiscarder(logRotator(artifactDaysToKeepStr: '1', artifactNumToKeepStr: '1', daysToKeepStr: '5', numToKeepStr: '50'))
// Disable concurrent builds. It will wait until the pipeline finish before start a new one
disableConcurrentBuilds()
}

environment {
// Jenkins credentials id for ssh agent
sshAgentCredentials = '3d0fa2a4-5cf0-4cf5-a3fd-23655eb33c11'

// Nexus3 API url
nexusApiUrl = 'devon.s2-eu.capgemini.com/nexus3/'
// Maven repository
repository = 'snapshots'
// Repository format
format = 'maven2'
// Artifact group
group = 'com.devonfw.mythaistar'
// Application name
name = 'mythaistar-restaurant'
// Artifact extension
extension = 'zip'
}

parameters {
string(name: 'VERSION', defaultValue: '1.12.0-SNAPSHOT', description: 'Version number')
string(name: 'EXTERNAL_SERVER_IP', defaultValue: '10.40.235.244', description: 'Server IP')
string(name: 'APPLICATION_DIR', defaultValue: '/root/mythaistar/reverse-proxy/', description: 'My Thai Star application directory')
}

stages {
stage ('Download artifact from Nexus') {
steps {
script {
// Download artifact from nexus3 using the nexus3 beta api
withCredentials([usernamePassword(credentialsId: 'pl-technical-user', passwordVariable: 'pass', usernameVariable: 'user')]) {
// Search the list of artifacts
def response = httpRequest """https://${user}:${pass}@${nexusApiUrl}service/rest/beta/search/assets?repository=${repository}&format=${format}&group=${group}&name=${name}&maven.groupId=${group}&maven.artifactId=${name}&maven.baseVersion=${params.VERSION}&maven.extension=${extension}"""
def props = readJSON text: response.content

// Get the last snapshot download url
def num = -1
def url = ''
props.items.each {
def n = (it.downloadUrl =~ /.*-(\d*)\.zip/)[0][1]
if (n > num) {
num = n
url = it.downloadUrl
}
}

// Download the snapshot
sh """wget -O ${name}-${params.VERSION}.${extension} ${url.replace('https://','https://'+user+':'+pass+'@')}"""

// Unzip the angular application
sh "mkdir -p dist"
unzip dir: 'dist', zipFile: """${name}-${params.VERSION}.${extension}"""
}
}
}
}

stage ('Deployment') {
steps {
script {
dir('dist'){
sshagent (credentials: [sshAgentCredentials]) {
sh """
# Copy resulting "dist" folder from workspace to deployment server
ssh -o StrictHostKeyChecking=no root@${params.EXTERNAL_SERVER_IP} mkdir -p ${params.APPLICATION_DIR}angular/dist/
ssh -o StrictHostKeyChecking=no root@${params.EXTERNAL_SERVER_IP} rm -r ${params.APPLICATION_DIR}angular/dist/* 2> /dev/null
scp -o StrictHostKeyChecking=no -r . root@${params.EXTERNAL_SERVER_IP}:${params.APPLICATION_DIR}angular/dist/
# Launch application in Docker container
ssh -o StrictHostKeyChecking=no root@${params.EXTERNAL_SERVER_IP} docker-compose -f ${params.APPLICATION_DIR}docker-compose.yml up -d --build
"""
}
}
}
}
}
}

post {
always {
cleanWs()
}
}
}
28 changes: 28 additions & 0 deletions jenkins/angular/deployment/pipeline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version='1.1' encoding='UTF-8'?>
<flow-definition plugin="[email protected]">
<description></description>
<keepDependencies>false</keepDependencies>
<properties/>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin="[email protected]">
<scm class="hudson.plugins.git.GitSCM" plugin="[email protected]">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>https://github.com/dario-rodriguez/my-thai-star.git</url>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>*/cicd-and-deployment</name>
</hudson.plugins.git.BranchSpec>
</branches>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<submoduleCfg class="list"/>
<extensions/>
</scm>
<scriptPath>jenkins/deployment/cicd/Jenkinsfile</scriptPath>
<lightweight>true</lightweight>
</definition>
<triggers/>
<disabled>false</disabled>
</flow-definition>
Binary file added jenkins/console.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2a53458

Please sign in to comment.