Snippets

troii Software GmbH Jenkinsfile Groovy Syntax

Created by Thomas Einwaller last modified
#!/usr/bin/env groovy

def slack(String color, String message) {
	for (channel in [ "#builds", "#project-status" ]) {
		slackSend(channel: channel, color: color, message: message)
	}
}

node(label: "java8") {

	checkout scm

	try {

		withCredentials([usernamePassword(credentialsId: "nexusCredentials", passwordVariable: "ORG_GRADLE_PROJECT_nexusPassword", usernameVariable: "ORG_GRADLE_PROJECT_nexusUser")]) {

			stage("Test") {
				try {
					bitbucketStatusNotify(buildState: "INPROGRESS")
					sh "./gradlew clean test"
				} catch (ex) {
					currentBuild.result = "UNSTABLE"
				} finally {
					junit allowEmptyResults: true, testResults: "**/build/test-results/test/*.xml"
				}
			}

			stage("Build") {
				sh "./gradlew assemble"
			}

			stage("Publish") {
				if (BRANCH_NAME ==~ "master|release/.*|hotfix/.*") {
					dockerTag = sh(
							script: "./gradlew printDockerTag --quiet",
							returnStdout: true
					).trim()
					sh "./gradlew publish pushDockerImage"
					slack("good", "Docker Image $dockerTag build for ${currentBuild.fullDisplayName} finished")
				}
			}

		}

	} finally {
		if (currentBuild.result == null) {
			bitbucketStatusNotify(buildState: "SUCCESSFUL")
		} else if (currentBuild.result == "FAILURE" || currentBuild.result == "UNSTABLE" ) {
			bitbucketStatusNotify(buildState: "FAILED")
			slack("danger", "The pipeline ${currentBuild.fullDisplayName} failed. (<${env.BUILD_URL}|Open>)")
		}
	}

}

Comments (0)