Devops自动化构建流程
构建一个流水线项目
配置完jenkins并且编写pipeline脚本
在GitLab中添加Push events,这个webhook将由存储库的push操作则触发Jenkins自动化构建
本地Push代码,触发Jenkins自动化构建,并将镜像推送只Harbor仓库
查看Pod状态
#!groovy
pipeline {
agent any
environment {
REPOSITORY = "git@192.168.253.133:root/javapro.git"
MODULE="javamodule"
SCRIPT_PATH="/scripts"
}
stages {
stage('获取代码') {
steps {
echo "start fetch code from git:${REPOSITORY}"
deleteDir()
git credentialsId: 'c9b1967c-6fee-438c-a45d-821c5b37918b', url: 'git@192.168.1.7:root/javaproject.git'
}
}
stage('编译+单元测试') {
steps {
echo "start compile"
sh "mvn -D maven.test.skip=true clean package "
}
}
stage('代码检查') {
steps {
echo "代码检查"
}
}
stage('构建镜像') {
steps {
echo "start build image"
sh "${SCRIPT_PATH}/build-images.sh ${MODULE}"
}
}
stage('发布系统') {
steps {
echo "start deploy"
sh "${SCRIPT_PATH}/deploy.sh javademon ${MODULE}"
}
}
}
}
/script/build-images.sh
#!/bin/bash
MODULE=$1
TIME=`date "+%Y%m%d%H%M"`
GIT_VERSION=`git log -1 --pretty=format:"%h"`
IMAGE_NAME=192.168.1.8/javaproject/${MODULE}:${TIME}_${GIT_VERSION}
docker build -t ${IMAGE_NAME} .
echo {IMAGE_NAME}
docker push ${IMAGE_NAME}
echo "${IMAGE_NAME}" > IMAGE_NAME
/script/deploy.sh
#!/bin/bash
IMAGE=`cat IMAGE_NAME`
DEPLOYMENT=$1
MODULE=$2
echo "update image to:${IMAGE}"
kubectl set image deployment/${DEPLOYMENT} ${MODULE}=${IMAGE}