Compare commits
5 Commits
feature/an
...
feature/k8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9904bca4f8 | ||
|
|
ae5ce790f4 | ||
|
|
8decc9d40e | ||
|
|
c204af2a10 | ||
|
|
9c7552b5c9 |
8
Dockerfile
Normal file
8
Dockerfile
Normal 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
|
||||||
73
Jenkinsfile
vendored
73
Jenkinsfile
vendored
@@ -1,37 +1,68 @@
|
|||||||
|
#!/usr/bin/env groovy
|
||||||
|
|
||||||
pipeline {
|
pipeline {
|
||||||
agent any
|
agent any
|
||||||
environment {
|
tools {
|
||||||
ANSIBLE_SERVER = "167.99.136.157"
|
maven 'Maven'
|
||||||
}
|
}
|
||||||
stages {
|
stages {
|
||||||
stage("copy files to ansible server") {
|
stage('increment version') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
echo "copying all neccessary files to ansible control node"
|
echo 'incrementing app version...'
|
||||||
sshagent(['ansible-server-key']) {
|
sh 'mvn build-helper:parse-version versions:set \
|
||||||
sh "scp -o StrictHostKeyChecking=no ansible/* root@${ANSIBLE_SERVER}:/root"
|
-DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion} \
|
||||||
|
versions:commit'
|
||||||
withCredentials([sshUserPrivateKey(credentialsId: 'ec2-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) {
|
def matcher = readFile('pom.xml') =~ '<version>(.+)</version>'
|
||||||
sh 'scp $keyfile root@$ANSIBLE_SERVER:/root/ssh-key.pem'
|
def version = matcher[0][1]
|
||||||
}
|
env.IMAGE_NAME = "$version-$BUILD_NUMBER"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('build app') {
|
||||||
|
steps {
|
||||||
|
script {
|
||||||
|
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("execute ansible playbook") {
|
stage('deploy') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
echo "calling ansible playbook to configure ec2 instances"
|
withKubeConfig([credentialsId: 'k8s-credentials', serverUrl: 'https://7293fae4-4c9d-4629-bc82-262d0a2b8e3c.eu-central-2.linodelke.net']) {
|
||||||
def remote = [:]
|
withCredentials([usernamePassword(credentialsId: 'docker-hub', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
|
||||||
remote.name = "ansible-server"
|
sh "kubectl create secret docker-registry my-registry-key --docker-server=docker.io --docker-username=$USER --docker-password=$PASS"
|
||||||
remote.host = ANSIBLE_SERVER
|
}
|
||||||
remote.allowAnyHosts = true
|
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')]){
|
sh "git remote set-url origin https://${USER}:${PASS}@gitlab.com/nanuchi/java-maven-app.git"
|
||||||
remote.user = user
|
sh 'git add .'
|
||||||
remote.identityFile = keyfile
|
sh 'git commit -m "ci: version bump"'
|
||||||
sshScript remote: remote, script: "prepare-ansible-server.sh"
|
sh 'git push origin HEAD:jenkins-jobs'
|
||||||
sshCommand remote: remote, command: "ansible-playbook my-playbook.yaml"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
---
|
|
||||||
plugin: aws_ec2
|
|
||||||
regions:
|
|
||||||
- eu-west-3
|
|
||||||
keyed_groups:
|
|
||||||
- key: tags
|
|
||||||
prefix: tag
|
|
||||||
- key: instance_type
|
|
||||||
prefix: instance_type
|
|
||||||
@@ -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
|
|
||||||
25
kubernetes/deployment.yaml
Normal file
25
kubernetes/deployment.yaml
Normal 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
11
kubernetes/service.yaml
Normal 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
11
pom.xml
@@ -22,17 +22,6 @@
|
|||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</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>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
apt update
|
|
||||||
apt install ansible -y
|
|
||||||
apt install python3-pip -y
|
|
||||||
pip3 install boto3 botocore
|
|
||||||
BIN
target/java-maven-app-1.1.1.jar
Normal file
BIN
target/java-maven-app-1.1.1.jar
Normal file
Binary file not shown.
BIN
target/java-maven-app-1.1.1.jar.original
Normal file
BIN
target/java-maven-app-1.1.1.jar.original
Normal file
Binary file not shown.
Reference in New Issue
Block a user