jenkins部署vue程序

2023-07-01 14:53:27 浏览数 (3)

# 配置jenkinsfile编译并发布

代码语言:javascript复制
pipeline {
    agent { label "jenkins-slave1" }
    parameters {
        gitParameter (branch:'', branchFilter: 'origin/(.*)', defaultValue: 'master', description: '选择将要构建的分支', name: 'TAG', quickFilterEnabled: true, selectedValue: 'TOP', sortMode: 'DESCENDING_SMART', tagFilter: '*', type: 'PT_BRANCH_TAG', useRepository: 'ssh://git@xxxx.git')
    }
    options {
        // 表示保留50次构建历史
        buildDiscarder(logRotator(numToKeepStr: '50'))
    }
    stages {
        stage('CleanWorkspace') {
            steps {
                cleanWs()
            }
        }
        stage('拉取脚本') {
            steps {
                dir("/var/jenkins_home/script"){
                    script {
                        try {
                            checkout(
                                [$class: 'GitSCM', doGenerateSubmoduleConfigurations: false, submoduleCfg: [], extensions: [[$class: 'CloneOption', depth: 1, noTags: false, reference: '', shallow: true]],
                                branches: [[name: "master"]],userRemoteConfigs: [[url: "ssh://git@xxxx/rdito/yw_db.git"]]]
                            )
                        }catch(exc) {
                            env.REASON = "拉取脚本出错"
                            throw(exc)
                        }
                    }
                }
            }
        }        
        stage('拉取代码') {
                    steps {
                        dir("${WORKSPACE}/"){
                            script {
                                try {
checkout([$class: 'GitSCM', branches: [[name: '$TAG']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CloneOption', depth: 1, noTags: false, reference: '', shallow: true]], submoduleCfg: [], userRemoteConfigs: [[url: "ssh://git@xxxx.git"]]])
                            env.COMMIT_ID = sh(script: 'git log --pretty=format:%h',  returnStdout: true).trim() // 提交ID
                            env.COMMIT_USER = sh(script: 'git log --pretty=format:%an', returnStdout: true).trim() // 提交者
                            env.COMMIT_TIME = sh(script: 'git log --pretty=format:%ai', returnStdout: true).trim() // 提交时间
                            env.COMMIT_INFO = sh(script: 'git log --pretty=format:%s',  returnStdout: true).trim() // 提交信息                                   
                                }catch(exc) {
                                    env.REASON = "拉取代码出错"
                                    throw(exc)
                                }
                            }
                        }
                    }
                }
        stage('编译') {
            steps{
            nodejs('npm-v14.11.0'){
            sh """
            npm config set registry=http://172.16.30.249:8081/repository/group-npm/ && npm i --unsafe-perm=true --allow-root && npm run build
            cd dist
            rm -rf dist.tar.gz
            tar -zcf dist.tar.gz *
            cp ../package.json
             """
            }
            }


        }
        stage('部署') {
            steps {
            ansiblePlaybook become: true, becomeUser: null, credentialsId: 'deploy_pro', extras: '--extra-vars "WORKSPACE=${WORKSPACE}"', inventory: '/var/jenkins_home/ansible/hosts', playbook: '/var/jenkins_home/ansible/gg/ggclient.yml', sudoUser: null, disableHostKeyChecking: true
            }
        } 
    }
    post {
        always {
            wrap([$class: 'BuildUser']){
                script{
                    currentBuild.description = "提交者: ${COMMIT_USER}" // 添加说明信息
                    currentBuild.description  = "n提交ID: ${COMMIT_ID}" // 添加说明信息
                    currentBuild.description  = "n提交时间: ${COMMIT_TIME}" // 添加说明信息
                }
            }
        }
    }
}

ggclient.yml

代码语言:javascript复制
---
- hosts: gg
  remote_user: admin
  gather_facts: false
  become: yes
  tasks:
    - name: "复制主机上的文件到服务器上"
      copy:
        src: "{{item.src}}"
        dest: "{{item.dest}}"
        owner: admin
        group: admin
        mode: 0755
      with_items:
        - {src: "{{ WORKSPACE }}/dist/dist.tar.gz",dest: "/data/www/filecache"}
    - name: "Execute the go script"
      shell: |
        cd /data/www/filecache && rm -rf /data/www/gg/*
        cp dist.tar.gz /data/www/gg
        cd /data/www/gg && tar -xf dist.tar.gz
        rm -f dist.tar.gz

0 人点赞