cicd-java-spring-boot/Jenkinsfile
2023-02-01 22:39:33 +00:00

109 lines
3.6 KiB
Groovy

import org.gradle.tooling.GradleConnector
import org.gradle.tooling.ProjectConnection
import org.gradle.tooling.model.GradleProject
def getIncludedProjectNamesMap(Project project) {
def projectNamesMap = new HashMap<String, String>()
project.gradle.includedBuilds.each { includedBuild ->
ProjectConnection connection = GradleConnector.newConnector()
.forProjectDirectory(includedBuild.projectDir)
.connect()
GradleProject includedProject = connection.getModel(GradleProject.class);
def name = includedProject.getName();
connection.close();
projectNamesMap.put includedBuild.name, name;
}
return projectNamesMap
}
def label = "worker-${UUID.randomUUID().toString()}"
podTemplate(
label: label,
containers: [
containerTemplate(
name: 'gradle',
image: 'gradle:7.6.0-jdk11',
command: 'sleep',
args: '30d'
),
containerTemplate(
name: 'docker',
image: 'docker:latest',
command: 'cat',
ttyEnabled: true
),
containerTemplate(
name: 'kubectl',
image: 'bitnami/kubectl:latest',
command: 'cat',
ttyEnabled: true
),
containerTemplate(
name: 'helm',
image: 'alpine/helm:latest',
command: 'cat',
ttyEnabled: true
)
],
volumes: [
hostPathVolume(mountPath: '/home/gradle/.gradle', hostPath: '/tmp/jenkins/.gradle'),
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
]
)
{
node(label) {
def myRepo = checkout scm
def gitCommit = myRepo.GIT_COMMIT
def gitBranch = myRepo.GIT_BRANCH
def shortGitCommit = "${gitCommit[0..10]}"
def previousGitCommit = sh(script: "git rev-parse ${gitCommit}~", returnStdout: true)
def docker_registry_url = "harbor.loafle.net"
def app_version_value = sh(returnStdout: true, script: "cat build.gradle | grep -o 'version = [^,]*'").trim()
sh "echo Project in version value: $app_version_value"
def app_version = app_version_value.split(/=/)[1].trim()
println getIncludedProjectNamesMap(project)
stage('Test') {
try {
container('gradle') {
sh """
pwd
echo "GIT_BRANCH=${gitBranch}" >> /etc/environment
echo "GIT_COMMIT=${gitCommit}" >> /etc/environment
gradle test
"""
}
}
catch (exc) {
println "Failed to test - ${currentBuild.fullDisplayName}"
throw(exc)
}
}
stage('Build') {
container('gradle') {
sh "gradle build"
}
}
stage('Create Docker images') {
container('docker') {
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'harbor.loafle.net',
usernameVariable: 'HARBOR_USER',
passwordVariable: 'HARBOR_PASSWORD']]) {
sh """
docker login -u "${HARBOR_USER}" -p "${HARBOR_PASSWORD}" ${docker_registry_url}
docker build -t ${docker_registry_url}/prototype/cicd-java-spring-boot:${app_version} --build-arg JAR_NAME=cicd-${app_version}.jar .
docker push ${docker_registry_url}/prototype/cicd-java-spring-boot:${app_version}
"""
}
}
}
}
}