Snippets

troii Software GmbH Jenkinsfile Groovy Syntax

You are viewing an old version of this snippet. View the current version.
Revised by Thomas Einwaller 619fcf8
#!/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"
					currentBuild.result = "SUCCESS"
				} catch (ex) {
					currentBuild.result = "UNSTABLE"
				} finally {
					junit allowEmptyResults: true, testResults: "**/build/test-results/test/*.xml"
				}
			}

			stage("Build") {
				sh "./gradlew assemble"
				currentBuild.result = "SUCCESS"
			}

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

		}

	} finally {
		if (currentBuild.result == "SUCCESS") {
			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>)")
		}
	}

}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.