Difference between revisions of "Jenkins/Job DSL, Pipelines, JaaC"
< Jenkins
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
= Example - explained line by line = | |||
{| | |||
| <source lang="java"> | |||
job("Demo build job") { | |||
scm { | |||
git { | |||
remote { | |||
url 'https://github.com/lexandro/restapi-demo.git' | |||
} | |||
branch 'master' | |||
shallowClone true | |||
} | |||
} | |||
steps { | |||
maven('compile') | |||
} | |||
} | |||
</source> | |||
| | |||
#In this line we are defining the closure as job and specifying the name. The name is mandatory! | |||
#Here's the definition start for version controller configuration | |||
#We are using git. | |||
#Defining the remote endpoint for git. | |||
#Specifying the URL of the repository | |||
#Closing the block | |||
#We are operation only on the master branch at the moment | |||
#To speed up the checkout process we are using shallow cloning. | |||
#closing block | |||
#the definition of the checkout part is done | |||
#Start the definition of build steps block | |||
#we are using maven to compile the code as demonstration | |||
|} | |||
= Example - shell in line = | = Example - shell in line = | ||
<source lang="js"> | <source lang="js"> |
Revision as of 00:01, 15 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 - 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') } } }