htlle-da-vorlage/Jenkinsfile

205 lines
7.8 KiB
Plaintext
Raw Permalink Normal View History

2020-04-16 14:08:44 +02:00
pipeline {
agent { label 'docker-lehrer' }
2020-04-16 22:43:53 +02:00
2020-04-16 14:08:44 +02:00
options {
buildDiscarder(logRotator(artifactNumToKeepStr: '0'))
2020-04-17 09:07:55 +02:00
skipDefaultCheckout(true)
2020-04-16 14:08:44 +02:00
}
2020-04-19 10:32:47 +02:00
2020-04-16 14:08:44 +02:00
parameters {
2020-04-19 10:32:47 +02:00
string(
name: 'REPOSITORY',
2020-04-20 10:16:24 +02:00
defaultValue: 'https://itsp.htl-leoben.at/git/Hg/HTLLE-DA-Vorlage.git',
2020-04-19 10:32:47 +02:00
description: 'URL git-Repository DA//'
2020-04-19 18:50:53 +02:00
)
2020-04-19 10:32:47 +02:00
string(
name: 'GIT_PATH',
defaultValue: './',
description: "Pfad zur DA in git (z.B.: dipl/)"
2020-04-19 18:50:53 +02:00
)
string(
name: 'GIT_BRANCH',
defaultValue: '*/master',
2020-04-27 18:33:51 +02:00
description: 'Experte: Wenn Sie einen anderen als den \'master\' Branch bauen möchten'
2020-04-19 18:50:53 +02:00
)
choice(
name: 'ARCHIVE_FORMAT',
2020-04-27 18:43:41 +02:00
choices: ['zip', 'tar.gz'],
description: 'Archiv-Typ als der die Arbeit per Mail versandt wird')
2020-04-16 14:08:44 +02:00
}
2020-04-19 10:32:47 +02:00
2020-04-16 14:08:44 +02:00
stages {
stage('Set some variables and clean workspace'){
2020-04-20 10:16:24 +02:00
steps{
cleanWs()
2020-04-19 10:32:47 +02:00
script {
2020-04-20 10:16:24 +02:00
// set template values
env.TEMPLATE_URL = 'https://itsp.htl-leoben.at/git/Hg/HTLLE-DA-Vorlage.git'
env.TEMPLATE_NAME = 'HTLLE-DA-Vorlage'
// builduser credentials id in jenkins store for gitea login
env.GIT_CRED_ID = 'd65d903b-21ee-4055-98aa-ef82a903e287'
2020-04-20 10:16:24 +02:00
2020-04-19 10:32:47 +02:00
// add https to the url if not present
env.REPOSITORY = params.REPOSITORY
if (!env.REPOSITORY.startsWith("https://")) {
2020-04-19 10:32:47 +02:00
env.REPOSITORY = "https://" + env.REPOSITORY
}
// sanitize git dir path
env.GIT_PATH = params.GIT_PATH
if (env.GIT_PATH.endsWith("/")) {
env.GIT_PATH = env.GIT_PATH.substring(
0, (env.GIT_PATH.length() - 1))
2020-04-19 10:32:47 +02:00
}
if (env.GIT_PATH.startsWith("/")) {
2020-04-19 10:32:47 +02:00
env.GIT_PATH = env.GIT_PATH.substring(1)
}
if (env.GIT_PATH.startsWith("~/")) {
2020-04-21 00:15:21 +02:00
env.GIT_PATH = env.GIT_PATH.substring(2)
2020-04-19 10:32:47 +02:00
}
if (env.GIT_PATH.contains("../")) {
2020-04-21 00:15:21 +02:00
error("GIT_PATH must not contain '../'")
2020-04-22 10:58:42 +02:00
}
2020-04-21 00:15:21 +02:00
2020-04-19 18:50:53 +02:00
// be sure branch is set
env.GIT_BRANCH = params.GIT_BRANCH ?: '*/master'
2020-04-27 18:43:41 +02:00
// output archive filename
env.ARCHIVE_FILENAME = 'diplomarbeit.' + params.ARCHIVE_FORMAT
// if following files exist add them to the output archive
env.ARCHIVE_FILES = 'diplomarbeit.pdf diplomarbeit.pdf.log spellcheck-results.txt'
2020-04-17 09:07:55 +02:00
}
2020-04-20 10:16:24 +02:00
}
}
stage('Checkout DA') {
when {
expression { env.TEMPLATE_URL != env.REPOSITORY }
}
2020-04-20 10:16:24 +02:00
steps {
2020-04-19 10:32:47 +02:00
// checkout out the repository including submodules
// builduser acc used in git
checkout([
$class: 'GitSCM',
2020-04-19 18:50:53 +02:00
branches: [[name: "${env.GIT_BRANCH}"]],
2020-04-19 10:32:47 +02:00
doGenerateSubmoduleConfigurations: false,
extensions: [[
$class: 'SubmoduleOption',
disableSubmodules: true,
2020-04-19 10:32:47 +02:00
parentCredentials: true,
recursiveSubmodules: false,
shallow: true,
2020-04-19 10:32:47 +02:00
reference: '',
trackingSubmodules: false
]],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: "${env.GIT_CRED_ID}",
2020-04-19 10:32:47 +02:00
url: "${env.REPOSITORY}"
]]
])
2020-04-16 15:54:33 +02:00
}
}
stage('Checkout template') {
steps {
// remove template folder and fresh checkout
sh "rm -rf ${env.GIT_PATH}/${env.TEMPLATE_NAME}"
checkout([
$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[
$class: 'RelativeTargetDirectory',
relativeTargetDir: "${env.GIT_PATH}/${env.TEMPLATE_NAME}"
]],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: "${env.GIT_CRED_ID}",
url: "${env.TEMPLATE_URL}"
]]
])
}
}
2020-04-19 10:32:47 +02:00
stage('Build DA') {
2020-04-16 14:08:44 +02:00
when {
2020-04-20 10:16:24 +02:00
expression { env.TEMPLATE_URL != env.REPOSITORY }
2020-04-16 14:08:44 +02:00
}
steps {
2020-04-19 10:32:47 +02:00
dir(env.GIT_PATH) {
sh"""#!/bin/bash
make pdf -C ${env.TEMPLATE_NAME} SOURCEDIR=`pwd`
make spellcheck -C ${env.TEMPLATE_NAME} SOURCEDIR=`pwd`
"""
2020-04-19 10:32:47 +02:00
}
2020-04-16 14:08:44 +02:00
}
}
2020-04-19 10:32:47 +02:00
stage('Build only template') {
2020-04-16 14:08:44 +02:00
when {
2020-04-20 10:16:24 +02:00
expression { env.TEMPLATE_URL == env.REPOSITORY }
2020-04-16 14:08:44 +02:00
}
steps {
2020-04-19 10:32:47 +02:00
// build the template with the examples from DA point of view
2020-04-20 10:16:24 +02:00
sh """#!/bin/bash
cp -rv ${env.TEMPLATE_NAME}/example/. .
make pdf -C ${env.TEMPLATE_NAME} SOURCEDIR=`pwd`
"""
2020-04-16 14:08:44 +02:00
}
}
2020-04-19 10:32:47 +02:00
stage('Test if diplomarbeit.pdf exists') {
2020-04-16 14:08:44 +02:00
steps {
2020-04-19 10:32:47 +02:00
dir(env.GIT_PATH) {
2020-04-16 15:54:33 +02:00
sh "test -f diplomarbeit.pdf"
2020-04-19 10:32:47 +02:00
}
2020-04-16 14:08:44 +02:00
}
}
stage('Create Archive') {
steps {
dir(env.GIT_PATH) {
2020-04-27 18:49:07 +02:00
script {
2020-04-28 15:54:04 +02:00
// only include actually created files to the archive
env.FILES_TO_INCLUDE = ''
def include = env.ARCHIVE_FILES.split(' ').each { filename ->
if (fileExists(filename)) {
env.FILES_TO_INCLUDE += (filename + ' ')
}
}
if (env.ARCHIVE_FORMAT == 'zip') {
sh "zip -q ${env.ARCHIVE_FILENAME} ${env.FILES_TO_INCLUDE}"
2020-04-27 18:49:07 +02:00
} else {
sh "tar -czf ${env.ARCHIVE_FILENAME} ${env.FILES_TO_INCLUDE}"
2020-04-27 18:49:07 +02:00
}
}
}
}
}
2020-04-16 14:08:44 +02:00
}
post {
2020-04-16 14:11:06 +02:00
always {
2020-04-19 10:32:47 +02:00
script {
env.RECIPIENTS = ""
// get mail addresses
// needs script approval in jenkins
def metadata = readFile(file: "${env.GIT_PATH}/metadata.yaml").split('\n').each { line ->
if (line.contains("- build-notification:")) {
// remove yaml comments
if (line.contains('#')) {
line = line.substring(0, line.indexOf('#'))
}
2020-04-19 10:32:47 +02:00
def mailMatch = line =~ /[_A-Za-z0-9-]+(.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(.[A-Za-z0-9]+)*(.[A-Za-z]{2,})/
if (mailMatch) {
env.RECIPIENTS += (mailMatch[0][0] + ";")
}
2020-04-19 10:32:47 +02:00
}
}
}
2020-04-19 11:11:37 +02:00
dir(env.GIT_PATH) {
2020-04-27 18:43:41 +02:00
emailext attachmentsPattern: "${env.ARCHIVE_FILENAME}",
2020-04-19 11:11:37 +02:00
to: "${env.RECIPIENTS}",
subject: "[${currentBuild.currentResult}] Diplomarbeit Build #${env.BUILD_NUMBER}",
body: "Job ${env.JOB_NAME}: ${env.JOB_URL}"
}
2020-04-16 14:08:44 +02:00
}
}
}