forked from jenkins-infra/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.groovy
82 lines (72 loc) · 2.49 KB
/
Jenkinsfile.groovy
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
pipeline {
agent any
options {
disableConcurrentBuilds()
quietPeriod 30
}
stages{
stage("文章格式校验"){
when {
changeRequest target: 'master'
}
steps{
script{
withChangeSets(){
if(env.changePath != "" && env.changePath != null && env.changePath.endsWith(".md")) {
checkArticleMeta(env.changePath)
}
}
}
}
}
stage("preview"){
when {
changeRequest target: 'master'
}
steps{
script{
def branch = "wechat-$BRANCH_NAME"
branch = branch.toLowerCase()
def prNum = "$BRANCH_NAME".replace('PR-','')
build job: 'jenkins-zh/jenkins-zh/master', parameters: [string(name: 'previewUpstream', value: branch),string(name: 'previewUpstreamPR', value: prNum)]
pullRequest.createStatus(status: 'success',
context: 'continuous-integration/jenkins/pr-merge/preview',
description: 'Website preview',
targetUrl: "http://" + branch + ".preview.jenkins-zh.cn")
}
}
}
}
}
def checkArticleMeta(filePath){
def articleText = readFile encoding: 'UTF-8', file: filePath
def articleYamlText = getYamlMeta(articleText)
if(isEmpty(articleYamlText)){
error "cannot find the yaml meta from current article"
}
def articles = readYaml file: articleYamlText
def article = articles[0]
if(isEmpty(article.title) || isEmpty(article.description)
|| isEmpty(article.author) || isEmpty(article.poster)){
error "title, description, author or poster can not be empty"
}
if(!isEmpty(article.translator) && isEmpty(article.original)) {
error "current article is translated from the origin one, please provide the original link"
}
}
def isEmpty(str){
return str == "" || str == null
}
def getYamlMeta(txt){
def firstIndex = txt.indexOf('---')
if(firstIndex == -1){
return ""
}
def secondIndex = txt.indexOf('---', firstIndex + 3)
if(secondIndex == -1){
return ""
}
return txt.substring(firstIndex, secondIndex + 3)
}
// DSL withChangeSets has not been released, see also
// https://github.com/LinuxSuRen/change-handler