jenkins 打包前端项目
东北小麦客 2023-05-20 DockerJenkins
# 一. 基础
参考资料
# shell基础语法
#! /bin/bash
# ~~字符串替换~~
# 编辑.apimetarc 生成接口文档的地址
#TAG='"sale": ["https:\/\/oapipre.imyunxia.com\/dcosshare\/v2\/api-docs", "https:\/\/oapipre.imyunxia.com\/dcos\/v2\/api-docs"]'
# 替换199服接口生成器的地址
echo "---替换199服接口生成器的地址---"
TAG="\"${npmBuildMode}\": [\"${javaApi1}\", \"${javaApi2}\"],"
# 方案1
#sed -i "s@\"sale\".*@$TAG@" .apimetarc
# 方案2 在"env" 后一行 添加api环境接口信息
sed -i "/\"env\":/a $TAG" .apimetarc
echo "---当前位置---"
cat .apimetarc
#echo "${TAG}"
#! /bin/bash
project="/root/myitem/fileServer/bucket/jenkins/pipline/mock"
echo `
sudo rm -rf ${project_full_path}
sudo rm -rf ${project_full_path}.tar.gz
sudo mv ~/${cRemoteDirectory}/${project_folder_name}.tar.gz ${project_path_prefix}
sudo tar -zxvf ${project_full_path}.tar.gz -C ${project_path_prefix}
`
# 1.1 自由风格的软件项目
# 1.2 多分支流水线任务
pipeline {
agent any
environment {
project_path_prefix = "/rootxx/sdada/xxx/"
project_folder_name = "xxx_yyy"
cSourceFiles = """packages/front_web/${project_folder_name}.tar.gz"""
cRemovePrefix = "packages/front_web"
cRemoteDirectory = "zzz_go"
project_full_path = """${project_path_prefix}/${project_folder_name}"""
cExecCommand = """
sudo rm -rf ${project_full_path}
sudo rm -rf ${project_full_path}.tar.gz
sudo mv ~/${cRemoteDirectory}/${project_folder_name}.tar.gz ${project_path_prefix}
sudo tar -zxvf ${project_full_path}.tar.gz -C ${project_path_prefix}
"""
}
stages {
stage('GitBranch Pull Code') {
steps {
echo """${cExecCommand}"""
echo """要打包的git分支: ${git_branch}"""
git branch: """${git_branch}""", credentialsId: '98774449-git仓库链接生成的通行证', url: 'http://git仓库的url链接'
}
}
stage('ApiScriptContent') {
steps {
echo "接口生成器脚本内容:"
sh '''
echo ${api_script_content} > .apimetarc
'''
}
}
stage('Npm Install packages') {
steps {
nodejs('node_18.14.0') {
echo 'node_18.14.0 安装依赖'
sh 'npm i'
}
}
}
stage('Npm Build') {
steps {
nodejs('node_18.14.0') {
echo """开始打包: --- ${git_branch} ---分支"""
sh """
if [ "${git_branch}" = "online" ];
then
npm run build
else
npm run build:${git_branch}
fi
"""
}
}
}
stage('Tar 打包产物') {
steps {
echo "---打包产物---"
sh """
cd packages/ops_web
rm -rf ${project_folder_name}.tar.gz ${project_folder_name}
mv dist/ ${project_folder_name}
tar -zcvf ${project_folder_name}.tar.gz ${project_folder_name}
"""
}
}
stage('部署至 34测试服'){
when {
expression { git_branch == 'release' }
}
steps{
sshPublisher(
publishers: [
sshPublisherDesc(
configName: '34 pre',
transfers: [sshTransfer(
sourceFiles: """${cSourceFiles}""", removePrefix: cRemovePrefix, remoteDirectory: cRemoteDirectory,
execCommand: cExecCommand,
cleanRemote: false, excludes: '',
execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectorySDF: false
)],
usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)
]
)
echo "34测试服 部署完成~"
}
}
stage('部署至 43预发布服'){
when {
expression { git_branch == 'preview' }
}
steps{
sshPublisher(
publishers: [
sshPublisherDesc(
configName: '43 预发布',
transfers: [sshTransfer(
sourceFiles: """${cSourceFiles}""", removePrefix: cRemovePrefix, remoteDirectory: cRemoteDirectory,
execCommand: cExecCommand,
cleanRemote: false, excludes: '',
execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectorySDF: false
)],
usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)
]
)
echo "43预发布服 部署完成~"
}
}
stage('部署至 199销售服'){
when {
expression { git_branch == 'sale' }
}
steps{
sshPublisher(
publishers: [
sshPublisherDesc(
configName: '199',
transfers: [sshTransfer(
sourceFiles: """${cSourceFiles}""", removePrefix: cRemovePrefix, remoteDirectory: cRemoteDirectory,
execCommand: cExecCommand,
cleanRemote: false, excludes: '',
execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectorySDF: false
)],
usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)
]
)
echo "199销售服 部署完成~"
}
}
}
}