一 背景说明:
想通过pipeline的方法实现构建部署k8s项目,前提是代码仓库里面有一个Dockfile的文件,和deploy.yml部署文件
1.1 dockfile内容
#FROM java:8
FROM openjdk:8-jdk-alpine
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
EXPOSE 10086
ENTRYPOINT ["java","-jar","/app.jar"]
二 获取pipeline里面的语法
2.1 git拉取代码-语法
第一种方法:
第二种方法
2.2 maven编译-语法
2.3 拷贝yaml部署文件到k8s-master-语法
剩下的可以不填写,默认拷贝到了你设置publish over ssh的时候,指定的目录,我这里是/tmp
2.4 上传镜像到docker仓库-语法
三 设置jenkins自动构建
如果没有下面这个选项,就需要安装gitlab插件,
如果在gitlab里添加jekins的地址报错,去掉这个选项
四 pipeline脚本内容
4.1 脚本含有密码
pipeline{
agent any
environment {
harboruser="8255368@qq.com"
harborpasswd="111xxxaaa"
harboraddress="registry.cn-hangzhou.aliyuncs.com"
registry="registry.cn-hangzhou.aliyuncs.com/huningfei/welcome"
image_name="$registry:$BUILD_NUMBER"
}
stages {
stage('拉取代码'){
steps {
git credentialsId: '2751f03c-ba70-494a-a13c-cac7f9c3ffcc', url: 'https://gitee.com/huningfei/demo-test.git'
}
}
// 第二步
stage('代码编译'){
steps {
sh '/var/jenkins_home/mvn/bin/mvn clean package -Dmaven.test.skip=true'
}
}
stage('通过dockerfile构建镜像'){
steps {
sh '''cp -r $WORKSPACE/target/*.jar .
docker build -t mytest .'''
}
}
stage('上传dockerhub仓库'){
steps {
sh '''docker login -u ${harboruser} -p ${harborpasswd} ${harboraddress}
docker tag mytest:latest ${image_name}
docker push ${image_name}'''
}
}
stage('通过publish over ssh 拷贝deploy.yml到k8s-master'){
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '172.31.7.112-k8s-master', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'deploy.yml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
stage('更改 yam里的image地址 '){
steps {
sh 'ssh root@172.31.7.112 sed -i "s#address#${image_name}#g" /tmp/deploy.yml'
}
}
stage('部署到 k8s'){
steps {
sh 'ssh root@172.31.7.112 kubectl apply -f /tmp/deploy.yml'
}
}
}
}
4.2 脚本不含密码
pipeline{
agent any
environment {
registry="registry.cn-hangzhou.aliyuncs.com/huningfei/welcome"
image_name="$registry:$BUILD_NUMBER"
}
stages {
stage('拉取代码'){
steps {
git credentialsId: '2751f03c-ba70-494a-a13c-cac7f9c3ffcc', url: 'https://gitee.com/huningfei/demo-test.git'
}
}
// 第二步
stage('代码编译'){
steps {
sh '/var/jenkins_home/mvn/bin/mvn clean package -Dmaven.test.skip=true'
}
}
stage('通过dockerfile构建镜像'){
steps {
sh '''cp -r $WORKSPACE/target/*.jar .
docker build -t mytest .'''
}
}
stage('上传dockerhub仓库'){
steps {
withCredentials([usernamePassword(credentialsId: 'a98622af-1b3d-42bd-8f1b-d732ad3a3e6a', passwordVariable: 'password', usernameVariable: 'username')]) {
sh '''
docker tag mytest:latest ${image_name}
docker push ${image_name}'''
}
}
}
stage('通过publish over ssh 拷贝deploy.yml到k8s-master'){
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '172.31.7.112-k8s-master', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'deploy.yml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
stage('更改 yam里的image地址 '){
steps {
sh 'ssh root@172.31.7.112 sed -i "s#address#${image_name}#g" /tmp/deploy.yml'
}
}
stage('部署到 k8s'){
steps {
sh 'ssh root@172.31.7.112 kubectl apply -f /tmp/deploy.yml'
}
}
}
}
4.3 pipeline SCM方式
前提条件,代码仓库里必须含有Jenkinsfile,这种方式就是不需要把deploy.yml里的内容,放到jenkins页面上了,直接放到代码仓库即可。
五 构建并测试