27 Commits

Author SHA1 Message Date
Nana Janashia
76a9356555 fix 2021-04-04 09:39:09 +02:00
Nana Janashia
24827f1eaa fix 2021-04-03 22:33:02 +02:00
Nana Janashia
8119d609aa fix 2021-04-03 22:21:16 +02:00
Nana Janashia
87f0379969 fix 2021-04-03 22:17:35 +02:00
Nana Janashia
897dbc06ed fix 2021-04-03 22:12:18 +02:00
Nana Janashia
04f497e6b8 Fix 2021-04-03 22:07:09 +02:00
Nana Janashia
9bb9220f0d Dx 2021-04-03 22:01:22 +02:00
Nana Janashia
009e4e7cb3 Fix setting creds 2021-04-03 19:03:39 +02:00
Nana Janashia
f883a86564 add server script 2021-04-03 18:57:36 +02:00
Nana Janashia
3e561bbf2e Add commands 2021-04-03 18:41:58 +02:00
Nana Janashia
7061560918 Add python boto lib installation 2021-04-03 18:39:08 +02:00
Nana Janashia
78fbc1f580 Add playbook execution 2021-04-03 18:31:42 +02:00
Nana Janashia
3c9afc11b1 Fix secret value leak warning 2021-04-03 18:29:22 +02:00
Nana Janashia
d8dda2f0ce Add copying ssh key for ec2 2021-04-03 18:13:49 +02:00
Nana Janashia
533c4ba7da add correct user on remote 2021-04-03 18:05:00 +02:00
Nana Janashia
610c123802 Add host check disabling 2021-04-03 18:03:31 +02:00
Nana Janashia
0e2f79cbcd Fix with sshagent 2021-04-03 17:56:45 +02:00
Nana Janashia
f5aa1df5d3 Add copying to remote 2021-04-03 17:53:39 +02:00
Nana Janashia
c727d27e81 Fix 2021-04-03 15:07:22 +02:00
Nana Janashia
a9a8f554e8 Add required name attribite to remote 2021-04-03 14:22:41 +02:00
Nana Janashia
91a42fc5d7 Fix 2021-04-03 14:21:36 +02:00
Nana Janashia
d6fd074273 Fix 2021-04-03 14:19:10 +02:00
Nana Janashia
7a72b254ed Fix user var 2021-04-03 14:07:16 +02:00
Nana Janashia
8ae83c5555 Initi commit 2021-04-03 14:03:51 +02:00
Nana Janashia
851ef30959 Update pom.xml 2021-03-24 14:38:33 +00:00
Nana Janashia
feda16a9a8 Delete freestyle-build.sh 2021-01-01 14:25:12 +00:00
Nana Janashia
0c2ab7794d Add new file 2021-01-01 14:23:51 +00:00
15 changed files with 202 additions and 134 deletions

View File

@@ -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

71
Jenkinsfile vendored
View File

@@ -1,71 +0,0 @@
#!/usr/bin/env groovy
pipeline {
agent any
tools {
maven 'Maven'
}
stages {
stage('increment version') {
steps {
script {
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('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"
sh 'git add .'
sh 'git commit -m "ci: version bump"'
sh 'git push origin HEAD:jenkins-jobs'
}
}
}
}
}
}

52
Jenkinsfile-v1 Normal file
View File

@@ -0,0 +1,52 @@
pipeline {
agent any
environment {
ANSIBLE_SERVER = "157.230.120.252"
}
stages {
stage("copy ansible folder and ec2 access key to ansible-server") {
steps {
script {
sshagent(['ansible-server-key']) {
echo "copying ansible folder to ansible server"
// ${ANSIBLE_SERVER}:/root without root will give jenkins@${ANSIBLE_SERVER}:/root
sh "scp -o StrictHostKeyChecking=no ansible/* root@${ANSIBLE_SERVER}:/root"
echo "copying ssh keys for ec2 instances"
withCredentials([sshUserPrivateKey(credentialsId: 'ec2-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) {
sh 'scp $keyfile root@$ANSIBLE_SERVER:/root/ssh-key.pem'
}
}
}
}
}
stage("execute ansible playbook from the ansible-server") {
environment {
AWS_ACCESS_KEY_ID = credentials('jenkins_aws_access_key_id')
AWS_SECRET_ACCESS_KEY = credentials('jenkins_aws_secret_access_key')
}
steps {
script {
echo "executing ansible-playbook"
def remote = [:]
remote.name = "ansible-server"
remote.host = ANSIBLE_SERVER
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId: 'ansible-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) {
remote.identityFile = keyfile
remote.user = user
sshCommand remote: remote, command: "ls -l"
// set AWS credentials
sshScript remote: remote, script: "ansible/prepare-server.sh"
sshCommand remote: remote, command: "export AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}; export AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}; ansible-playbook docker-and-compose.yaml"
}
}
}
}
}
}

48
Jenkinsfile-v2 Normal file
View File

@@ -0,0 +1,48 @@
pipeline {
agent any
environment {
ANSIBLE_SERVER = "157.230.120.252"
}
stages {
stage("copy ansible folder and ec2 access key to ansible-server") {
steps {
script {
sshagent(['ansible-server-key']) {
echo "copying ansible folder to ansible server"
// ${ANSIBLE_SERVER}:/root without root will give jenkins@${ANSIBLE_SERVER}:/root
sh "scp -o StrictHostKeyChecking=no ansible/* root@${ANSIBLE_SERVER}:/root"
echo "copying ssh keys for ec2 instances"
withCredentials([sshUserPrivateKey(credentialsId: 'ec2-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) {
sh 'scp $keyfile root@$ANSIBLE_SERVER:/root/ssh-key.pem'
}
}
}
}
}
stage("execute ansible playbook from the ansible-server") {
environment {
AWS_ACCESS_KEY_ID = credentials('jenkins_aws_access_key_id')
AWS_SECRET_ACCESS_KEY = credentials('jenkins_aws_secret_access_key')
}
steps {
script {
echo "executing ansible-playbook"
def remote = [:]
remote.name = "ansible-server"
remote.host = ANSIBLE_SERVER
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId: 'ansible-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) {
remote.identityFile = keyfile
remote.user = user
sshCommand remote: remote, command: "ls -l"
sshCommand remote: remote, command: "ansible-playbook docker-and-compose.yaml"
}
}
}
}
}
}

34
Jenkinsfile-v3 Normal file
View File

@@ -0,0 +1,34 @@
def gv
pipeline {
agent any
stages {
stage("build jar") {
steps {
script {
echo "building the application..."
sh 'mvn package'
}
}
}
stage("build image") {
steps {
script {
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'
}
}
}
}
stage("deploy") {
steps {
script {
echo 'deploying the application...'
}
}
}
}
}

9
ansible/ansible.cfg Normal file
View 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

View 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

View File

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

10
ansible/prepare-server.sh Normal file
View File

@@ -0,0 +1,10 @@
#!/usr/bin/env bash
apt update
apt install ansible -y
apt install python3-pip -y
pip3 install boto3 botocore
# export AWS_ACCESS_KEY_ID=$1
# export AWS_SECRET_ACCESS_KEY=$2

View File

@@ -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

View File

@@ -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
View File

@@ -22,6 +22,17 @@
</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,19 +0,0 @@
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...'
}
return this

Binary file not shown.