-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
183 lines (164 loc) · 4.93 KB
/
Jenkinsfile
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env groovy Jenkinsfile
import java.text.SimpleDateFormat
pipeline {
agent any
triggers {
cron('H H 1 1 *')
}
parameters {
string(
name: 'post_title',
description: 'Used for filename and post.sh name. The space will be replaced by `-` inv the file name.',
defaultValue: ''
)
text(
name: 'post_body',
description: 'Use GitHub MD format',
defaultValue: 'This week on Java club we will'
)
booleanParam(
name: 'post_footer',
description: 'Add "Join us next Thursday, at 10:00 in room"',
defaultValue: true
)
choice(
name: 'room',
description: '',
choices: 'Online\nin Jamaica (room 223)\nin Spain (room 426)'
)
string(
name: 'details_url',
description: 'Path to video or GitHub repo',
defaultValue: ''
)
string(
name: 'tags',
description: 'Tags in lowercase are separated by commas',
defaultValue: ''
)
string(
name: 'image_url',
description: 'Path to photo',
defaultValue: '/images/default.jpg',
)
}
environment {
SLACK_AUTOMATION_CHANNEL = "#automation"
SLACK_AUTOMATION_TOKEN = credentials("jenkins-ci-integration-token")
JENKINS_HOOKS = credentials("jenkins-ci-hooks")
SLACK_DEEJAY_TOKEN = credentials("slack_deejay_hooks")
GIT_TOKEN = credentials("Jenkins-GitHub-Apps-Personal-access-tokens")
}
stages {
stage('Set up env') {
when {
branch 'master'
}
steps {
script {
env.SKIP_AUTO_RUN = params.post_title == ''
}
}
}
stage('Set up git env') {
when {
branch 'master'
environment name: 'SKIP_AUTO_RUN', value: 'false'
}
steps {
script {
dir("${env.WORKSPACE}") {
sh 'git fetch'
sh 'git pull'
sh "git config remote.origin.url 'https://${env.GIT_TOKEN}@github.com/lvivJavaClub/lvivJavaClub.github.io.git'"
sh 'git clean -fdx'
}
}
}
}
stage('Prepare post') {
when {
branch 'master'
environment name: 'SKIP_AUTO_RUN', value: 'false'
}
steps {
script {
def date = new Date()
def dateday = new SimpleDateFormat('yyyy-MM-dd').format(date)
def datetime = new SimpleDateFormat('yyyy-MM-dd H:m').format(date)
def title = "${params.post_title}".replaceAll('[ #]', '-')
env.FILENAME = "${dateday}-${title}.md"
env.MESSAGE = "${params.post_body.replaceAll("'ll", " will")}" +
({params.details_url} ? "${params.details_url}" : "") +
"\\n" +
({params.post_footer} ? "Join us next Thursday, at 10:00 ${params.room}" : "")
env.POST = "---\n" +
"layout: post\n" +
"title: \"${params.post_title}\"\n" +
"image: ${params.image_url}\n" +
"tags: [${params.tags}]\n" +
"date: ${datetime}:00 +0200\n" +
"---\n" +
"\n" +
"${params.post_body}" +
({params.details_url} ? "[${params.details_url}](${params.details_url})" : "") +
"\n\n" +
({params.post_footer} ? "Join us next Thursday, at 10:00 ${params.room}" : "")
sh 'printenv'
}
}
}
stage('Create post') {
when {
branch 'master'
environment name: 'SKIP_AUTO_RUN', value: 'false'
}
steps {
script {
dir("${env.WORKSPACE}/_posts") {
writeFile([file: "${env.FILENAME}", text: "${env.POST}"])
sh 'git status'
sh "git add ${env.FILENAME}"
sh "git commit -m '${params.post_title}'"
sshagent(['jenkins-deploy-keys']) {
sh "git push origin HEAD:${env.GIT_BRANCH}"
}
}
}
}
}
stage('Post reminders') {
when {
branch 'master'
environment name: 'SKIP_AUTO_RUN', value: 'false'
}
steps {
script {
sh "curl --silent --request POST --url '${env.SLACK_DEEJAY_TOKEN}' --data '{\"text\":\"${env.MESSAGE}\"}'"
}
}
}
}
post {
success {
slackSend(
baseUrl: "${env.JENKINS_HOOKS}",
token: "${env.SLACK_AUTOMATION_TOKEN}",
channel: "${env.SLACK_AUTOMATION_CHANNEL}",
botUser: true,
color: "good",
message: "BUILD SUCCESS: Job ${env.JOB_NAME} [${env.BUILD_NUMBER}]\nCheck console output at: ${env.BUILD_URL}"
)
}
failure {
slackSend(
baseUrl: "${env.JENKINS_HOOKS}",
token: "${env.SLACK_AUTOMATION_TOKEN}",
channel: "${env.SLACK_AUTOMATION_CHANNEL}",
botUser: true,
color: "danger",
message: "BUILD FAILURE: Job ${env.JOB_NAME} [${env.BUILD_NUMBER}]\nCheck console output at: ${env.BUILD_URL}"
)
}
}
}