在jenkins工作空间中动态生成shell脚本
shell语法
cat > h5build.sh <<EOF current=\`date "+%Y-%m-%d %H:%M:%S"\` timeStamp=\`date -d "\$current" +%s\` currentTimeStamp=\$((timeStamp*1000+\`date "+%N"\`/1000000)) echo \`cat version.json | grep -o _v.*_ | sed 's/_//g'\` \$currentTimeStamp > version2.json EOFView Code
pipeline语法
pipeline和shell语法有所不同
pipeline { agent any environment { //这里修改代码的分支名称 GIT_BRANCH = 'test' } stages { stage('GetCode'){ steps { git branch: "${env.GIT_BRANCH}", url: 'https://192.168.30.111:8090/soc/yun_soc_h5.git' } post { success { print("getCode success") } } } stage('build') { steps { script { echo "当前构建的Git分支名称是: ${env.GIT_BRANCH}" sh "npm config set unsafe-perm=true" sh "npm install" sh "npm install node-sass --unsafe-perm --save --registry=https://registry.npm.taobao.org" sh "npm install image-webpack-loader" sh "cnpm install image-webpack-loader" sh "npm run build:prod" sh "echo ${env.GIT_BRANCH} > ./version.json" } } } stage('Deploy') { steps { script { sh ''' cat > h5deploy.sh <<EOF current=\\`date "+%Y-%m-%d %H:%M:%S"\\` timeStamp=\\`date -d "\\$current" +%s\\` currentTimeStamp=\\$((timeStamp*1000+\\`date "+%N"\\`/1000000)) echo \\`cat version.json | grep -o _v.*_ | sed 's/_//g'\\` \\$currentTimeStamp > dist/version.json EOF ''' sh "chmod +x h5deploy.sh && sh h5deploy.sh" sh "ssh admin@192.168.30.92 'rm -fr /data/soc/nginx/html/*'" sh "scp -r ./dist/* admin@192.168.30.92:/data/soc/nginx/html" } } } } }pipeline生成shell文件
流水线构建结果
打包成功
标签:npm,pipeline,GIT,自定义,sh,shell,BRANCH,jenkins From: https://www.cnblogs.com/yxh168/p/18631516