[Asterisk-code-review] CI: Add Jenkins CI to certified/13.21 branch (...testsuite[certified/13.21])

George Joseph asteriskteam at digium.com
Tue Aug 6 11:12:10 CDT 2019


George Joseph has submitted this change and it was merged. ( https://gerrit.asterisk.org/c/testsuite/+/11688 )

Change subject: CI:  Add Jenkins CI to certified/13.21 branch
......................................................................

CI:  Add Jenkins CI to certified/13.21 branch

Change-Id: I9509e72a2bc84fa96fc8b96c305f11afdbcb3307
---
A CI/ci.functions
A CI/gates.jenkinsfile
A CI/setupJenkinsEnvironment.sh
A CI/unittests.jenkinsfile
4 files changed, 459 insertions(+), 0 deletions(-)

Approvals:
  George Joseph: Looks good to me, approved; Approved for Submit



diff --git a/CI/ci.functions b/CI/ci.functions
new file mode 100644
index 0000000..67616c2
--- /dev/null
+++ b/CI/ci.functions
@@ -0,0 +1,30 @@
+#
+# This file contains useful Bash functions
+# and can be "source"d from the scripts.
+#
+
+declare -a POSITIONAL_ARGS
+for a in "$@" ; do
+	OPTION_COUNT+=1
+	case "$a" in
+		--*=*)
+			[[ $a =~ --([^=]+)=(.*) ]]
+			l=${BASH_REMATCH[1]//-/_}
+			r=${BASH_REMATCH[2]}
+			eval ${l^^}=\"$r\"
+			;;
+		--*)
+			[[ $a =~ --(.+) ]]
+			l=${BASH_REMATCH[1]//-/_}
+			eval ${l^^}=1
+			;;
+		*)
+			POSITIONAL_ARGS+=($a)
+			;;
+	esac
+done
+
+runner() {
+	( set -x ; ${@} )
+}
+
diff --git a/CI/gates.jenkinsfile b/CI/gates.jenkinsfile
new file mode 100644
index 0000000..187eb74
--- /dev/null
+++ b/CI/gates.jenkinsfile
@@ -0,0 +1,207 @@
+/*
+ * This pipeline is the "template" for the Testsuite Unit Tests multi-branch
+ * parent job.  Jenkins will automatically scan the branches in the "testsuite"
+ * or "Security-testsuite" projects in Gerrit and automatically create a branch-
+ * specific job for each branch it finds this file in.
+ *
+ * This file starts as a declarative pipeline because with a declarative
+ * pipeline, you can define the trigger in the pipeline file.  This keeps
+ * everything in one place.  We transition to scripted pipeline later on because
+ * we need to dynamically determine which docker image we're going to use and
+ * you can't do that in a delcarative pipeline.
+ */
+pipeline {
+	triggers {
+		/*
+		 * This trigger will match either the "asterisk" or "Security-asterisk"
+		 * projects.  The branch is taken from the branch this job was created
+		 * for.
+		 */
+		gerrit customUrl: '',
+			commentTextParameterMode: 'PLAIN',
+			commitMessageParameterMode: 'PLAIN',
+			gerritBuildSuccessfulVerifiedValue: 2,
+			gerritBuildFailedVerifiedValue: -1,
+			gerritBuildUnstableVerifiedValue: -1,
+			gerritProjects: [
+				[branches: [[compareType: 'PLAIN', pattern: "${BRANCH_NAME}"]],
+					compareType: 'REG_EXP',
+					disableStrictForbiddenFileVerification: false,
+					pattern: '^(Security-)?testsuite.*'
+				]
+			],
+			silentMode: false,
+			triggerOnEvents: [
+				commentAddedContains('^regate$'),
+				commentAdded(commentAddedTriggerApprovalValue: '+2', verdictCategory: 'Code-Review'),
+			],
+			skipVote: [
+				onFailed: false,
+				onNotBuilt: true,
+				onSuccessful: false,
+				onUnstable: false
+			]
+	}
+	agent {
+		/* All of the stages need to be performed on a docker host */
+		label "swdev-docker"
+	}
+
+	stages {
+		stage ("->") {
+			/*
+			 * Jenkins will try to automatically rebuild this job when
+			 * the jenkinsfile changes but since this job is dependent on
+			 * Gerrit, we really don't want to do anything in that case.
+			 */
+			when {
+				not { environment name: 'GERRIT_CHANGE_NUMBER', value: '' }
+				not { environment name: 'GERRIT_EVENT_ACCOUNT_NAME', value: 'Jenkins2' }
+			}
+			steps {
+				script {
+					manager.build.displayName = "${env.GERRIT_CHANGE_NUMBER}"
+					manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Host: ${NODE_NAME}", false)
+
+					stage ("Checkout") {
+						sh "sudo chown -R jenkins:users ."
+						env.GERRIT_PROJECT_URL = env.GIT_URL.replaceAll(/[^\/]+$/, env.GERRIT_PROJECT)
+						sh "printenv | sort"
+
+						/*
+						 * Jenkins has already automatically checked out the base branch
+						 * for this change but we now need to check out the change itself
+						 * and rebase it on the current base branch.  If the rebase fails,
+						 * that's an indication to the user that they'll need to sort their
+						 * change out.
+						 *
+						 * The Gerrit Trigger provides all the URLs and refspecs to
+						 * check out the change.
+						 */
+						checkout scm: [$class: 'GitSCM',
+							branches: [[name: env.GERRIT_BRANCH ]],
+							extensions: [
+								[$class: 'ScmName', name: env.GERRIT_NAME],
+								[$class: 'CleanBeforeCheckout'],
+								[$class: 'PreBuildMerge', options: [
+									mergeRemote: env.GERRIT_NAME,
+									fastForwardMode: 'NO_FF',
+									mergeStrategy: 'RECURSIVE',
+									mergeTarget: env.GERRIT_BRANCH]],
+								[$class: 'CloneOption',
+									honorRefspec: true,
+									noTags: true,
+									depth: 10,
+									shallow: true
+								],
+								[$class: 'PruneStaleBranch'],
+								[$class: 'BuildChooserSetting',
+									buildChooser: [$class: 'GerritTriggerBuildChooser']
+								]
+							],
+							userRemoteConfigs: [
+								[name: env.GERRIT_NAME, refspec: env.GERRIT_REFSPEC, url: env.GERRIT_PROJECT_URL ]
+							]
+						]
+
+						sh "sudo CI/setupJenkinsEnvironment.sh --output-dir=CI/output"
+					}
+
+					def images = env.DOCKER_IMAGES_TESTSUITE.split(' ')
+					def r = currentBuild.startTimeInMillis % images.length
+					def ri = images[(int)r]
+					def randomImage = env.DOCKER_REGISTRY + "/" + ri;
+					
+					def bt = env.BUILD_TAG.replaceAll(/[^a-zA-Z0-9_.-]/, '-')
+					def dockerOptions = "--ulimit core=0 --ulimit nofile=10240 " +
+						" -v /srv/jenkins:/srv/jenkins:rw -v /srv/cache:/srv/cache:rw " +
+						" --entrypoint='' --name ${bt}-build"
+					def outputDir = "CI/output"
+					def astDir = "${outputDir}/asterisk"
+
+					manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Image: ${randomImage}", false)
+					def img = docker.image(randomImage)
+					img.pull()
+					img.inside(dockerOptions) {
+						def asteriskUrl = env.GERRIT_PROJECT_URL.replaceAll(/\/(Security-)?[^\/]+$/, "/\$1asterisk")	
+						checkout scm: [$class: 'GitSCM',
+							branches: [[name: "${BRANCH_NAME}"]],
+							extensions: [
+								[$class: 'RelativeTargetDirectory', relativeTargetDir: astDir],
+								[$class: 'CloneOption',
+									noTags: true,
+									depth: 10,
+									honorRefspec: true,
+									shallow: true
+								],
+							],
+							userRemoteConfigs: [[name: env.GERRIT_NAME, url: asteriskUrl]]
+						]
+						
+						stage ('Build') {
+							echo 'Building..'
+
+							sh """
+								cd ${astDir}
+								./tests/CI/buildAsterisk.sh --output-dir=${env.WORKSPACE}/${outputDir} --cache-dir=/srv/cache
+								sudo ./tests/CI/installAsterisk.sh --user-group=jenkins:users
+							"""									
+						}
+						stage ('Test') {
+							sh "./self_test"
+						}
+					}
+				}
+			}
+		}
+	}
+	post {
+		cleanup {
+			sh "sudo make distclean 2&>/dev/null || : "
+			sh "sudo rm -rf CI/output 2&>/dev/null || : "
+		}
+		/*
+		 * The Gerrit Trigger will automatically post the "Verified" results back
+		 * to Gerrit but the verification publisher publishes extra stuff in the
+		 * "Code Review" section of the review.
+		 */
+		always {
+			script {
+				def cat
+				def comment
+				def rvalue
+				switch (currentBuild.currentResult) {
+					case ~/^SUCCESS$/:
+						cat = "Passed"
+						comment = ""
+						rvalue = 1
+						break
+					case ~/^FAILURE$/:
+						cat = "Failed"
+						comment = "Fatal Error"
+						rvalue = -1
+						break
+					case ~/^UNSTABLE$/:
+						cat = "Failed"
+						comment = "Tests Failed"
+						rvalue = -1
+						break
+				}
+
+				gerritverificationpublisher verifyStatusValue: rvalue,
+					verifyStatusCategory: cat, verifyStatusURL: '',
+					verifyStatusComment: comment, verifyStatusName: '',
+					verifyStatusReporter: 'Jenkins2', verifyStatusRerun: 'regate'
+			}
+		}
+		success {
+			echo "Reporting ${currentBuild.currentResult} Passed"
+		}
+		failure {
+			echo "Reporting ${currentBuild.currentResult}: Failed: Fatal Error"
+		}
+		unstable {
+			echo "Reporting ${currentBuild.currentResult}: Failed: Tests Failed"
+		}
+	}
+}
diff --git a/CI/setupJenkinsEnvironment.sh b/CI/setupJenkinsEnvironment.sh
new file mode 100755
index 0000000..d97ad0a
--- /dev/null
+++ b/CI/setupJenkinsEnvironment.sh
@@ -0,0 +1,12 @@
+#!/usr/bin/env bash
+CIDIR=$(dirname $(readlink -fn $0))
+source $CIDIR/ci.functions
+
+mkdir -p /srv/cache/externals /srv/cache/sounds /srv/cache/ccache || :
+chown -R jenkins:users /srv/cache
+chmod g+rw /srv/cache/ccache
+chmod g+s /srv/cache/ccache
+if [ -n "${OUTPUT_DIR}" ] ; then
+	mkdir -p "${OUTPUT_DIR}" || :
+	chown -R jenkins:users "${OUTPUT_DIR}"
+fi
diff --git a/CI/unittests.jenkinsfile b/CI/unittests.jenkinsfile
new file mode 100644
index 0000000..33be6b0
--- /dev/null
+++ b/CI/unittests.jenkinsfile
@@ -0,0 +1,210 @@
+/*
+ * This pipeline is the "template" for the Testsuite Unit Tests multi-branch
+ * parent job.  Jenkins will automatically scan the branches in the "testsuite"
+ * or "Security-testsuite" projects in Gerrit and automatically create a branch-
+ * specific job for each branch it finds this file in.
+ *
+ * This file starts as a declarative pipeline because with a declarative
+ * pipeline, you can define the trigger in the pipeline file.  This keeps
+ * everything in one place.  We transition to scripted pipeline later on because
+ * we need to dynamically determine which docker image we're going to use and
+ * you can't do that in a delcarative pipeline.
+ */
+pipeline {
+	triggers {
+		/*
+		 * This trigger will match either the "asterisk" or "Security-asterisk"
+		 * projects.  The branch is taken from the branch this job was created
+		 * for.
+		 */
+		gerrit customUrl: '',
+			commentTextParameterMode: 'PLAIN',
+			commitMessageParameterMode: 'PLAIN',
+			gerritBuildSuccessfulVerifiedValue: 1,
+			gerritBuildFailedVerifiedValue: -1,
+			gerritBuildUnstableVerifiedValue: -1,
+			gerritProjects: [
+				[branches: [[compareType: 'PLAIN', pattern: "${BRANCH_NAME}"]],
+					compareType: 'REG_EXP',
+					disableStrictForbiddenFileVerification: false,
+					pattern: '^(Security-)?testsuite.*'
+				]
+			],
+			silentMode: false,
+			triggerOnEvents: [
+				commentAddedContains('^recheck$'),
+				patchsetCreated(excludeDrafts: false,
+								excludeNoCodeChange: true,
+								excludeTrivialRebase: false),
+				draftPublished()
+			],
+			skipVote: [
+				onFailed: false,
+				onNotBuilt: true,
+				onSuccessful: false,
+				onUnstable: false
+			]
+	}
+	agent {
+		/* All of the stages need to be performed on a docker host */
+		label "swdev-docker"
+	}
+
+	stages {
+		stage ("->") {
+			/*
+			 * Jenkins will try to automatically rebuild this job when
+			 * the jenkinsfile changes but since this job is dependent on
+			 * Gerrit, we really don't want to do anything in that case.
+			 */
+			when {
+				not { environment name: 'GERRIT_CHANGE_NUMBER', value: '' }
+				not { environment name: 'GERRIT_EVENT_ACCOUNT_NAME', value: 'Jenkins2' }
+			}
+			steps {
+				script {
+					manager.build.displayName = "${env.GERRIT_CHANGE_NUMBER}"
+					manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Host: ${NODE_NAME}", false)
+
+					stage ("Checkout") {
+						sh "sudo chown -R jenkins:users ."
+						env.GERRIT_PROJECT_URL = env.GIT_URL.replaceAll(/[^\/]+$/, env.GERRIT_PROJECT)
+						sh "printenv | sort"
+
+						/*
+						 * Jenkins has already automatically checked out the base branch
+						 * for this change but we now need to check out the change itself
+						 * and rebase it on the current base branch.  If the rebase fails,
+						 * that's an indication to the user that they'll need to sort their
+						 * change out.
+						 *
+						 * The Gerrit Trigger provides all the URLs and refspecs to
+						 * check out the change.
+						 */
+						checkout scm: [$class: 'GitSCM',
+							branches: [[name: env.GERRIT_BRANCH ]],
+							extensions: [
+								[$class: 'ScmName', name: env.GERRIT_NAME],
+								[$class: 'CleanBeforeCheckout'],
+								[$class: 'PreBuildMerge', options: [
+									mergeRemote: env.GERRIT_NAME,
+									fastForwardMode: 'NO_FF',
+									mergeStrategy: 'RECURSIVE',
+									mergeTarget: env.GERRIT_BRANCH]],
+								[$class: 'CloneOption',
+									honorRefspec: true,
+									noTags: true,
+									depth: 10,
+									shallow: true
+								],
+								[$class: 'PruneStaleBranch'],
+								[$class: 'BuildChooserSetting',
+									buildChooser: [$class: 'GerritTriggerBuildChooser']
+								]
+							],
+							userRemoteConfigs: [
+								[name: env.GERRIT_NAME, refspec: env.GERRIT_REFSPEC, url: env.GERRIT_PROJECT_URL ]
+							]
+						]
+
+						sh "sudo CI/setupJenkinsEnvironment.sh --output-dir=CI/output"
+					}
+
+					def images = env.DOCKER_IMAGES_TESTSUITE.split(' ')
+					def r = currentBuild.startTimeInMillis % images.length
+					def ri = images[(int)r]
+					def randomImage = env.DOCKER_REGISTRY + "/" + ri;
+					
+					def bt = env.BUILD_TAG.replaceAll(/[^a-zA-Z0-9_.-]/, '-')
+					def dockerOptions = "--ulimit core=0 --ulimit nofile=10240 " +
+						" -v /srv/jenkins:/srv/jenkins:rw -v /srv/cache:/srv/cache:rw " +
+						" --entrypoint='' --name ${bt}-build"
+					def outputDir = "CI/output"
+					def astDir = "${outputDir}/asterisk"
+
+					manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Image: ${randomImage}", false)
+					def img = docker.image(randomImage)
+					img.pull()
+					img.inside(dockerOptions) {
+						def asteriskUrl = env.GERRIT_PROJECT_URL.replaceAll(/\/(Security-)?[^\/]+$/, "/\$1asterisk")	
+						checkout scm: [$class: 'GitSCM',
+							branches: [[name: "${BRANCH_NAME}"]],
+							extensions: [
+								[$class: 'RelativeTargetDirectory', relativeTargetDir: astDir],
+								[$class: 'CloneOption',
+									noTags: true,
+									depth: 10,
+									honorRefspec: true,
+									shallow: true
+								],
+							],
+							userRemoteConfigs: [[name: env.GERRIT_NAME, url: asteriskUrl]]
+						]
+						
+						stage ('Build') {
+							echo 'Building..'
+
+							sh """
+								cd ${astDir}
+								./tests/CI/buildAsterisk.sh --output-dir=${env.WORKSPACE}/${outputDir} --cache-dir=/srv/cache
+								sudo ./tests/CI/installAsterisk.sh --user-group=jenkins:users
+							"""									
+						}
+						stage ('Test') {
+							sh "./self_test"
+						}
+					}
+				}
+			}
+		}
+	}
+	post {
+		cleanup {
+			sh "sudo make distclean 2&>/dev/null || : "
+			sh "sudo rm -rf CI/output 2&>/dev/null || : "
+		}
+		/*
+		 * The Gerrit Trigger will automatically post the "Verified" results back
+		 * to Gerrit but the verification publisher publishes extra stuff in the
+		 * "Code Review" section of the review.
+		 */
+		always {
+			script {
+				def cat
+				def comment
+				def rvalue
+				switch (currentBuild.currentResult) {
+					case ~/^SUCCESS$/:
+						cat = "Passed"
+						comment = ""
+						rvalue = 1
+						break
+					case ~/^FAILURE$/:
+						cat = "Failed"
+						comment = "Fatal Error"
+						rvalue = -1
+						break
+					case ~/^UNSTABLE$/:
+						cat = "Failed"
+						comment = "Tests Failed"
+						rvalue = -1
+						break
+				}
+
+				gerritverificationpublisher verifyStatusValue: rvalue,
+					verifyStatusCategory: cat, verifyStatusURL: '',
+					verifyStatusComment: comment, verifyStatusName: '',
+					verifyStatusReporter: 'Jenkins2', verifyStatusRerun: 'regate'
+			}
+		}
+		success {
+			echo "Reporting ${currentBuild.currentResult} Passed"
+		}
+		failure {
+			echo "Reporting ${currentBuild.currentResult}: Failed: Fatal Error"
+		}
+		unstable {
+			echo "Reporting ${currentBuild.currentResult}: Failed: Tests Failed"
+		}
+	}
+}

-- 
To view, visit https://gerrit.asterisk.org/c/testsuite/+/11688
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: testsuite
Gerrit-Branch: certified/13.21
Gerrit-Change-Id: I9509e72a2bc84fa96fc8b96c305f11afdbcb3307
Gerrit-Change-Number: 11688
Gerrit-PatchSet: 2
Gerrit-Owner: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190806/ac38d105/attachment-0001.html>


More information about the asterisk-code-review mailing list