12 Commits

Author SHA1 Message Date
Nana Janashia
d7e9360e2c fix 2020-11-08 17:28:39 +01:00
Nana Janashia
6b1d998791 add library def inside Jenkinsfile 2020-11-08 17:25:26 +01:00
Nana Janashia
922c8df4c6 add ind commands 2020-11-08 17:12:58 +01:00
Nana Janashia
946909a1b0 add param to dockerbuild 2020-11-08 16:41:07 +01:00
Nana Janashia
5aab8caed6 fix lib call 2020-11-08 16:30:28 +01:00
Nana Janashia
63a5e576a8 fix lib call 2020-11-08 16:27:28 +01:00
Nana Janashia
aa6f394e65 fix lib call 2020-11-08 16:20:19 +01:00
Nana Janashia
15a05dc886 Update Jenkinsfile 2020-11-08 15:15:26 +00:00
Nana Janashia
81b557f424 Update Jenkinsfile 2020-11-08 15:14:14 +00:00
Nana Janashia
6e6e1fdcb6 Update Jenkinsfile 2020-11-08 15:12:19 +00:00
Nana Janashia
75f780bd0c fix lib call 2020-11-08 16:05:48 +01:00
Nana Janashia
6a5572a5f7 call library functions in jenkinsfile 2020-11-08 15:58:31 +01:00
8 changed files with 47 additions and 124 deletions

8
Dockerfile Normal file
View File

@@ -0,0 +1,8 @@
FROM openjdk:8-jre-alpine
EXPOSE 8080
COPY ./target/java-maven-app-1.0-SNAPSHOT.jar /usr/app/
WORKDIR /usr/app
ENTRYPOINT ["java", "-jar", "java-maven-app-1.0-SNAPSHOT.jar"]

58
Jenkinsfile vendored
View File

@@ -1,38 +1,48 @@
#!/usr/bin/env groovy
library identifier: 'jenkins-shared-library@master', retriever: modernSCM(
[$class: 'GitSCMSource',
remote: 'https://gitlab.com/nanuchi/jenkins-shared-library.git',
credentialsId: 'gitlab-credentials'
]
)
def gv
pipeline {
agent any
environment {
ANSIBLE_SERVER = "167.99.136.157"
tools {
maven 'Maven'
}
stages {
stage("copy files to ansible server") {
stage("init") {
steps {
script {
echo "copying all neccessary files to ansible control node"
sshagent(['ansible-server-key']) {
sh "scp -o StrictHostKeyChecking=no ansible/* root@${ANSIBLE_SERVER}:/root"
withCredentials([sshUserPrivateKey(credentialsId: 'ec2-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) {
sh 'scp $keyfile root@$ANSIBLE_SERVER:/root/ssh-key.pem'
gv = load "script.groovy"
}
}
}
}
}
stage("execute ansible playbook") {
stage("build jar") {
steps {
script {
echo "calling ansible playbook to configure ec2 instances"
def remote = [:]
remote.name = "ansible-server"
remote.host = ANSIBLE_SERVER
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId: 'ansible-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]){
remote.user = user
remote.identityFile = keyfile
sshScript remote: remote, script: "prepare-ansible-server.sh"
sshCommand remote: remote, command: "ansible-playbook my-playbook.yaml"
}
buildJar()
}
}
}
stage("build and push image") {
steps {
script {
buildImage 'nanajanashia/demo-app:jma-3.0'
dockerLogin()
dockerPush 'nanajanashia/demo-app:jma-3.0'
}
}
}
stage("deploy") {
steps {
script {
gv.deployApp()
}
}
}

View File

@@ -1,9 +0,0 @@
[defaults]
host_key_checking = False
inventory = inventory_aws_ec2.yaml
interpreter_python = /usr/bin/python3
enable_plugins = aws_ec2
remote_user = ec2-user
private_key_file = ~/ssh-key.pem

View File

@@ -1,9 +0,0 @@
---
plugin: aws_ec2
regions:
- eu-west-3
keyed_groups:
- key: tags
prefix: tag
- key: instance_type
prefix: instance_type

View File

@@ -1,29 +0,0 @@
---
- name: Install python3, docker, docker-compose
hosts: all
become: yes
gather_facts: False
tasks:
- name: Install python3 and docker
vars:
ansible_python_interpreter: /usr/bin/python
yum:
name:
- python3
- docker
update_cache: yes
state: present
- name: Install Docker-compose
get_url:
url: https://github.com/docker/compose/releases/download/1.27.4/docker-compose-Linux-{{lookup('pipe', 'uname -m')}}
dest: /usr/local/bin/docker-compose
mode: +x
- name: Start docker daemon
systemd:
name: docker
state: started
- name: Install docker python module
pip:
name:
- docker
- docker-compose

30
pom.xml
View File

@@ -6,35 +6,7 @@
<groupId>com.example</groupId>
<artifactId>java-maven-app</artifactId>
<version>1.1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.5.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- to handle any Java version mismatch, add the following configuration for maven-compiler-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>

View File

@@ -1,6 +0,0 @@
#!/usr/bin/env bash
apt update
apt install ansible -y
apt install python3-pip -y
pip3 install boto3 botocore

View File

@@ -1,17 +1,3 @@
def buildJar() {
echo "building the application..."
sh 'mvn package'
}
def buildImage() {
echo "building the docker image..."
withCredentials([usernamePassword(credentialsId: 'docker-hub-repo', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
sh 'docker build -t nanajanashia/demo-app:jma-2.0 .'
sh "echo $PASS | docker login -u $USER --password-stdin"
sh 'docker push nanajanashia/demo-app:jma-2.0'
}
}
def deployApp() {
echo 'deploying the application...'
}