Compare commits
28 Commits
feature/je
...
feature/k8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9904bca4f8 | ||
|
|
ae5ce790f4 | ||
|
|
8decc9d40e | ||
|
|
c204af2a10 | ||
|
|
9c7552b5c9 | ||
|
|
31209ff01c | ||
|
|
fd26c2e81b | ||
|
|
4e6c3e791d | ||
|
|
b9c23ee70d | ||
|
|
2f9fd78922 | ||
|
|
7848de66a2 | ||
|
|
007c036d2a | ||
|
|
3da4e695d0 | ||
|
|
b7f48cdd4a | ||
|
|
d7839d4033 | ||
|
|
af78bcdf1e | ||
|
|
5a4e789475 | ||
|
|
9969d8b2cf | ||
|
|
3e129808aa | ||
|
|
f03d95e413 | ||
|
|
c8f9061ef6 | ||
|
|
6804a58c01 | ||
|
|
5ef50d2628 | ||
|
|
1c02e35f97 | ||
|
|
f298808592 | ||
|
|
a14a9d4f9a | ||
|
|
d267e0d112 | ||
|
|
833dd401b3 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1 @@
|
|||||||
.idea/*
|
.idea/*
|
||||||
target
|
|
||||||
@@ -2,7 +2,7 @@ FROM openjdk:8-jre-alpine
|
|||||||
|
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
COPY ./target/java-maven-app-1.0-SNAPSHOT.jar /usr/app/
|
COPY ./target/java-maven-app-*.jar /usr/app/
|
||||||
WORKDIR /usr/app
|
WORKDIR /usr/app
|
||||||
|
|
||||||
ENTRYPOINT ["java", "-jar", "java-maven-app-1.0-SNAPSHOT.jar"]
|
CMD java -jar java-maven-app-*.jar
|
||||||
|
|||||||
65
Jenkinsfile
vendored
65
Jenkinsfile
vendored
@@ -1,51 +1,68 @@
|
|||||||
#!/usr/bin/env groovy
|
#!/usr/bin/env groovy
|
||||||
|
|
||||||
library identifier: 'jenkins-shared-library@master', retriever: modernSCM(
|
|
||||||
[$class: 'GitSCMSource',
|
|
||||||
remote: 'https://gitlab.com/nanuchi/jenkins-shared-library.git',
|
|
||||||
credentialsId: 'gitlab-credentials'
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
pipeline {
|
pipeline {
|
||||||
agent any
|
agent any
|
||||||
tools {
|
tools {
|
||||||
maven 'Maven'
|
maven 'Maven'
|
||||||
}
|
}
|
||||||
environment {
|
|
||||||
IMAGE_NAME = 'nanajanashia/demo-app:java-maven-2.0'
|
|
||||||
}
|
|
||||||
stages {
|
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') {
|
stage('build app') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
echo 'building application jar...'
|
echo "building the application..."
|
||||||
buildJar()
|
sh 'mvn clean package'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stage('build image') {
|
stage('build image') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
echo 'building docker image...'
|
echo "building the docker image..."
|
||||||
buildImage(env.IMAGE_NAME)
|
withCredentials([usernamePassword(credentialsId: 'docker-hub', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
|
||||||
dockerLogin()
|
sh "docker build -t nanajanashia/demo-app:${IMAGE_NAME} ."
|
||||||
dockerPush(env.IMAGE_NAME)
|
sh "echo $PASS | docker login -u $USER --password-stdin"
|
||||||
|
sh "docker push nanajanashia/demo-app:${IMAGE_NAME}"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stage('deploy') {
|
stage('deploy') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
echo 'deploying docker image to EC2...'
|
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"'
|
||||||
|
|
||||||
def shellCmd = "bash ./server-cmds.sh ${IMAGE_NAME}"
|
sh "git remote set-url origin https://${USER}:${PASS}@gitlab.com/nanuchi/java-maven-app.git"
|
||||||
def ec2Instance = "ec2-user@35.180.251.121"
|
sh 'git add .'
|
||||||
|
sh 'git commit -m "ci: version bump"'
|
||||||
sshagent(['ec2-server-key']) {
|
sh 'git push origin HEAD:jenkins-jobs'
|
||||||
sh "scp -o StrictHostKeyChecking=no server-cmds.sh ${ec2Instance}:/home/ec2-user"
|
|
||||||
sh "scp -o StrictHostKeyChecking=no docker-compose.yaml ${ec2Instance}:/home/ec2-user"
|
|
||||||
sh "ssh -o StrictHostKeyChecking=no ${ec2Instance} ${shellCmd}"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
version: '3.8'
|
|
||||||
services:
|
|
||||||
java-maven-app:
|
|
||||||
image: ${IMAGE}
|
|
||||||
ports:
|
|
||||||
- 8080:8080
|
|
||||||
postgres:
|
|
||||||
image: postgres:13
|
|
||||||
ports:
|
|
||||||
- 5432:5432
|
|
||||||
environment:
|
|
||||||
- POSTGRES_PASSWORD=my-pwd
|
|
||||||
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
|
||||||
8
pom.xml
8
pom.xml
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>com.example</groupId>
|
<groupId>com.example</groupId>
|
||||||
<artifactId>java-maven-app</artifactId>
|
<artifactId>java-maven-app</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
@@ -24,11 +24,7 @@
|
|||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
<properties>
|
|
||||||
<java.version>1.8</java.version>
|
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
|
||||||
</properties>
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
|||||||
19
script.groovy
Normal file
19
script.groovy
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
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
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
export IMAGE=$1
|
|
||||||
docker-compose -f docker-compose.yaml up --detach
|
|
||||||
echo "success"
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>MyApp</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Welcome to Java Maven Application</h1>
|
|
||||||
<!-- add image here <img src="" width="" /> -->
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
import com.example.Application;
|
|
||||||
import org.junit.Test;
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
public class AppTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testApp() {
|
|
||||||
Application myApp = new Application();
|
|
||||||
|
|
||||||
String result = myApp.getStatus();
|
|
||||||
|
|
||||||
assertEquals("OK", result);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
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