5 Commits

Author SHA1 Message Date
Nana Janashia
9904bca4f8 Update Jenkinsfile 2021-01-04 11:56:48 +00:00
Nana Janashia
ae5ce790f4 add private registry login for deplyoment 2020-12-04 13:41:04 +01:00
Nana Janashia
8decc9d40e add dockerfile 2020-12-04 13:11:51 +01:00
Nana Janashia
c204af2a10 fix repo cred id 2020-12-04 13:10:16 +01:00
Nana Janashia
9c7552b5c9 add kubectl command exec in jenkinsfile 2020-12-04 13:05:46 +01:00
11 changed files with 97 additions and 86 deletions

8
Dockerfile Normal file
View File

@@ -0,0 +1,8 @@
FROM openjdk:8-jre-alpine
EXPOSE 8080
COPY ./target/java-maven-app-*.jar /usr/app/
WORKDIR /usr/app
CMD java -jar java-maven-app-*.jar

75
Jenkinsfile vendored
View File

@@ -1,37 +1,68 @@
#!/usr/bin/env groovy
pipeline {
agent any
environment {
ANSIBLE_SERVER = "167.99.136.157"
tools {
maven 'Maven'
}
stages {
stage("copy files to ansible server") {
stage('increment version') {
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'
echo 'incrementing app version...'
sh 'mvn build-helper:parse-version versions:set \
-DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion} \
versions:commit'
def matcher = readFile('pom.xml') =~ '<version>(.+)</version>'
def version = matcher[0][1]
env.IMAGE_NAME = "$version-$BUILD_NUMBER"
}
}
}
}
}
stage("execute ansible playbook") {
stage('build app') {
steps {
script {
echo "calling ansible playbook to configure ec2 instances"
def remote = [:]
remote.name = "ansible-server"
remote.host = ANSIBLE_SERVER
remote.allowAnyHosts = true
echo "building the application..."
sh 'mvn clean package'
}
}
}
stage('build image') {
steps {
script {
echo "building the docker image..."
withCredentials([usernamePassword(credentialsId: 'docker-hub', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
sh "docker build -t nanajanashia/demo-app:${IMAGE_NAME} ."
sh "echo $PASS | docker login -u $USER --password-stdin"
sh "docker push nanajanashia/demo-app:${IMAGE_NAME}"
}
}
}
}
stage('deploy') {
steps {
script {
withKubeConfig([credentialsId: 'k8s-credentials', serverUrl: 'https://7293fae4-4c9d-4629-bc82-262d0a2b8e3c.eu-central-2.linodelke.net']) {
withCredentials([usernamePassword(credentialsId: 'docker-hub', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
sh "kubectl create secret docker-registry my-registry-key --docker-server=docker.io --docker-username=$USER --docker-password=$PASS"
}
sh 'envsubst < kubernetes/deployment.yaml | kubectl apply -f -'
}
}
}
}
stage('commit version update') {
steps {
script {
withCredentials([usernamePassword(credentialsId: 'gitlab-credentials', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
// git config here for the first time run
sh 'git config --global user.email "jenkins@example.com"'
sh 'git config --global user.name "jenkins"'
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"
sh "git remote set-url origin https://${USER}:${PASS}@gitlab.com/nanuchi/java-maven-app.git"
sh 'git add .'
sh 'git commit -m "ci: version bump"'
sh 'git push origin HEAD:jenkins-jobs'
}
}
}

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

View File

@@ -0,0 +1,25 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: java-maven-app
labels:
app: java-maven-app
spec:
replicas: 2
selector:
matchLabels:
app: java-maven-app
template:
metadata:
labels:
app: java-maven-app
spec:
imagePullSecrets:
- name: my-registry-key
containers:
- name: java-maven-app
image: nanajanashia/demo-app:$IMAGE_NAME
imagePullPolicy: Always
ports:
- containerPort: 8080

11
kubernetes/service.yaml Normal file
View File

@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: java-maven-app
spec:
selector:
app: java-maven-app
ports:
- protocol: TCP
port: 80
targetPort: 8080

11
pom.xml
View File

@@ -22,17 +22,6 @@
</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>

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

Binary file not shown.

Binary file not shown.