]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Split jenkinsfile so we can use the functions elsewhere
authorMatthew Newton <matthew-git@newtoncomputing.co.uk>
Mon, 11 Nov 2019 21:20:46 +0000 (21:20 +0000)
committerMatthew Newton <matthew-git@newtoncomputing.co.uk>
Mon, 11 Nov 2019 23:25:17 +0000 (23:25 +0000)
scripts/jenkins/Jenkinsfile
scripts/jenkins/Jenkinsfile.defs [new file with mode: 0644]

index 0bac4a3f3f8c281e6b7aae98dda065e0f50241c6..7f7f40f1f3c42d51d003a7c4f77de0ab04fc4a16 100644 (file)
@@ -1,82 +1,17 @@
-platforms = ["ubuntu14", "ubuntu16", "ubuntu18", "centos7", "centos8", "debian8", "debian9", "debian10"]
-
-/*  The detectChanges function looks at the last git commit and searches for changes to
- *   "redhat/freeradius.spec" or anything in the "debian" directory.  If it
- *   finds any changes to those files it returns the string " --no-cache "
- *   which can be fed to a docker build command to tell docker to rebuild
- *   the image from scratch so that it can pick up any new dependencies
- *   specified in those package manifest files
- */
-
-def detectChanges() {
-    def string = ""
-    def changeLogSets = currentBuild.changeSets
-    for (int i = 0; i < changeLogSets.size(); i++) {
-        def entries = changeLogSets[i].items
-        for (int j = 0; j < entries.length; j++) {
-            def entry = entries[j]
-            def files = new ArrayList(entry.affectedFiles)
-            for (int k = 0; k < files.size(); k++) {
-                def file = files[k]
-                if (file.path =~ /(^debian\/.*)|(redhat\/freeradius.spec)/) {
-                    echo "changes in file ${file.path}, passing --no-cache flag to docker build"
-                    string = " --no-cache "
-                    return string
-                }
-            }
-        }
-    }
-    return string
-}
-
 /*
- *  The buildClosures function is the core function of the script and uses
- *  function currying to be able to pass multiple dynamically generated
- *  jenkins build commands into a jenkins parallel block.
- *  This function ensures that the docker image is built and builds
- *  FreeRADIUS packages inside the docker image
+ *  Build FreeRADIUS packages for common Linux distributions.
+ *  Function definitions are pulled in from Jenkinsfile.defs so
+ *  that they can be used in other Jenkinsfiles.
  */
 
-def buildClosures(arg) {
-    println arg.inspect()
-    def platforms = arg
-    def closures = [:]
-    for (value in platforms) {
-        //final valueCopy = value
+node {
+       cleanWs(patterns: [[pattern: '**/*.deb , **/*.changes , **/*.buildinfo , **/*.rpm', type: 'INCLUDE']])
+       checkout scm
 
-        closures[value] = {platform ->
-                stage("build-${platform}") {
-                    docker.build("${platform}-test","-f ./scripts/jenkins/${platform}/Dockerfile ./scripts/jenkins/${platform}").inside {
-                        checkout([$class: 'GitSCM', branches: scm.branches, userRemoteConfigs: scm.userRemoteConfigs, extensions: [[$class: 'CleanBeforeCheckout'],[$class: 'RelativeTargetDirectory', relativeTargetDir: "${platform}/build"],[$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false]], submoduleCfg: []])
-                        sh "cat /etc/os-release || cat /etc/redhat-release"
-                        def commit_num = readFile("./build-number").trim()
-                        dir("${platform}/build/") {
-                            if (platform.contains("centos")) {
-                                sh 'mkdir -p rpmbuild/{SOURCES,SPECS,BUILD,BUILDROOT,RPMS,SRPMS}'
-                                sh 'tar -j -c --transform "s/./freeradius-server-$(cat VERSION)/" --exclude ".git" --exclude "rpmbuild" -f rpmbuild/SOURCES/freeradius-server-$(cat VERSION).tar.bz2 .'
-                                sh 'for file in $(grep "^Source...:" redhat/freeradius.spec | cut -d" " -f2) ; do cp redhat/$file rpmbuild/SOURCES/$file ; done'
-                                sh "sed -i -e \"s/^Release:.*\$/Release: ${commit_num}/\" redhat/freeradius.spec"
-                                sh 'rpmbuild --define "_topdir $(pwd)/rpmbuild" -bb redhat/freeradius.spec'
-                            } else {
-                                def version = sh (script: "dpkg-parsechangelog | grep '^Version: ' | awk '{print \$2}'", returnStdout: true).trim()
-                                def commit_msg = sh (script: "git log --oneline -1 \$GIT_COMMIT", returnStdout: true).trim()
+       def fr = load "scripts/jenkins/Jenkinsfile.defs"
+       fr.genBuildNumber()
 
-                                sh "dch -b -v ${version}-${commit_num} \"${commit_msg}\""
-                                sh "make deb"
-                            }
-                        }
-                    }
-                }
-                echo platform.toString()
-            }.curry(value)
-    }
-    closures
-}
+       parallel fr.buildClosures(platforms, scm.branches, scm.userRemoteConfigs)
 
-node {
-    cleanWs(patterns: [[pattern: '**/*.deb , **/*.changes , **/*.buildinfo , **/*.rpm', type: 'INCLUDE']])
-    checkout scm
-    sh (script: "git describe --tags --long --match 'release_*' --match 'branch_*' | sed -e \'s/^.*-\\([0-9]*\\)-g[0-9a-f]*/\\1/\' > build-number")
-    parallel buildClosures(platforms)
-    archiveArtifacts 'build-number,**/*.changes,**/*.buildinfo,**/*.deb,**/*.rpm'
+       archiveArtifacts artifacts
 }
diff --git a/scripts/jenkins/Jenkinsfile.defs b/scripts/jenkins/Jenkinsfile.defs
new file mode 100644 (file)
index 0000000..b79163c
--- /dev/null
@@ -0,0 +1,92 @@
+platforms = ["ubuntu14", "ubuntu16", "ubuntu18", "centos7", "centos8", "debian8", "debian9", "debian10"]
+artifacts = 'build-number, **/*.changes, **/*.buildinfo, **/*.deb, **/*.rpm'
+
+/*  The detectChanges function looks at the last git commit and searches for changes to
+ *   "redhat/freeradius.spec" or anything in the "debian" directory.  If it
+ *   finds any changes to those files it returns the string " --no-cache "
+ *   which can be fed to a docker build command to tell docker to rebuild
+ *   the image from scratch so that it can pick up any new dependencies
+ *   specified in those package manifest files
+ */
+
+def detectChanges() {
+    def string = ""
+    def changeLogSets = currentBuild.changeSets
+    for (int i = 0; i < changeLogSets.size(); i++) {
+        def entries = changeLogSets[i].items
+        for (int j = 0; j < entries.length; j++) {
+            def entry = entries[j]
+            def files = new ArrayList(entry.affectedFiles)
+            for (int k = 0; k < files.size(); k++) {
+                def file = files[k]
+                if (file.path =~ /(^debian\/.*)|(redhat\/freeradius.spec)/) {
+                    echo "changes in file ${file.path}, passing --no-cache flag to docker build"
+                    string = " --no-cache "
+                    return string
+                }
+            }
+        }
+    }
+    return string
+}
+
+/*
+ *  The buildClosures function is the core function of the script and uses
+ *  function currying to be able to pass multiple dynamically generated
+ *  jenkins build commands into a jenkins parallel block.
+ *  This function ensures that the docker image is built and builds
+ *  FreeRADIUS packages inside the docker image
+ */
+
+def buildClosures(arg, scmBranches, scmURC) {
+    println arg.inspect()
+    def platforms = arg
+    def closures = [:]
+    for (value in platforms) {
+        //final valueCopy = value
+
+        closures[value] = {platform ->
+                stage("build-${platform}") {
+                    docker.build("${platform}-test","-f ./scripts/jenkins/${platform}/Dockerfile ./scripts/jenkins/${platform}").inside {
+
+                        //checkout([$class: 'GitSCM', branches: scm.branches, userRemoteConfigs: scm.userRemoteConfigs, extensions: [[$class: 'CleanBeforeCheckout'],[$class: 'RelativeTargetDirectory', relativeTargetDir: "${platform}/build"],[$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false]], submoduleCfg: []])
+
+                        checkout([$class: 'GitSCM', branches: scmBranches, userRemoteConfigs: scmURC, extensions: [[$class: 'CleanBeforeCheckout'],[$class: 'RelativeTargetDirectory', relativeTargetDir: "${platform}/build"],[$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false]], submoduleCfg: []])
+                        sh "cat /etc/os-release || cat /etc/redhat-release"
+                        def commit_num = readFile("./build-number").trim()
+                        dir("${platform}/build/") {
+                            if (platform.contains("centos")) {
+                                sh 'mkdir -p rpmbuild/{SOURCES,SPECS,BUILD,BUILDROOT,RPMS,SRPMS}'
+                                sh 'tar -j -c --transform "s/./freeradius-server-$(cat VERSION)/" --exclude ".git" --exclude "rpmbuild" -f rpmbuild/SOURCES/freeradius-server-$(cat VERSION).tar.bz2 .'
+                                sh 'for file in $(grep "^Source...:" redhat/freeradius.spec | cut -d" " -f2) ; do cp redhat/$file rpmbuild/SOURCES/$file ; done'
+                                sh "sed -i -e \"s/^Release:.*\$/Release: ${commit_num}/\" redhat/freeradius.spec"
+                                sh 'rpmbuild --define "_topdir $(pwd)/rpmbuild" -bb redhat/freeradius.spec'
+                            } else {
+                                def version = sh (script: "dpkg-parsechangelog | grep '^Version: ' | awk '{print \$2}'", returnStdout: true).trim()
+                                def commit_msg = sh (script: "git log --oneline -1 \$GIT_COMMIT", returnStdout: true).trim()
+
+                                sh "dch -b -v ${version}-${commit_num} \"${commit_msg}\""
+                                sh "make deb"
+                            }
+                        }
+                    }
+                }
+                echo platform.toString()
+            }.curry(value)
+    }
+    closures
+}
+
+
+/*
+ *  Find the number of commits since the previous release and
+ *  write out to a file, "build-number". This is used in packages
+ *  so that new commits don't create packages with the same
+ *  version number.
+ */
+
+def genBuildNumber() {
+       sh (script: "git describe --tags --long --match 'release_*' --match 'branch_*' | sed -e \'s/^.*-\\([0-9]*\\)-g[0-9a-f]*/\\1/\' > build-number")
+}
+
+return this