Difference between revisions of "Jenkins/Job DSL, Pipelines, JaaC"
< Jenkins
Jump to navigation
Jump to search
Line 128: | Line 128: | ||
} | } | ||
} | } | ||
</source> | |||
Example multiline shell script takes interpreter from first non-blank line. A workaround of putting .trim() at the end of the triple-double quote does work. | |||
<source lang="js"> | |||
sh """ | |||
#!/bin/bash -xel | |||
set -o pipefail | |||
# do stuff | |||
""".trim() | |||
</source> | </source> | ||
Revision as of 21:44, 17 May 2018
Example - explained line by line
job("Demo build job") {
scm {
git {
remote {
url 'https://github.com/lexandro/restapi-demo.git'
}
branch 'master'
shallowClone true
}
}
steps {
maven('compile')
}
}
|
|
Example - shell in line
hudson.FilePath workspace = hudson.model.Executor.currentExecutor().getCurrentWorkspace()
String scriptSH1 = workspace.list("test-certs.sh")[0].read().getText()
job("DSL_Inline_Certs_expiry_test_shell_inline") {
description("Creates Certs_expiry_test job"
authorization {
blocksInheritance(true)
permission('hudson.model.Item.Read:anonymous')
permission('hudson.model.Item.Discover:anonymous')
permissionAll('authenticated')
}
logRotator {
daysToKeep(-1)
numToKeep(10)
artifactDaysToKeep(-1)
artifactNumToKeep(-1)
}
wrappers {
colorizeOutput()
maskPasswords()
preBuildCleanup()
timestamps()
buildNameSetter {
template('#${BUILD_NUMBER} ${CHANNEL} ${ENV}')
runAtStart(true)
runAtEnd(false)
}
}
publishers {
wsCleanup {
deleteDirectories(true)
setFailBuild(false)
cleanWhenSuccess(true)
cleanWhenUnstable(false)
cleanWhenFailure(false)
cleanWhenNotBuilt(true)
cleanWhenAborted(true)
}
}
multiscm {
git { remote {
url("git@gitlab.com:pio2pio/dsl-jenkins.git")
credentials("123abc12-1234-1234-1234-abc123abc123")
branches('*/master') }
extensions { relativeTargetDirectory("secrets-non-prod") }
}
git { remote {
url("git@gitlab.com:pio2pio/dsl-jenkins.git")
credentials("123abc12-1234-1234-1234-abc123abc123")
branches('*/master') }
extensions { relativeTargetDirectory("secrets-prod") }
}
}
steps {
shell {
// command(scriptSH1)
command('''#!/bin/bash
red_bold="\e[1;31m"
green="\e[32m"
green_bold="\e[1;32m"
yellow_bold="\e[1;93m"
blue_bold="\e[1;34m"
reset="\e[0m"
datediff() {
d1=$(date -d "$1" +%s)
d2=$(date -d "$2" +%s)
echo $(( (d1 - d2) / 86400 ))
}
set -f
paths=('secrets-non-prod/ssl/*crt' 'secrets-prod/ssl/*crt')
today=$(date +"%Y%m%d")
today30=$(date -d "+30 days" +"%Y%m%d")
for path in ${paths[@]}; do
set +f #enable fileglobbing
echo ""
echo -e "${blue_bold} Certificates in ${path} ${reset}"
for i in $(ls -1 $path); do
enddate=$(date --date="$(openssl x509 -in $i -noout -enddate | cut -d= -f 2)" --iso-8601)
enddate_d=$(date -d $enddate +"%Y%m%d")
if [ $today -lt $enddate_d ] && [ $today30 -gt $enddate_d ]; then
colour="${yellow_bold} WARN"
elif [ $today30 -lt $enddate_d ]; then
colour="${green_bold} PASS"
else
colour="${red_bold} ERRO"
fi
echo -e "${colour} ${enddate} $(basename $i) DaysToExpire: $(datediff $enddate_d $today) ${reset}"
done | sort -k3r | column -t
set -f
done''')
}
}
}
Example multiline shell script takes interpreter from first non-blank line. A workaround of putting .trim() at the end of the triple-double quote does work.
sh """
#!/bin/bash -xel
set -o pipefail
# do stuff
""".trim()
Example - groovy variable substitution and for.each
def owner = 'integrations'
def project = 'jenkins-dsl'
def branchApi = new URL("https://api.github.com/repos/${owner}/${project}/branches")
def branches = new groovy.json.JsonSlurper().parse(branchApi.newReader())
branches.each {
def branchName = it.name
def jobName = "${owner}-${project}-${branchName}".replaceAll('/','-')
job(jobName) {
scm {
git {
remote {
github("${owner}/${project}")
}
branch("${branchName}")
createTag(false)
}
}
triggers {
scm('*/15 * * * *')
}
steps {
shell('ls -l')
}
}
}