jenkins部署vue程序
# 配置jenkinsfile编译并发布
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}" // 添加说明信息
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
ggclient.yml
---
- 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
上次更新: 2023/06/25, 16:58:02