Skip to content

Instantly share code, notes, and snippets.

@aerobless
Created January 29, 2018 12:08
Show Gist options
  • Save aerobless/61719d2515546210f1206354b6ceb4f6 to your computer and use it in GitHub Desktop.
Save aerobless/61719d2515546210f1206354b6ceb4f6 to your computer and use it in GitHub Desktop.
A Jenkinsfile describing the build process of a Angular4 & Spring Boot application
pipeline {
agent {
label 'BUILD' //Executed on agents with this label
}
triggers {
pollSCM('* * * * *') //Polls every minute. If your git repo supports web-hooks this is not necessary.
}
environment {
NO_CHROME_OPTS = "TRUE" //Environment variables
}
options {
disableConcurrentBuilds()
}
stages {
stage('Checkout') {
steps {
checkout scm //Checks out the SCM where the Jenkinsfile is located
addJFrogCredentialsToGradlePropertiesFile() //custom groovy method
}
}
stage('Frontend Static Code Analysis') { //stages are displayed in Jenkins UI
steps {
sshagent(['git']) { //provide SSH key for a git repo from another project necessary for the build, "git" is configured in Jenkins
script {
try {
sh './gradlew tslint --no-daemon'
} finally { //make checkstyle results accessible to user
checkstyle canComputeNew: false, defaultEncoding: '', healthy: '', pattern: 'frontend/tslint-result.xml', unHealthy: ''
}
}
}
}
}
stage('Frontend Unit Tests') {
steps {
sshagent(['git']) {
script {
try {
sh './gradlew cleanFrontendTest --no-daemon'
sh './gradlew frontendUnitTest --no-daemon'
} finally {
junit 'frontend/test/karma-result.xml' //make junit report accessible to user
}
}
}
}
}
stage('Unit & Integration Tests') {
steps {
script {
try {
sh './gradlew clean test --no-daemon'
} finally {
junit '**/build/test-results/test/*.xml'
}
}
}
}
stage('End 2 End Tests') {
steps {
script {
try {
sh './gradlew e2e --no-daemon'
} finally {
junit '**/e2e-results/junit-formatted/*.xml'
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'frontend/test/e2e-results/html-formatted/', reportFiles: 'htmlReport.html', reportName: 'End 2 End Test Report', reportTitles: ''])
} //make the HTML results of the e2e tests available to user
}
}
}
stage('Publish Artifact') {
steps {
sh './gradlew publish --no-daemon'
}
}
stage('SonarQube Analysis') {
steps {
script {
scannerHome = tool 'sonarscanner'
}
withSonarQubeEnv('sonar') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
}
post {
always { //If the build fails, send an email to the last person(s) to commit since the last green build
step([$class : 'Mailer',
notifyEveryUnstableBuild: true,
recipients : [emailextrecipients([[$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider']])].join(' ')])
}
}
}
private void addJFrogCredentialsToGradlePropertiesFile() { //Write jFrog credentials to gradle.properties file
withCredentials([usernamePassword(credentialsId: 'jFrog', usernameVariable: 'JFROG_USER', passwordVariable: 'JFROG_PASSWORD')]) {
sh "rm -f ~/.gradle/gradle.properties"
sh "echo jFrogUser=$JFROG_USER >> ~/.gradle/gradle.properties"
sh "echo jFrogPassword=$JFROG_PASSWORD >> ~/.gradle/gradle.properties"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment