Jenkins-file Example

Jenkins file example

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
properties([buildDiscarder(logRotator(artifactNumToKeepStr: '10', numToKeepStr: '10'))])
node('build') {
def serviceName = ""
def warFilePath = [
'external-api-web/target/external-api-web-1.0-SNAPSHOT.jar'
]
def artifactsPath = "devops-artifacts"
def artifactOssPath = "bi/${ env.JOB_NAME }"
sh "echo ${ env.JOB_NAME }"
def tempFolder = "/tmp/${ env.JOB_NAME }"
def git_commit_id = ""
def artifact = ""

stage('Preparation') {
deleteDir()
sh "rm -rf ${ tempFolder }"
}

try {
stage('Clone from SCM') {
// checkout scm
checkout poll: false,
scm: [$class: 'GitSCM',
branches: [[name: 'master']],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '7bc6cda9-a250-4d99-962c-e2ede002db06',
url: 'git@git.wecash.net:bi/external-api.git']]]
git_commit_id = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim() //需要获取shell返回值操作
git_committer = sh(returnStdout: true, script: "git log -1 --format='%ae'").trim()
sh "echo ${ git_committer }"
}

stage('Build') {
withMaven(mavenSettingsConfig: '02e95d2b-6bb6-457d-943f-13c63095e500',
globalMavenSettingsConfig: 'bfa90212-2742-4611-aa71-8e0ec76d0c28',
jdk: 'Oracle JDK 8', maven: 'Maven 3') {
switch(env.BRANCH_NAME) {
case "master":
sh "echo this is master branch."
sh "mvn clean install -Pprod -Dmaven.test.skip=true"
break
case "dev":
sh "echo this is dev branch."
sh "mvn clean install -Pdev -Dmaven.test.skip=true"
break
}
}
}

stage('Run Tests') {
sh "echo 'No tests to do.'"
}

stage('Update to Aliyun OSS') {
sh "mkdir -p ${ tempFolder }"
sh "echo ${ env.JOB_NAME }"
sh " echo ${ artifactOssPath }"
for ( servicePath in warFilePath ) {
serviceName = sh(returnStdout: true, script: "basename ${ servicePath } | cut -d . -f 1").trim()
serviceFullName = sh(returnStdout: true, script: "basename ${ servicePath }").trim()
sh "mv ${ servicePath } ${ tempFolder }/${ serviceFullName }"
artifact = "${ serviceName }_${ env.BUILD_NUMBER }-${ env.BRANCH_NAME }-${ git_commit_id }.tar.gz"
sh "mkdir -p ${ artifactsPath } && tar czf ${ artifactsPath }/${ artifact } -C ${ tempFolder } ${ serviceFullName }"
sh "upload-flash -f ${ artifactsPath }/${ artifact } -t ${ artifactOssPath }"
}
}

stage('Archive artifacts') {
archiveArtifacts allowEmptyArchive: true, artifacts: "${ artifactsPath }/*", fingerprint: true, onlyIfSuccessful: true
currentBuild.result = 'SUCCESS'
}
} catch (e) {
currentBuild.result = "FAILED"
throw e
} finally {
// notifyBuild(currentBuild.result,git_committer)
}
}

def notifyBuild(String buildStatus = 'STARTED',String GitCommitter) {
// build status of null means successful
buildStatus = buildStatus ?: 'SUCCESSFUL'

def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
def summary = "${subject} (${env.BUILD_URL})"
def details = """STARTED: Job '${env.JOB_NAME} [BUILD_NUMBER:${env.BUILD_NUMBER}]'
Check console output at "${env.BUILD_URL}">${env.JOB_NAME} [${env.BUILD_NUMBER}]"""

// Send notifications
mail (
to: GitCommitter,
subject: subject,
body: details,
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)
}

本文标题:Jenkins-file Example

文章作者:shuke

发布时间:2020年04月20日 - 19:04

最后更新:2020年04月20日 - 19:04

原始链接:https://shuke163.github.io/2020/04/20/Jenkins-file-Example/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------

本文标题:Jenkins-file Example

文章作者:shuke

发布时间:2020年04月20日 - 19:04

最后更新:2020年04月20日 - 19:04

原始链接:https://shuke163.github.io/2020/04/20/Jenkins-file-Example/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%