Compare commits
9 Commits
feature/k8
...
feature/an
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
45e509506b | ||
|
|
3b6780360c | ||
|
|
3208a0e63e | ||
|
|
c70bfe4bbd | ||
|
|
2b7f3b1471 | ||
|
|
9573355ec3 | ||
|
|
851ef30959 | ||
|
|
feda16a9a8 | ||
|
|
0c2ab7794d |
@@ -1,8 +0,0 @@
|
|||||||
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
|
|
||||||
85
Jenkinsfile
vendored
85
Jenkinsfile
vendored
@@ -1,68 +1,37 @@
|
|||||||
#!/usr/bin/env groovy
|
|
||||||
|
|
||||||
pipeline {
|
pipeline {
|
||||||
agent any
|
agent any
|
||||||
tools {
|
environment {
|
||||||
maven 'Maven'
|
ANSIBLE_SERVER = "167.99.136.157"
|
||||||
}
|
}
|
||||||
stages {
|
stages {
|
||||||
stage('increment version') {
|
stage("copy files to ansible server") {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
echo 'incrementing app version...'
|
echo "copying all neccessary files to ansible control node"
|
||||||
sh 'mvn build-helper:parse-version versions:set \
|
sshagent(['ansible-server-key']) {
|
||||||
-DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion} \
|
sh "scp -o StrictHostKeyChecking=no ansible/* root@${ANSIBLE_SERVER}:/root"
|
||||||
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('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"'
|
|
||||||
|
|
||||||
sh "git remote set-url origin https://${USER}:${PASS}@gitlab.com/nanuchi/java-maven-app.git"
|
withCredentials([sshUserPrivateKey(credentialsId: 'ec2-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) {
|
||||||
sh 'git add .'
|
sh 'scp $keyfile root@$ANSIBLE_SERVER:/root/ssh-key.pem'
|
||||||
sh 'git commit -m "ci: version bump"'
|
}
|
||||||
sh 'git push origin HEAD:jenkins-jobs'
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage("execute ansible playbook") {
|
||||||
|
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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
9
ansible/ansible.cfg
Normal file
9
ansible/ansible.cfg
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[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
|
||||||
9
ansible/inventory_aws_ec2.yaml
Normal file
9
ansible/inventory_aws_ec2.yaml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
---
|
||||||
|
plugin: aws_ec2
|
||||||
|
regions:
|
||||||
|
- eu-west-3
|
||||||
|
keyed_groups:
|
||||||
|
- key: tags
|
||||||
|
prefix: tag
|
||||||
|
- key: instance_type
|
||||||
|
prefix: instance_type
|
||||||
29
ansible/my-playbook.yaml
Normal file
29
ansible/my-playbook.yaml
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
---
|
||||||
|
- 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
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
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
|
|
||||||
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
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,6 +22,17 @@
|
|||||||
</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>
|
||||||
|
|
||||||
|
|||||||
6
prepare-ansible-server.sh
Normal file
6
prepare-ansible-server.sh
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#!/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.
Reference in New Issue
Block a user