jenkins集成自定义shell脚本

在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
EOF
View 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文件

      

     流水线构建结果

       

          

         

       打包成功

posted @ 2024-12-25 22:38  不懂123  阅读(23)  评论(0)    收藏  举报