cicd-java-spring-boot/Jenkinsfile
2023-02-01 23:22:44 +00:00

90 lines
3.0 KiB
Groovy

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 dockerRegistryUrl = "harbor.loafle.net"
stage('Test') {
try {
container('gradle') {
def projectDetails = sh(returnStdout: true, script: "gradle -q projectDetails").trim()
sh """
pwd
echo "GIT_BRANCH=${gitBranch}" >> /etc/environment
echo "GIT_COMMIT=${gitCommit}" >> /etc/environment
echo "${projectDetails}" >> /etc/environment
gradle test
"""
def eevv = sh(returnStdout: true, script: "cat /etc/environment").trim()
sh "echo /etc/environment: $eevv"
}
}
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}" ${dockerRegistryRrl}
docker build -t ${dockerRegistryRrl}/prototype/${PROJECT_NAME}:${PROJECT_VERSION} --build-arg PROJECT_NAME=${PROJECT_NAME} --build-arg PROJECT_VERSION=${PROJECT_VERSION} .
docker push ${dockerRegistryRrl}/prototype/${PROJECT_NAME}:${PROJECT_VERSION}
"""
}
}
}
}
}