一、介绍
jenkins流水线介绍:在CI/CD过程中可以通过流水线pipeline{}区分不通的步骤,步骤可以通过stages去定义。这样的好处是每一个过程都能清楚的知道,参考官方文档: https://jenkins.io/zh/doc/book/pipeline/syntax/
凭据介绍:jenkins凭据可以存储git仓库及docker仓库等认证信息,在处理持续集成这块可以将凭据子串写入到脚本中进行认证
SSHagent:操作远端服务器执行命令
并行:通过定义parallel可以实现并行任务,加入有一个项目,同时20个开发git仓库需要打包,如果安装面向过程执行完耗时肯定是好长,当然”并行”也是面向过程了。只是它可以在某个步骤下同时执行多个任务
二、需求
- 快速出包(并行编译15个代码仓库)
三、jenkinsfile打包核心解说
3.1拉取推送代码
def devgit(){
script{
checkout([class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[class: 'RelativeTargetDirectory',
relativeTargetDir: project ]],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: git_key,
url: git_url ]]])
sshagent(credentials: [git_key], ignoreMissing: true) {
dir("{env.WORKSPACE}/{env.project}") {
sh '''
function Add_tag() {
tag_num=(git tag -l{release_tag}|wc -l)
git config user.name 'ops'
git config user.email 'ops@g6p.cn'
if [ {tag_num} -eq 1 ]; then
echo "release tag{release_tag} 存在,正在删除..."
git tag -d {release_tag}
git push origin --delete{release_tag}
fi
git tag -a {release_tag} -m "jenkins 自动打包tag{release_tag}"
git push --tags
}
Add_tag
'''
}
}
}
}
3.2并行
parallel {
stage('编译gmf_irs: php5') {
environment {
project='gmf_irs'
}
steps {
php5_encryption()
}
}
stage('编译gmf_utility: php5') {
environment {
project='gmf_utility'
}
steps {
php5_encryption()
}
}
stage('编译gmf_rms: php5') {
environment {
project='gmf_rms'
}
steps {
php5_encryption()
}
}
stage('编译xc-uds: php5') {
environment {
project='xc-uds'
}
steps {
php5_encryption()
}
}
}
四、源代码
//需要安装插件ssh agent,Blue Ocean
//拉取开发Git库源码:分四个阶段,主要因为减轻git服务器压力.(如果放在一个阶段并发会导致git服务器负载过高.)
def timeform() {
script {
return sh(script : 'date "+%Y-%m-%d---%H:%M:%S"', returnStdout: true).trim()
}
}
def time() {
script {
return sh(script : 'date +%s', returnStdout: true).trim()
}
}
def message(){
script {
sh '''
displaysecs() {
local T=1
local D=((T/60/60/24))
local H=((T/60/60%24))
local M=((T/60%60))
local S=((T%60))
((D > 0 )) && printf '%d,days' D
((H > 0 )) && printf '%d,hours' H
((M > 0 )) && printf '%d,minutes' M
((D > 0 || H>0 ||M > 0 )) && printf 'and,'
printf '%d,seconds\n' S
}
runtime=(displaysecs (({eDate}-{sDate})))
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=3d34f677-802c-479d-834b-a1460e1efa7c' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "text",
"text": {
"content": "小鲤鱼平台系统 tag:'release_tag' 打包完毕
打包任务开始时间:'{startDate}'
打包任务结束时间:'{endDate}'
打包任务总耗时间:'{runtime}' ",
"mentioned_list":["XiangYouSong"],
}
}'
'''
}
}
def devgit(){
script{
checkout([class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[class: 'RelativeTargetDirectory',
relativeTargetDir: project ]],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: git_key,
url: git_url ]]])
sshagent(credentials: [git_key], ignoreMissing: true) {
dir("{env.WORKSPACE}/{env.project}") {
sh '''
function Add_tag() {
tag_num=(git tag -l {release_tag}|wc -l)
git config user.name 'ops'
git config user.email 'ops@g6p.cn'
if [{tag_num} -eq 1 ]; then
echo "release tag {release_tag} 存在,正在删除..."
git tag -d{release_tag}
git push origin --delete {release_tag}
fi
git tag -a{release_tag} -m "jenkins 自动打包tag {release_tag}"
git push --tags
}
Add_tag
'''
}
}
}
}
def fsync(){
script {
sh '''
/bin/rsync -vzrtopgl --delete --exclude .git --exclude "LICENSE"{WORKSPACE}/{project}{WORKSPACE}/xc_rel_compile_pack/
'''
}
}
def php5_encryption(){
script {
dir("{env.WORKSPACE}/xc_rel_compile_pack/{env.project}") {
sh '''
/usr/local/ZendGuard/bin/zendenc55 --short-tags on --delete-source --recursive --ignore-errors --ignore {WORKSPACE}/xc_rel_compile_pack/{project}/resources --ignore {WORKSPACE}/xc_rel_compile_pack/{project}/storage --ignore {WORKSPACE}/xc_rel_compile_pack/{project}/vendor --quiet --symlinks {WORKSPACE}/xc_rel_compile_pack/{project}
'''
}
}
}
def php7_encryption(){
script {
dir("{env.WORKSPACE}/xc_rel_compile_pack/{env.project}") {
sh '''
find {WORKSPACE}/xc_rel_compile_pack/{project} ! -path "{WORKSPACE}/xc_rel_compile_pack/{project}/scripts" ! -path "{WORKSPACE}/xc_rel_compile_pack/{project}/scripts/*" ! -path "{WORKSPACE}/xc_rel_compile_pack/{project}/vendor" ! -path "{WORKSPACE}/xc_rel_compile_pack/{project}/vendor/*" ! -path "{WORKSPACE}/xc_rel_compile_pack/{project}/storage" ! -path "{WORKSPACE}/xc_rel_compile_pack/{project}/storage/*" ! -path "{WORKSPACE}/xc_rel_compile_pack/{project}/resources" ! -path "{WORKSPACE}/xc_rel_compile_pack/{project}/resources/*" -name "*.php" -type f -exec ls -1 {} \\;|while read file;
do
/usr/local/php7/bin/php /data/script/encode.php {file}{file};
done
cd {WORKSPACE}/xc_rel_compile_pack/{project}
/usr/local/php7/bin/php /bin/composer install
if [[ ? != '0' ]];then
echo "composer install fail"
exit 1;
else
echo "composer install succeed"
fi
'''
}
}
}
def tunnel_build(){
script {
dir("{env.WORKSPACE}/xc_rel_compile_pack/{env.project}") {
sh '''
pwd
rm -f .gitignore
cnpm install
if [[? != '0' ]];then
echo "cnpm install fail"
exit 1;
else
echo "cnpm install succeed"
fi
mv etc etc_backup
'''
}
}
}
def front_build(){
script {
dir("{env.WORKSPACE}/xc_rel_compile_pack/{env.project}") {
sh '''
rm -f .gitignore
echo Begin:cnpm install
cnpm install
if [[ ? != '0' ]];then
echo "cnpm install fail"
exit 1;
else
echo "cnpm install succeed"
fi
echo End:cnpm install
echo Begin: npm run build
npm run build
if [[? != '0' ]];then
echo "npm build fail"
exit 2;
else
echo "npm build succeed"
fi
echo End:npm run build
'''
}
}
}
def new_front_build(){
script {
dir("{env.WORKSPACE}/xc_rel_compile_pack/{env.project}") {
sh '''
rm -f .gitignore
echo Begin:yarn install
yarn
if [[ ? != '0' ]];then
echo "yarn install fail"
exit 1;
else
echo "yarn install succeed"
fi
echo End:yarn install
echo Begin: npm run build
npm run build
if [[? != '0' ]];then
echo "npm build fail"
exit 2;
else
echo "npm build succeed"
fi
echo End:npm run build
'''
}
}
}
pipeline{
agent {
node {
label 'xunce-oms-release'
}
}
environment {
sDate=time()
startDate=timeform()
}
stages{
stage('删除工作空间') {
steps {
script{
deleteDir()
}
}
}
stage('拉取开发Git库源码:第一阶段') {
failFast true
parallel {
stage('拉取tunnel仓库,并创建tag') {
environment {
git_url='ssh://git@172.20.1.16:11022/backadmin/xc-live-tunnel.git'
project='xc-live-tunnel'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
devgit()
}
}
stage('拉取bms仓库,并创建tag') {
environment {
git_url='ssh://git@172.20.1.16:11022/backadmin/gmf_bms.git'
project='gmf_bms'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
devgit()
}
}
stage('拉取oms仓库,并创建tag') {
environment {
git_url='ssh://git@172.20.1.16:11022/backadmin/gmf_oms_v2.git'
project='gmf_oms'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
devgit()
}
}
stage('拉取front仓库,并创建tag') {
environment {
git_url='ssh://git@172.20.1.16:11022/backadmin/xcfrontend.git'
project='gmf_front'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
devgit()
}
}
}
}
stage('拉取开发Git库源码:第二阶段') {
failFast true
parallel {
stage('拉取irs仓库,并创建tag') {
environment {
git_url='ssh://git@172.20.1.16:11022/backadmin/xc-irs.git'
project='gmf_irs'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
devgit()
}
}
stage('拉取irs_front仓库,并创建tag') {
environment {
git_url='ssh://git@172.20.1.16:11022/backadmin/xc-irs-front.git'
project='gmf_irs_front'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
devgit()
}
}
stage('拉取utility仓库,并创建tag') {
environment {
git_url='ssh://git@172.20.1.16:11022/backadmin/gmf-utility.git'
project='gmf_utility'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
devgit()
}
}
stage('拉取uds仓库,并创建tag') {
environment {
git_url='ssh://git@172.20.1.16:11022/backadmin/xc-uds.git'
project='xc-uds'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
devgit()
}
}
}
}
stage('拉取开发Git库源码:第三阶段') {
failFast true
parallel {
stage('拉取ipb仓库,并创建tag') {
environment {
git_url='ssh://git@172.20.1.16:11022/backadmin/gmf_ipb.git'
project='gmf_ipb'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
devgit()
}
}
stage('拉取pms仓库,并创建tag') {
environment {
git_url='ssh://git@172.20.1.16:11022/backadmin/xc-pms.git'
project='xc-pms'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
devgit()
}
}
stage('拉取rms仓库,并创建tag') {
environment {
git_url='ssh://git@172.20.1.16:11022/backend/gmf_rms.git'
project='gmf_rms'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
devgit()
}
}
}
}
stage('拉取开发Git库源码:第四阶段') {
failFast true
parallel {
stage('拉取estimate仓库,并创建tag') {
environment {
git_url='ssh://git@172.20.1.16:11022/backadmin/xc-estimate.git'
project='xc-estimate'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
devgit()
}
}
stage('拉取estimate-front仓库,并创建tag') {
environment {
git_url='ssh://git@172.20.1.16:11022/backadmin/xc-estimate-front.git'
project='xc-estimate-front'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
devgit()
}
}
stage('拉取new_front仓库,并创建tag') {
environment {
git_url='ssh://git@172.20.1.16:11022/backadmin/xc-new-front.git'
project='xc-new-front'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
dev_tag='{new_front_tag}'
}
steps {
devgit()
}
}
}
}
stage('拉取xc_rel_compile_pack运维git仓库') {
environment {
git_url='ssh://git@192.168.0.38/code/xc_rel_compile_pack.git'
project='xc_rel_compile_pack'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
checkout([class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[class: 'RelativeTargetDirectory',
relativeTargetDir: project ],
[class: 'CloneOption',
noTags: true, reference: '',
shallow: true]],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: git_key,
url: git_url ]]])
}
}
stage('同步源码到编译目录:第一阶段') {
failFast true
parallel {
stage('同步gmf_bms目录') {
environment {
project='gmf_bms'
}
steps {
fsync()
}
}
stage('同步gmf_oms目录') {
environment {
project='gmf_oms'
}
steps {
fsync()
}
}
stage('同步gmf_rms目录') {
environment {
project='gmf_rms'
}
steps {
fsync()
}
}
stage('同步gmf_irs目录') {
environment {
project='gmf_irs'
}
steps {
fsync()
}
}
}
}
stage('同步源码到编译目录:第二阶段') {
failFast true
parallel {
stage('同步gmf_irs_front目录') {
environment {
project='gmf_irs_front'
}
steps {
fsync()
}
}
stage('同步gmf_utility目录') {
environment {
project='gmf_utility'
}
steps {
fsync()
}
}
stage('同步xc-uds目录') {
environment {
project='xc-uds'
}
steps {
fsync()
}
}
stage('同步xc-live-tunnel目录') {
environment {
project='xc-live-tunnel'
}
steps {
fsync()
}
}
}
}
stage('同步源码到编译目录:第三阶段') {
failFast true
parallel {
stage('同步gmf_ipb目录') {
environment {
project='gmf_ipb'
}
steps {
fsync()
}
}
stage('同步xc-pms目录') {
environment {
project='xc-pms'
}
steps {
fsync()
}
}
stage('同步gmf_front目录') {
environment {
project='gmf_front'
}
steps {
fsync()
}
}
stage('同步xc-estimate目录') {
environment {
project='xc-estimate'
}
steps {
fsync()
}
}
}
}
stage('同步源码到编译目录:第四阶段') {
failFast true
parallel {
stage('同步xc-estimate-front目录') {
environment {
project='xc-estimate-front'
}
steps {
fsync()
}
}
stage('同步xc-new-front目录') {
environment {
project='xc-new-front'
}
steps {
fsync()
}
}
}
}
stage('编译平台系统:第一阶段') {
failFast true
parallel {
stage('编译xc-live-tunnel: nodejs') {
environment {
project='xc-live-tunnel'
}
steps {
tunnel_build()
}
}
stage('编译gmf_bms: php5') {
environment {
project='gmf_bms'
}
steps {
php5_encryption()
}
}
stage('编译gmf_oms: php5') {
environment {
project='gmf_oms'
}
steps {
php5_encryption()
}
}
stage('编译gmf_ipb: php5') {
environment {
project='gmf_ipb'
}
steps {
php5_encryption()
}
}
}
}
stage('编译平台系统:第二阶段') {
failFast true
parallel {
stage('编译gmf_irs: php5') {
environment {
project='gmf_irs'
}
steps {
php5_encryption()
}
}
stage('编译gmf_utility: php5') {
environment {
project='gmf_utility'
}
steps {
php5_encryption()
}
}
stage('编译gmf_rms: php5') {
environment {
project='gmf_rms'
}
steps {
php5_encryption()
}
}
stage('编译xc-uds: php5') {
environment {
project='xc-uds'
}
steps {
php5_encryption()
}
}
}
}
stage('编译平台系统:第三阶段') {
failFast true
parallel {
stage('编译xc-estimate: php7') {
environment {
project='xc-estimate'
}
steps {
php7_encryption()
}
}
stage('编译gmf_front: nodejs') {
environment {
project='gmf_front'
}
steps {
front_build()
}
}
stage('编译xc-pms: php7') {
environment {
project='xc-pms'
}
steps {
php7_encryption()
}
}
stage('编译xc-estimate-front: nodejs') {
environment {
project='xc-estimate-front'
}
steps {
front_build()
}
}
stage('编译xc-new-front: nodejs') {
environment {
project='xc-new-front'
}
steps {
new_front_build()
}
}
}
}
stage('整理打包后的文件') {
steps {
dir("{env.WORKSPACE}/xc_rel_compile_pack") {
sh '''
rm -fr *@tmp
rm -rf gmf_oms/public/*
rm -rf gmf_oms/resources/views/*
rm -rf gmf_oms/resources/views_h5/*
cp -rf gmf_front/public/* gmf_oms/public/
cp -rf gmf_front/resources/views/* gmf_oms/resources/views/
cp -rf gmf_front/resources/views_h5/* gmf_oms/resources/views_h5/
find ./ -maxdepth 2 -name ".gitignore" -type f|xargs -I {} rm -rf {}
echo gmf_front/ >.gitignore
'''
}
}
}
stage('上传小鲤鱼平台系统到运维git仓库') {
environment {
git_url='ssh://git@192.168.0.38/code/xc_rel_compile_pack.git'
project='xc_rel_compile_pack'
git_key='bbe66c93-76e5-4945-a886-76937956503f'
}
steps {
sshagent(credentials: [git_key], ignoreMissing: true) {
dir("{env.WORKSPACE}/{env.project}") {
sh '''
function Add_tag() {
tag_num=(git tag -l {release_tag}|wc -l)
git config user.name 'ops'
git config user.email 'ops@g6p.cn'
if [{tag_num} -eq 1 ]; then
echo "release tag {release_tag} 存在,正在删除..."
git tag -d{release_tag}
git push origin --delete {release_tag}
fi
echo{release_tag} >version
git add .
git commit -m {release_tag}
git tag -a{release_tag} -m "jenkins 自动打包tag ${release_tag}"
git push --tags -f
}
Add_tag
'''
}
}
}
}
stage('小鲤鱼平台系统打包状态通知') {
environment {
eDate=time()
endDate=timeform()
}
steps {
message()
}
}
stage('打包平台系统数据库,并推送到web服务器') {
when {
expression {
return env.xunce_oms_db == 'true';
}
}
steps {
build job: 'xunce-oms-release-db', parameters: [
string(name: 'release_tag', value: env.release_tag)
]
}
}
stage('更新平台系统beta环境') {
when {
expression {
return env.xunce_oms_beta == 'true';
}
}
steps {
build job: 'xunce-oms-beta-update', parameters: [
string(name: 'release_tag', value: env.release_tag)
]
}
}
}
}
留言