forked from JointCloud/JCC-RIP
71 lines
2.8 KiB
Groovy
71 lines
2.8 KiB
Groovy
def JOB_NAME = "${env.JOB_NAME}"
|
|
def BUILD_NUMBER = "${env.BUILD_NUMBER}"
|
|
def label = "jenkins-${JOB_NAME}-${BUILD_NUMBER}-${UUID.randomUUID().toString()}"
|
|
def secret_name = "harbor-auth"
|
|
|
|
podTemplate(label: label, containers: [
|
|
containerTemplate(name: 'nodejs', image: 'node:14.20.0-alpine3.16', command: 'cat', ttyEnabled: true),
|
|
containerTemplate(name: 'docker', image: 'docker:latest', command: 'cat', ttyEnabled: true),
|
|
containerTemplate(name: 'kubectl', image: 'jcce/kubectl:1.23.7', command: 'cat', ttyEnabled: true)
|
|
], serviceAccount: 'jenkins', volumes: [
|
|
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
|
|
]) {
|
|
node(label) {
|
|
def myRepo = checkout scm
|
|
// 获取 git commit id 作为镜像标签
|
|
def imageTag = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
|
|
// 仓库地址
|
|
def registryUrl = "hub.jcce.dev:18443/repository/docker-hub"
|
|
def imageEndpoint = "jcce/kubex-frontend"
|
|
// 镜像
|
|
def image = "${registryUrl}/${imageEndpoint}:${imageTag}"
|
|
def imageLatest = "${registryUrl}/${imageEndpoint}:latest"
|
|
|
|
stage('单元测试') {
|
|
echo "1.测试阶段"
|
|
}
|
|
stage('代码编译打包') {
|
|
try {
|
|
container('nodejs') {
|
|
echo "2.代码编译打包阶段"
|
|
sh('npm install --registry=https://registry.npmmirror.com')
|
|
sh('npm run build:prod')
|
|
}
|
|
} catch (exc) {
|
|
println "构建失败 - ${currentBuild.fullDisplayName}"
|
|
throw(exc)
|
|
}
|
|
}
|
|
stage('构建 Docker 镜像') {
|
|
withCredentials([[$class: 'UsernamePasswordMultiBinding',
|
|
credentialsId: 'docker-auth',
|
|
usernameVariable: 'DOCKER_USER',
|
|
passwordVariable: 'DOCKER_PASSWORD']]) {
|
|
container('docker') {
|
|
echo "3. 构建 Docker 镜像阶段"
|
|
sh('cat /etc/resolv.conf')
|
|
sh("docker login '${registryUrl}' -u '${DOCKER_USER}' -p '${DOCKER_PASSWORD}' ")
|
|
sh("docker build -t '${image}' -t '${imageLatest}' .")
|
|
sh("docker push '${image}'")
|
|
sh("docker push '${imageLatest}'")
|
|
sh("docker rmi '${image}' '${imageLatest}'")
|
|
}
|
|
}
|
|
}
|
|
stage('运行 Kubectl 部署到k8s平台') {
|
|
withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
|
|
container('kubectl') {
|
|
echo "5.部署应用"
|
|
sh('mkdir -p ~/.kube && cp ${KUBECONFIG} ~/.kube/config')
|
|
sh("sed -i 's#IMAGE_NAME#${image}#' deploy/k8s/deployment.yaml")
|
|
sh("sed -i 's#SECRET_NAME#${secret_name}#' deploy/k8s/deployment.yaml")
|
|
sh('kubectl apply -f deploy/k8s/')
|
|
sh('sleep 3')
|
|
echo "6.查看应用"
|
|
sh('kubectl get all -n jcce-system -l app=${JOB_NAME}')
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|