Jenkins部署Vue到Nginx

时间:2021-6-19 作者:qvyue

1.安装NodeJS和Publish Over SSH插件

2.配置NodeJS得到版本:

Jenkins部署Vue到Nginx
image.png

3.在系统设置中配置远程服务器的信息

Jenkins部署Vue到Nginx
image.png

4.添加Webhooks
http://192.168.32.128:8080/generic-webhook-trigger/invoke?token=Iotmars_CMS_Vue

5.pipeline脚本

pipeline {
  agent any
  options{
    buildDiscarder(logRotator(numToKeepStr:'2'))  //持久化工件和控制台输出,规定pipeline运行的最大个数
    disableConcurrentBuilds() //设置pipeline不能并行运行,放置同时访问共享资源。
    skipDefaultCheckout() //跳过默认设置的代码check out
    skipStagesAfterUnstable() //一旦构建状态变成unstable不稳定状态,跳过该阶段
    timeout(time:1,unit:'HOURS')  //设置该pipeline运行的超时时间,超时的pipeline会自动被终止
    timestamps()  //为控制台输出增加时间戳
  }
  environment {
    CREDENTIALSID = 'smartcook'
    GIT_URL = 'http://gitlab.iotmars.com/loushenghua/zhcpadmin.git'
    BRANCH = 'master'
    EMAIL = '792965772@qq.com'
  }
  tools {
    nodejs 'NodeJS_14.9.0'
  }

  triggers {
    GenericTrigger (
      genericVariables: [
        [key: 'ref',value: '$.ref']
      ],
      causeString: 'Triggered on $ref',
      token: 'Iotmars_CMS_Vue',
            
      printContributedVariables: true,
      printPostContent: true,
            
      silentResponse: false,
            
      regexpFilterText: '$ref',
      regexpFilterExpression: "refs/heads/master"
    )
  }
  
  stages {
    stage('Git Checkout'){
      steps {
        retry(3){
        //1.拉取源码
          git (
            branch:"${BRANCH}" ,
            credentialsId:"${CREDENTIALSID}",
            url: "${GIT_URL}" ,
            changelog: true 
          )
        }
      }
    }
    stage('Vue Build') {
      steps { 
        retry(3){
          //2.打包
        //   nodejs() {
            // some block
        //   }
        //   sh 'node -v'
          sh 'npm -v'
          sh 'npm install'
          sh 'npm run build'
          sh 'tar -zcvf dist.tar dist'
        }
      }
    }
    stage('Push Dist'){
      steps{
        retry(3){
          script{
            //3.部署
            sshPublisher(publishers: [sshPublisherDesc(configName: '128', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''source ~/.bash_profile >/dev/null 2>&1;
              cd /root/nginx/html;
              rm -rf iotmars;
              tar -zxvf dist.tar;
              mv dist iotmars;
              rm -rf dist.tar dist;''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'dist.tar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]
            )
            sshPublisher(publishers: [sshPublisherDesc(configName: '248', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''source ~/.bash_profile >/dev/null 2>&1;
              cd /root/nginx/html;
              rm -rf aliyun;
              tar -zxvf dist.tar;
              mv dist aliyun;
              rm -rf dist.tar dist;''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'dist.tar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]
            )
          }
        }
      }
    }
  }
  post {
    always {
      echo 'This will always run'
      script{
        currentBuild.description = "n always"
      }
      deleteDir() /* clean up our workspace */
      //archiveArtifacts artifacts: 'build/libs/**/*.jar', fingerprint: true
      //junit 'build/reports/**/*.xml'
            emailext body: '''



${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志
(本邮件是程序自动下发的,请勿回复!)

构建结果 - ${BUILD_STATUS}


构建信息
  • 项目名称 : ${PROJECT_NAME}
  • 构建编号 : 第${BUILD_NUMBER}次构建
  • 触发原因: ${CAUSE}
  • 构建日志: ${BUILD_URL}console
  • 构建  Url : ${BUILD_URL}
  • 工作目录 : ${PROJECT_URL}ws
  • 项目  Url : ${PROJECT_URL}
Changes Since Last Successful Build:
  • 历史变更记录 : ${PROJECT_URL}changes
${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:
%c
",showPaths=true,changesFormat="
[%a]
%m

",pathFormat="    %p"}

Failed Test Results


$FAILED_TESTS

构建日志 (最后 100行):


''', subject: '${BUILD_STATUS} - ${PROJECT_NAME} - Build # ${BUILD_NUMBER} !', to: "${EMAIL}"
}
success {
println("success!!!!!!!")
script{
currentBuild.description = "n success"
}
//mail to: "${EMAIL}",
// subject: "Success Pipeline: ${currentBuild.fullDisplayName}",
// body: "Success with ${env.BUILD_URL}" /*该构建的url地址*/
}
failure {
echo 'This will run only if failed'
script{
currentBuild.description = "n failure"
}
//mail to: "${EMAIL}",
// subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
// body: "Something is wrong with ${env.BUILD_URL}" /*该构建的url地址*/
}
}
}

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:qvyue@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。