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
|
||||
75
Jenkinsfile
vendored
75
Jenkinsfile
vendored
@@ -1,40 +1,71 @@
|
||||
#!/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('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('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("execute ansible playbook") {
|
||||
stage('commit version update') {
|
||||
steps {
|
||||
script {
|
||||
echo "calling ansible playbook to configure ec2 instances"
|
||||
def remote = [:]
|
||||
remote.name = "ansible-server"
|
||||
remote.host = ANSIBLE_SERVER
|
||||
remote.allowAnyHosts = true
|
||||
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'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
</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>
|
||||
|
||||
|
||||
@@ -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