0
点赞
收藏
分享

微信扫一扫

jenkins使用Jenkinsfile参数化构建中转站

乱世小白 2022-02-17 阅读 84


  由于服务器网路原因,Jenkins服务器无法访问gitlab仓库所以采用多个Jenkins配合使用完成项目部署 81->82 <-105 ,82是中转服务器

81上的Jenkinsfile如下:

import java.text.SimpleDateFormat;

pipeline{
agent any

environment{
//应用自身属性
SERVICE_NAME = 'asset'
SERVICE_VERSION = 'V1.0.8'
ARTIFACTS_NAME = "${SERVICE_NAME}_${SERVICE_VERSION}.tar.gz"

//maven私服
NEXUS2_URL = '20.122.62.81:8989'


}

options{
//保留构建历史记录数量
buildDiscarder(logRotator(numToKeepStr: '5'))
//不允许并行执行流水线,防止同时执行多次
disableConcurrentBuilds()
//整体构建超时
timeout(time: 30, unit: 'MINUTES')
}

tools {
maven 'mvn'
nodejs 'nodejs-12.9.1'
}


stages{

stage('初始化(clean)'){
steps{
sh "echo 环境变量"
sh "printenv"
sh "mvn clean"
sh 'cd ${SERVICE_NAME}-ui && mvn clean'
sh 'cd dist/serviceLogs/${SERVICE_NAME}/ && rm -f ./*.log'
sh 'cd dist/service/${SERVICE_NAME}/ && rm -rf ./lib'
}
}

stage('行为确认') {
steps {
script {
properties(
[ gitLabConnection(''),
[
$class: 'RebuildSettings',
autoRebuild: false,
rebuildDisabled: false
],
parameters(
[
booleanParam(defaultValue: false, description: '上传到产品', name: 'IS_UPLOAD_PRODUCT'),
booleanParam(defaultValue: false, description: '发送到中转服', name: 'IS_TRANSFER')
]
)
]
)
}
}
}

stage('前端构建(webjar)'){
steps{
script{
if("${IS_FRONTEND_PACKAGE}" == "yes") {
sh "cd ${SERVICE_NAME}-ui && npm config set registry http://${NEXUS2_URL}/nexus/content/groups/npm-all/"
sh "cd ${SERVICE_NAME}-ui && npm i --unsafe-perm"
sh 'cd ${SERVICE_NAME}-ui && npm run build'
sh 'cd ${SERVICE_NAME}-ui && mvn deploy'
}
}

}
}


stage('maven打包'){
steps{
sh "mvn clean package -DskipTests"
}
}

stage("发布制品到产品目录") {
when {
environment name: 'IS_UPLOAD_PRODUCT', value: 'true'
}
steps {
script {
sh "echo 发布制品到产品目录 start"
sh "pwd"
sh "cp ./target/${ARTIFACTS_NAME} ~/workspace/apps/${SERVICE_NAME}/"
sh "echo 发布制品到产品目录 end"
}
}
}

stage("制品传到中转服务器") {
when {
environment name: 'IS_TRANSFER', value: 'true'
}
steps {
sh "echo 制品传到中转服务器 start"
sh "pwd"
sshPublisher(
publishers: [
sshPublisherDesc(
configName: '82',
transfers: [
sshTransfer(
cleanRemote: true,
execCommand: 'ls /opt/opt/asset',
execTimeout: 120000,
flatten: false,
makeEmptyDirs: true,
noDefaultExcludes: false,
patternSeparator: '[, ]+',
remoteDirectory: '/opt/opt/asset',
remoteDirectorySDF: false,
removePrefix: 'target',
sourceFiles: 'target/*'
)
],
usePromotionTimestamp: false,
useWorkspaceInPromotion: false,
verbose: false
)
]
)
sh "echo 制品传到中转服务器 end"
}
}






//步骤结束
}






post{
always{
archiveArtifacts artifacts: "target/${ARTIFACTS_NAME}", fingerprint: true
}
}
}


def getHost(name, host, user, passwd) {
def remote = [:]
remote.name = name
remote.host = host
remote.user = user
remote.port = 22
remote.password = passwd
remote.allowAnyHosts = true
return remote
}

def buildTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd.HHmm");
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
return sdf.format(calendar.getTime());
}

需要说明的是:

jenkins使用Jenkinsfile参数化构建中转站_运维

jenkins使用Jenkinsfile参数化构建中转站_linux_02

sshplulisher:配置在 系统管理->配置下的 Publish over SSH中

jenkins使用Jenkinsfile参数化构建中转站_服务器_03

jenkins使用Jenkinsfile参数化构建中转站_jenkins_04

105Jenkins配置,

import java.text.SimpleDateFormat;

pipeline {

agent any

environment {
deploy_name = 'asset'
deploy_ip = '22.221.118.105'
deploy_path = '/data3/asset'
deploy_shell = '/data3/assetShell'
asset_port = '22120'
mysql_port = '3336'
zk_port = '2131'
kafka_port = '9032'
es_port = '9230'
kibana_port = '5631'
}

stages {
stage('deploy') {
steps {
script {
source_server = getHost("192.168.1.82", "22.221.118.105", "transferstation", "transferstation")
deploy_server = getHost("192.168.1.82", "22.221.118.105", "root", "Talent123")
sshCommand remote: deploy_server, command: "rm -rf ${deploy_path}"
sshCommand remote: deploy_server, command: "mkdir -p ${deploy_path}"
sshGet remote: source_server, from: '/opt/opt/opt/asset/*.tar.gz', into: 'asset.tar.gz', override: true
sshPut remote: deploy_server, from: "asset.tar.gz", into: "${deploy_path}", override: true
sshCommand remote: deploy_server, command: "cd ${deploy_path}asset/service/bin;chmod 755 *.sh;export deploy_path=${deploy_path};./stop.sh &"
sshCommand remote: deploy_server, command: "tar -zxvf ${DEPLOY_PATH}/asset.tar.gz -C ${DEPLOY_PATH}"
sshCommand remote: deploy_server, command: "source /etc/profile; cd ${deploy_path}/service/asset/bin; chmod 755 *.sh; export deploy_name=${deploy_name};export deploy_ip=${deploy_ip}; export asset_port=${asset_port};export mysql_port=${mysql_port};export zk_port=${zk_port};export kafka_port=${kafka_port};export es_port=${es_port};export kibana_port=${kibana_port}; source ${deploy_shell}/env.sh; ./service.sh stop; ./service.sh start "
}
}
}
}
}

def getHost(name, host, user, passwd) {
def remote = [:]
remote.name = name
remote.host = host
remote.user = user
remote.port = 22
remote.password = passwd
remote.allowAnyHosts = true
return remote
}


举报

相关推荐

0 条评论