问题
需要使用Jenkins中使用Maven构建Spring服务和Docker镜像后,并推送相关Docker镜像到AWS ECR,并发布AWS EKS相关服务。
Jenkinsfile
yaml
pipeline {
agent any
tools {
# Jenkins已经安装好了bellsoft-jdk17
jdk 'bellsoft-jdk17.0.10'
# Jenkins已经安装好了maven 3.6.3
maven '3.6.3'
}
stages {
# 检查 java 和 maven版本
stage('Check tool version') {
steps {
sh '''
env | grep -e PATH -e JAVA_HOME
which java
java -version
which mvn
mvn --version
'''
}
}
# 登录到 AWS ECR
stage('Prepare') {
steps {
sh '''
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 12342354214352.dkr.ecr.us-east-1.amazonaws.com
'''
}
}
# 使用maven构建和docker构建
stage('Build') {
steps {
sh '''
mvn clean && mvn package -Dmaven.test.skip=true
docker build -t websocket-server .
'''
}
}
# 打Docker标签
stage('Tag') {
steps {
script {
if (env.BRANCH_NAME.startsWith('release')){
sh '''
docker tag websocket-server:latest 12342354214352.dkr.ecr.us-east-1.amazonaws.com/websocket-server:$BRANCH_NAME
'''
} else if (env.BRANCH_NAME.startsWith('uat')){
sh '''
docker tag websocket-server:latest 12342354214352.dkr.ecr.us-east-1.amazonaws.com/websocket-server:uat
'''
} else {
echo 'Skipping Tag.'
}
}
}
}
# 推送Docker镜像到ECR
stage('Push to ECR') {
steps {
script {
if (env.BRANCH_NAME.startsWith('release')){
sh '''
docker push 12342354214352.dkr.ecr.us-east-1.amazonaws.com/websocket-server:$BRANCH_NAME
'''
} else if (env.BRANCH_NAME.startsWith('uat')){
sh '''
docker push 12342354214352.dkr.ecr.us-east-1.amazonaws.com/websocket-server:uat
'''
} else {
echo 'Skipping Deploy to K8S.'
}
}
}
}
# 重启UAT环境EKS集群中的指定服务
stage('Deploy to EKS') {
steps {
script {
if (env.BRANCH_NAME.startsWith('uat')){
sh '''
aws eks update-kubeconfig --region us-east-1 --name uat
kubectl config current-context
kubectl rollout restart deployment websocket-server -n stack-din
'''
} else {
echo 'Skipping Deploy to EKS.'
}
}
}
}
}
}
总结
使用Jenkinsfile进行构建管理挺简单的。