1 Star 1 Fork 1

rootegg / cicd

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
pipeline.groovy 5.46 KB
一键复制 编辑 原始数据 按行查看 历史
rootegg 提交于 2024-01-04 10:45 . 修改油猴脚本
// 注意先建system凭据
pipeline {
agent any
environment {
// 毫秒做镜像标签
imageTag = sh returnStdout: true, script: "date +%Y%m%d%H%M%S"
// harbor仓库
harborServer = '10.200.0.48:30020'
harborLibrary = 'library' // 第一次需要在harbor仓库建这个目录
// git项目
gitServer = '10.200.0.48:30300'
gitGroup = 'wms'
gitProjectName = 'wms-353-web'
// 后端接口,区分开测试环境和生产环境
testApiServer = 'http://10.200.0.48:8088'
proApiServer = 'https://xxx.xxx.xxx.xxx:8080'
// 测试环境前端地址,生产环境只打包镜像不发布
webHtmlServer = '10.200.0.48'
webHtmlPort = '50090'
}
parameters {
gitParameter name:'MR_TO_BRANCH',
type:'PT_BRANCH_TAG',
branchFilter:'origin/(.*)',
defaultValue:'master',
selectedValue:'DEFAULT',
sortMode:'DESCENDING_SMART',
description:'选择分支'
choice(
choices: "test\npro", name:'buildType', description:'选择发布环境'
)
}
stages {
stage('拉取代码') {
steps {
script {
if(params.buildType == 'test'){
env.testApiServer = env.proApiServer
}
}
git branch: "${MR_TO_BRANCH}",credentialsId: 'localgitlabroot', url: "http://${gitServer}/${gitGroup}/${gitProjectName}.git"
}
}
stage('打包docker') {
steps {
script{
withCredentials([usernamePassword(credentialsId: 'localharboradmin', passwordVariable: 'harbor_password', usernameVariable: 'harbor_username')]) {
sh '''
docker login -u ${harbor_username} -p ${harbor_password} ${harborServer}
docker build -t ${harborServer}/${harborLibrary}/${gitProjectName}_${buildType}:${imageTag} --build-arg API_SERVER=${testApiServer} .
docker push ${harborServer}/${harborLibrary}/${gitProjectName}_${buildType}:${imageTag}
'''
}
}
}
}
stage('清理镜像') {
steps {
script{
catchError(buildResult:'SUCCESS', stageResult:'SUCCESS'){
// 1、清理当前镜像 2、清理10天前历史缓存中间镜像
sh '''
docker rmi -f ${harborServer}/${harborLibrary}/${gitProjectName}_${buildType}:${imageTag}
docker images -f "dangling=true" '--format={{.Tag}} {{.ID}} {{.CreatedAt}}' | awk '{CS=mktime(sprintf("%s %s %s 00 00 00",substr($3,0,4),substr($3,6,2),substr($3,9,2)));ID=$2;NS=systime();DT=NS-10*86400;if(DT > CS){print ID}}' | xargs -r docker rmi -f
'''
// 不移除空白的镜像,因为那些是docker分层缓存 2023.09.08
// docker rmi -f $(docker images | grep "none" | awk '{print $3}')
// docker自带清理缓存10天前
// docker builder prune --filter 'until=240h' -f
}
}
}
}
stage('部署') {
steps {
script{
// 测试环境要打包+发布;生成环境只打包
if(params.buildType == 'test'){
//sshagent(credentials: ["sshserver"]) {
withCredentials([usernamePassword(credentialsId: 'localharboradmin', passwordVariable: 'harbor_password', usernameVariable: 'harbor_username')]) {
// 到远程服务器上建立文件夹 mkdir -p /home/nginx/${gitProjectName}_${buildType}/log
sh '''
# ssh -o StrictHostKeyChecking=no -l root ${webHtmlServer} uname -a
docker login -u ${harbor_username} -p ${harbor_password} ${harborServer}
docker ps -a --filter "name=${gitProjectName}_${buildType}" -aq | xargs -r docker rm -f
docker images | grep "${gitProjectName}_${buildType}" | awk '{print $3}'| xargs -r docker rmi -f
rm -rf /home/nginx/${gitProjectName}_${buildType} && mkdir -p /home/nginx/${gitProjectName}_${buildType}/log
docker run -d --restart always --name ${gitProjectName}_${buildType} -v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime -v /home/nginx/${gitProjectName}_${buildType}/log:/var/log/nginx -p ${webHtmlPort}:80 ${harborServer}/${harborLibrary}/${gitProjectName}_${buildType}:${imageTag}
'''
}
//}
}
}
}
}
stage('打印结果') {
steps {
script {
echo "本次构建 ${params.buildType} 完成:"
echo "镜像名:${harborServer}/${harborLibrary}/${gitProjectName}_${params.buildType}:${imageTag}"
if(params.buildType == 'test'){
echo "接口地址: ${testApiServer}"
echo "页面地址: http://${webHtmlServer}:${webHtmlPort}"
}
}
}
}
}
}
1
https://gitee.com/rootegg/cicd.git
git@gitee.com:rootegg/cicd.git
rootegg
cicd
cicd
master

搜索帮助