在jenkins工作空间中动态生成shell脚本
shell语法
cat > h5build.sh <<EOFcurrent=\`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 EOF
pipeline语法
pipeline和shell语法有所不同
pipeline {agent anyenvironment {//这里修改代码的分支名称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 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"}}}} }
流水线构建结果
打包成功