forked from apache/netbeans-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.groovy
165 lines (163 loc) · 7.1 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
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
/*
* This is a multibranch pipeline that:
* - Checks out (shallow, read-only) the 'master' branch from netbeans-website (github) in the 'master-branch' directory.
* - Checks out the 'asf-site' branch from netbeans-website (gitbox) in the 'asf-site-branch' directory.
* - Runs a gradle job to build the website in the 'master-branch' directory.
* - Copies the resulting web stuff (html, images, etc.) to the 'asf-site-branch' directory.
* - Commits and pushes to the 'asf-site' branch, for later publishing.
*
*
* Note from [email protected]: That what once was a simple shell script is now a nightmare of complexity and a waste of computing resources.
*
* Useful references:
* - The pipeline DSL nightmare reference is available at https://www.jenkins.io/doc/book/pipeline/syntax/
* -
*/
pipeline {
// 'git-websites' as set by ASF Infra. This is the label used to build websites.
agent { node { label 'git-websites' } }
tools { jdk 'jdk_11_latest' }
triggers {
pollSCM('') // Enabling being build on Push
}
options {
buildDiscarder(logRotator(numToKeepStr: '6'))
disableConcurrentBuilds()
timeout(time: 50, unit: 'MINUTES')
}
stages {
stage("Shallow clone of github master branch") {
steps {
script {
def masterBranch = checkout([
$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'MessageExclusion', excludedMessage: 'Automated site publishing.*'],
[$class: 'CloneOption', depth: 1, noTags: true, reference: '', shallow: true],
[$class: 'RelativeTargetDirectory', relativeTargetDir: 'master-branch']
],
submoduleCfg: [],
userRemoteConfigs: [[url: 'https://github.com/apache/netbeans-website.git']]
])
def siteBranch = checkout([
$class: 'GitSCM',
branches: [[name: '*/asf-site']],
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'MessageExclusion', excludedMessage: 'Automated site publishing.*'],
[$class: 'RelativeTargetDirectory', relativeTargetDir: 'asf-site-branch']
],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: '9b041bd0-aea9-4498-a576-9eeb771411dd',
url: 'https://gitbox.apache.org/repos/asf/netbeans-website.git'
]]
])
def gitCommit = masterBranch.GIT_COMMIT
def gitPreviousCommit = masterBranch.GIT_PREVIOUS_COMMIT
def gitPreviousSuccessfulCommit = masterBranch.GIT_PREVIOUS_SUCCESSFUL_COMMIT
println("GIT_COMMIT: ${gitCommit}")
println("GIT_PREVIOUS_COMMIT: ${gitPreviousCommit}")
println("GIT_PREVIOUS_SUCCESSFUL_COMMIT: ${gitPreviousSuccessfulCommit}")
println("MASTER BRANCH : ${masterBranch}")
// We want to rebuild the website when 'master' changes, not when 'asf-site' changes...
if (gitCommit.equals(gitPreviousSuccessfulCommit)) {
// vieiro/2020-07-25
// It seems there's no clean way to stop a build from a step without using exceptions and weird things.
// It seems "As a user I want to stop a build from a stage step" is not a user story jenkins authors had in mind.
// This is very unfortunate and has caused pain all around the world.
// https://devops.stackexchange.com/questions/885/cleanest-way-to-prematurely-exit-a-jenkins-pipeline-job-as-a-success
currentBuild.result = 'ABORTED'
error("Commit ${gitCommit} and previous commit ${gitPreviousCommit} are equal. No need to rebuild the website.")
} else {
println("Different commits, proceeding...")
}
}
dir('asf-site-branch') {
sh 'git fetch'
sh 'git checkout asf-site'
sh 'git pull'
sh 'git status'
}
}
}
stage("Building the website") {
steps {
dir('master-branch/netbeans.apache.org') {
sh 'chmod u+x ./gradlew'
sh './gradlew --version'
sh './gradlew --stop'
sh './gradlew clean --no-daemon'
sh './gradlew buildSite --no-daemon --no-build-cache --stacktrace'
}
}
}
stage("Updating asf-site branch") {
steps {
dir('asf-site-branch') {
sh script: '''
#!/usr/bin/env bash
echo "Deleting content directory..."
rm -rf content/
mkdir content
echo "Copying HTML files..."
cp -R ../master-branch/netbeans.apache.org/build/bake/* content
echo "Copying .htaccess..."
cp -R ../master-branch/netbeans.apache.org/src/content/.htaccess content
echo "Copying wiki/.htaccess..."
cp -R ../master-branch/netbeans.apache.org/src/content/wiki/.htaccess content/wiki
echo "All files copied"
cp ../master-branch/.asf.yaml .
echo "Copying asf yaml file to publish branch"
'''
}
}
}
stage("Commit and push to asf-site") {
steps {
dir('asf-site-branch') {
sh script: '''
#!/usr/bin/env bash
echo "Checking git status"
git status
echo "Adding content..."
git add -v content/
echo "Adding asf file"
git add -v .asf.yaml
echo "Commit to gitbox..."
git status
git commit -v -m "Automated site publishing by Jenkins build ${BUILD_NUMBER}"
if [ $? -ne 0 ]; then
echo "Commit failed."
exit 2
fi
echo "Pushing to gitbox... Disabled for testing"
git push -v origin asf-site
if [ $? -ne 0 ]; then
echo "Push failed."
exit 3
fi
echo "Done."
'''
}
}
}
}
post {
cleanup {
println "Cleanup todo (using cleanWS to test)"
cleanWs()
}
success {
//echo "Success?"
slackSend (channel:'#netbeans-builds', message:"SUCCESS: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL}) ",color:'#00FF00')
}
failure {
//echo "Failure?"
slackSend (channel:'#netbeans-builds', message:"FAILED/ABORTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})",color:'#FF0000')
}
}
}