Skip to content

Instantly share code, notes, and snippets.

@akostadinov
Last active April 15, 2020 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akostadinov/fc8940fda63a7489492b637063233524 to your computer and use it in GitHub Desktop.
Save akostadinov/fc8940fda63a7489492b637063233524 to your computer and use it in GitHub Desktop.
Jenkins Pipeline code Examples
// clone in sub-directory
checkout([
$class: 'GitSCM',
branches: [[name: 'master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[
$class: 'RelativeTargetDirectory',
relativeTargetDir: 'some/path'
]],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: 'a9fb86e4-bd29-425f-a834-16efe6009d84',
url: 'ssh://myuser@my-gerrit-repo.com:22/myproject'
]]
])
// Get parameter value of an existing build from another job
// this fails in sandbox
def job = Jenkins.instance.getItemByFullName("My Job")
def build = job.getBuildByNumber(buildNum.toInteger())
// println build.getEnvVars().get('CUCUSHIFT_CONFIG', null)
paramValue = build.allActions.
find {it in hudson.model.ParametersAction}.
getParameter("MY_PARAM_NAME").
value
// Launch Shell with variables and ascii color highlighting
dir('some/path') {
withEnv(["MY_ENV_VAR=${multilineString}"]) {
ansiColor('gnome-terminal') {
sh 'echo "$MY_ENV_VAR"'
}
}
}
@dyennam
Copy link

dyennam commented Jan 22, 2019

Hi, Just a quick question:
Shouldn't line 20 say buildNumber.toInteger() ?

@akostadinov
Copy link
Author

@dyennam, this is a variable you should have defined. It is not a method to get current build number or something. The thing is it needs to be an integer. What variable name you use is not important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment