snippetsJenkins.groovy
· 2.6 KiB · Groovy
原始文件
// Parameterize agent label
if (params.containsKey('AGENT_LABEL')) {
AGENT_LABEL = "$params.AGENT_LABEL"
}
pipeline {
agent { label "${AGENT_LABEL}" }
parameters {
string(name: 'AGENT_LABEL', defaultValue: 'labelA')
}
}
// Substitute pipeline environment variable in shared library code (environment{} block does not work)
withEnv(["ENV_ABC=123"]) {
script {
output = lib.function()
echo "output: ${output}"
}
}
// In shared library
String function() {
return "${ENV_ABC}"
}
// Set variable from sh stdout (full and last word)
// note: returnStdout removes the output from jenkins build console log, have to explicitly echo it
script {
stdout_full = sh (
script: "ls -ltr",
returnStdout: true
).trim()
echo "${stdout_full}"
stdout_last = full_stdout.tokenize().last()
echo "${stdout_last}"
}
// Active choice (plugin) parameters with dynamic editable user input
// outside pipeline{} block
properties([parameters([
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
omitValueField: true,
description: 'Editable field when PARENT_PARAM is Others',
name: 'ACTIVE_PARAM',
randomName: 'choice-parameter-5631314456178624',
referencedParameters: 'PARENT_PARAM',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: "return['undefined']"
],
script: [
classpath: [],
sandbox: true,
script:
"""
if(PARENT_PARAM.equals('Others')) {
inputBox="<input class='setting-input' name='value' type='text' value=''>"
} else {
inputBox="<input class='setting-input' name='value' type='hidden' value=''>"
}
"""
]
]
]
])])
// send mail to the executor of job (LDAP)
import hudson.model.User
def userId = currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause)?.getUserId()
if (userId) {
def user = User.get(userId)
env.user_email = user.getProperty(hudson.tasks.Mailer.UserProperty)?.getAddress()
}
// Check if branch exists on remote
def branchCheckOutput = bat(
script: "git ls-remote --heads origin ${env.JENKINS_UPLOAD_LABEL}",
returnStdout: true
).trim()
if (branchCheckOutput =~ /refs\/heads\/${env.JENKINS_UPLOAD_LABEL}/) {
error "Branch ${env.JENKINS_UPLOAD_LABEL} already exists on remote."
}
| 1 | // Parameterize agent label |
| 2 | if (params.containsKey('AGENT_LABEL')) { |
| 3 | AGENT_LABEL = "$params.AGENT_LABEL" |
| 4 | } |
| 5 | pipeline { |
| 6 | agent { label "${AGENT_LABEL}" } |
| 7 | parameters { |
| 8 | string(name: 'AGENT_LABEL', defaultValue: 'labelA') |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | // Substitute pipeline environment variable in shared library code (environment{} block does not work) |
| 13 | withEnv(["ENV_ABC=123"]) { |
| 14 | script { |
| 15 | output = lib.function() |
| 16 | echo "output: ${output}" |
| 17 | } |
| 18 | } |
| 19 | // In shared library |
| 20 | String function() { |
| 21 | return "${ENV_ABC}" |
| 22 | } |
| 23 | |
| 24 | // Set variable from sh stdout (full and last word) |
| 25 | // note: returnStdout removes the output from jenkins build console log, have to explicitly echo it |
| 26 | script { |
| 27 | stdout_full = sh ( |
| 28 | script: "ls -ltr", |
| 29 | returnStdout: true |
| 30 | ).trim() |
| 31 | echo "${stdout_full}" |
| 32 | stdout_last = full_stdout.tokenize().last() |
| 33 | echo "${stdout_last}" |
| 34 | } |
| 35 | |
| 36 | // Active choice (plugin) parameters with dynamic editable user input |
| 37 | // outside pipeline{} block |
| 38 | properties([parameters([ |
| 39 | [$class: 'DynamicReferenceParameter', |
| 40 | choiceType: 'ET_FORMATTED_HTML', |
| 41 | omitValueField: true, |
| 42 | description: 'Editable field when PARENT_PARAM is Others', |
| 43 | name: 'ACTIVE_PARAM', |
| 44 | randomName: 'choice-parameter-5631314456178624', |
| 45 | referencedParameters: 'PARENT_PARAM', |
| 46 | script: [ |
| 47 | $class: 'GroovyScript', |
| 48 | fallbackScript: [ |
| 49 | classpath: [], |
| 50 | sandbox: true, |
| 51 | script: "return['undefined']" |
| 52 | ], |
| 53 | script: [ |
| 54 | classpath: [], |
| 55 | sandbox: true, |
| 56 | script: |
| 57 | """ |
| 58 | if(PARENT_PARAM.equals('Others')) { |
| 59 | inputBox="<input class='setting-input' name='value' type='text' value=''>" |
| 60 | } else { |
| 61 | inputBox="<input class='setting-input' name='value' type='hidden' value=''>" |
| 62 | } |
| 63 | """ |
| 64 | ] |
| 65 | ] |
| 66 | ] |
| 67 | ])]) |
| 68 | |
| 69 | // send mail to the executor of job (LDAP) |
| 70 | import hudson.model.User |
| 71 | def userId = currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause)?.getUserId() |
| 72 | if (userId) { |
| 73 | def user = User.get(userId) |
| 74 | env.user_email = user.getProperty(hudson.tasks.Mailer.UserProperty)?.getAddress() |
| 75 | } |
| 76 | |
| 77 | |
| 78 | // Check if branch exists on remote |
| 79 | def branchCheckOutput = bat( |
| 80 | script: "git ls-remote --heads origin ${env.JENKINS_UPLOAD_LABEL}", |
| 81 | returnStdout: true |
| 82 | ).trim() |
| 83 | |
| 84 | if (branchCheckOutput =~ /refs\/heads\/${env.JENKINS_UPLOAD_LABEL}/) { |
| 85 | error "Branch ${env.JENKINS_UPLOAD_LABEL} already exists on remote." |
| 86 | } |