-
Notifications
You must be signed in to change notification settings - Fork 0
Home
In this example, I have used Declarative pipeline syntax. In this document, I am trying to explain some of the tricky parts of this pipeline of syntax. If you are new to Pipeline please have a look at Jenkins Pipeline documentation will be useful.
In this example, I have used a simple Hello World java program with a Maven build and a simple Junit test.
The Pipeline script is written to execute following steps in Jenkins.
- Checkout the project to a custom workspace.
- Build the project with Maven, run tests.
- Checkout pipeline-sub into a new folder inside the workspace.
- Export some variables using a block shell script.
- Run another shell script that uses the values of exported variables.
- Publish JUnit test results.
- Publish project artifacts.
- Send email notification if the build is a Failure, Unstable or Back to Normal. (No emails for back to back successful builds.)
- Send Mattermost notification with build time, build URL and test results summary.
Jenkins configuration
- Add a new Multibranch Pipeline project in Jenkins as explained in Jenkins docs.
- For the branch source select Github instead of Git. If GitHub is not displayed as an option, please install Jenkins GitHub plugin.
- Add credentials for the repository and put your GitHub username as the Owner. Then select the repository.
- Choose the discover branches and discover pull requests strategy. If you need to build pull requests automatically please choose from here.
-
If your GitHub repository is very large and take few minutes to clone and you need to reduce that time you can add advanced clone behaviors. Click Add button and choose Advanced clone behaviours.
- Shallow clone and Shallow clone depth When this option is selected Jenkins only checkout the repository with the history up to the specified depth. This option can speed up the cloning by limiting the size of fetch.
- Path of the reference repo to use during clone You can checkout a Git bare repository to the Jenkins server beforehand and set path to that repository here. Jenkins will use that repository as a reference when checkingout the project. This helps to reduce the checkout time and the disk space in the Jenkins server. More details are here.
-
Under Build Configuration set Mode to by Jenkinsfiles and set path to Jenkinsfile (In the example it has been set to Jenkinsfile because Jenkinsfile is located in the project root.
These are the basic configurations we need to have to get build running with Jenkins.
I think most of the code in Jenkinsfile is self explanatory. So I will only explain things which I think non-trivial.
- How to read project name and branch name inside Jenkinsfile?
Job name has the following format : /<branch_name>
def jobnameparts = JOB_NAME.tokenize('/') as String[]
def jobconsolename = jobnameparts[0]
def branchname = jobnameparts[1]
https://github.com/hpmtissera/pipeline/blob/master/Jenkinsfile#L2-L4
- How to configure Jenkins Pipeline to use a custom workspace?
agent {
node {
label 'master'
customWorkspace "${jobconsolename}"
}
}
For the customWorkspace property you need to specify the relative path from the Jenkins working directory.
- How to build another repository as a part of Pipeline build?
dir('pipeline-sub') {
git branch: 'mybranch',
url: 'https://github.com/prasadlvi/pipeline-sub.git'
sh '''
cat README.md
'''
}
https://github.com/hpmtissera/pipeline/blob/master/Jenkinsfile#L40-L57
- How to run a shell script inside Jenkins Pipeline build?
sh '''
export prev_version='My previous version'
cd ..
export branch_name=${PWD##*/}
cd -
./Build/test-script.sh
'''
https://github.com/hpmtissera/pipeline/blob/master/Jenkinsfile#L40-L57
- How to read current build result inside Jenkins file?
if (currentBuild.result == null) {
currentBuild.result = 'SUCCESS'
// Do something
} else if(currentBuild.result == 'FAILURE' || currentBuild.result == 'UNSTABLE') {
// Do something
}
https://github.com/hpmtissera/pipeline/blob/master/Jenkinsfile#L67-L80
- How to send email notification in Jenkins pipeline.
emailext to: 'test@test.com test1@test.com', subject: '$DEFAULT_SUBJECT', body: '$DEFAULT_CONTENT'
DEFAULT_SUBJECT and DEFAULT_CONTENT can be configured in Jenkins > Manage Jenkins (Jenkins main configuration page) as shown in the screenshot
- How to send Slack / Mattermost notifications using Jenkins pipeline?
mattermostSend message: "${env.JOB_NAME} - #${env.BUILD_NUMBER} after ${currentBuild.durationString.replace(' and counting', '')} <${env.BUILD_URL}|Open>${summary}"
- How to get test results summary inside Jenkins file?
def testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
if (testResultAction != null) {
def total = testResultAction.getTotalCount()
def failed = testResultAction.getFailCount()
def skipped = testResultAction.getSkipCount()
summary = "\nTest results:\n\t"
summary = summary + ("Passed: " + (total - failed - skipped))
summary = summary + (", Failed: " + failed)
summary = summary + (", Skipped: " + skipped)
} else {
summary = "No tests found"
}
- How to read a variable defined in the Jenkinsfile from inside a shell script?
As I understand this has to be done in 3 steps. Please refer the source code to understand more clearly.
Define the variable
def jobconsolename = jobnameparts[0]
Export the variable
environment {
jobconsolenameshell = "${jobconsolename}"
}
Use the variable inside a shell script.
sh "echo \$jobconsolenameshell"
sh '''
echo 'inside shell script'
echo \$jobconsolenameshell
'''
Finally this is how to get current build duration. Please note that the variable output has "and counting" part because we are reading the value inside the Pipeline file and that mean build is not end yet.
echo "Duration : ${currentBuild.durationString.replace(' and counting', '')}"