I've tried using the following Script, but all downstream jobs are running on different nodes.
Any idea how can I get a random node and run all downstream jobs on the same one?
#!/usr/bin/env groovy
pipeline {
agent { label 'WindowsServer' }
stages{
stage("Get Dev Branch"){
steps {
script {
build(job: "GetDevBranchStep", parameters: [string(name: 'DevBranchName', value: "${params.CloudDevBranch}")])
}
}
}
stage("Get SA Branch"){
steps {
script {
build(job: "GetSABranchStep", parameters: [string(name: 'SABranchName', value: "${params.SABranch}")])
}
}
}
stage("Compile Models and Copy To Network Folder"){
steps {
script {
build(job: "CompileNewModelsAndCopyToNetwork", parameters: [string(name: 'DevBranchName', value: "${params.CloudDevBranch}"), string(name: 'SABranchName', value: "${params.SABranch}"), string(name: 'GetSAStepJobName', value: "GetSABranchStep"), string(name: 'GetDevRepoJobName', value: "GetDevBranchStep"), string(name: 'NetworkFoderToCopyTo', value: "NetworkFolderAddress")])
}
}
}
}
}
provide a downstream job with ${NODE_NAME} as additional parameter
in downstream job in agent section you can use:
agent { label "${params.NODE_NAME}" }
(meanwhile did not found how to inject parameters of upstream job to the downstream without actually insert them one by one as input parameters)
Related
pipeline {
agent { label 'linux' }
stages{
stage("verify1"){
steps {
script {
build(job: "verfiy1", parameters: [string(name: 'verfiy1', value: "${params.verfiy1}")])
}
}
}
stage("verify2"){
steps {
script {
build(job: "verfiy2", parameters: [string(name: 'verfiy2', value: "${params.verfiy2}")])
}
}
}
stage("verify3"){
steps {
script {
build(job: "verify3", parameters: [string(name: 'verify3', value: "${params.verify3}")])
}
}
}
}
}
=================================================================
Hello
can anyone help me, right now from the above pipeline i am able to build 3 jobs sucessfull but the problem is every single job is executing on new ec2 slave instance instead of the instance where the job has started. I am expecting the output as once the above pipeline starts all the builds in the pipeline must execute on the same node (ec2 instance).
Thanks in advance
You can pass the upstream job's agent node to the downstream job.
Add one more job parameter to accept node
Pass upstream job's agent node via env.NODE_NAME when call build job step
// verify 1 job
pipeline {
agent { label "${params.agentNode}" }
parameters {
string(name: "agentNode",
defaultValue="<give default value in case run it directly>" )
}
}
// upstream job
build(job: "verify1", parameters: [
string(name: 'agentNode', value: "${env.NODE_NAME}"),
string(name: 'verify3', value: "${params.verify3}")
])
I have the following pipeline. I need this pipeline to run on 4 different nodes at the same time. I have read that using a matrix section within the declarative pipeline is key to making this work. How can I go about doing that with the pipeline below?
pipeline
{
stages
{
stage ('Test')
{
steps
{
script
{
def test_proj_choices = ['AD', 'CD', 'DC', 'DISP_A', 'DISP_PROC', 'EGI', 'FD', 'FLT', 'FMS_C', 'IFF', 'liblO', 'libNGC', 'libSC', 'MISCMP_MP', 'MISCMP_GP', 'NAV_MGR', 'RADALT', 'SYS', 'SYSIO15', 'SYSIO42', 'SYSRED', 'TACAN', 'VOR_ILS', 'VPA', 'WAAS', 'WCA']
for (choice in test_proj_choices)
{
stage ("${choice}")
{
echo "Running ${choice}"
build job: "UH60Job", parameters: [string(name: "TEST_PROJECT", value: choice), string(name: "SCADE_SUITE_TEST_ACTION", value: "all"), string(name: "VIEW_ROOT", value: "myview")]
}
}
}
}
}
}
}
One helpful article can be found here : https://www.jenkins.io/blog/2019/11/22/welcome-to-the-matrix/
The official documentation here: https://www.jenkins.io/doc/book/pipeline/syntax/#declarative-matrix
Accordingly, the syntax should be:
pipeline {
agent none
stages {
stage('Tests') {
matrix {
agent any
axes {
axis {
name 'CHOICE'
values 'AD', 'CD', 'DC', 'DISP_A', 'DISP_PROC', 'EGI', 'FD', 'FLT', 'FMS_C', 'IFF', 'liblO', 'libNGC', 'libSC', 'MISCMP_MP', 'MISCMP_GP', 'NAV_MGR', 'RADALT', 'SYS', 'SYSIO15', 'SYSIO42', 'SYSRED', 'TACAN', 'VOR_ILS', 'VPA', 'WAAS', 'WCA'
}
}
stages {
stage("Test") {
steps {
echo "Running ${CHOICE}"
build job: "UH60Job", parameters: [string(name: "TEST_PROJECT", value: CHOICE), string(name: "SCADE_SUITE_TEST_ACTION", value: "all"), string(name: "VIEW_ROOT", value: "myview")]
}
}
}
}
}
}
}
Note that your inner stage cannot be named dynamically, you'd get a syntax error trying to expand "${CHOICE}".
I have a parameterized Jenkins Pipeline with a default value and I'm trying to pass that param as a script argument but it doesn't seem to pass anything. Here is the script :
pipeline {
agent any
stages {
stage('Building') {
steps {
build job: 'myProject', parameters: [string(name: 'configuration', value: '${configuration}')]
}
}
stage('Doing stuff') {
steps {
sh "~/scripts/myScript ${configuration}"
}
}
}
}
It seems to work for the build step but not for the script. I returns an error saying I have no argument.
I tried to get it with ${configuration}, ${params.configuration} and $configuration.
What is the right way to access a param and pass it correctly to a script ?
Thanks.
Actually, you are using the build step, to pass a parameter to the Jenkins job 'myProject'.
build job: 'myProject', parameters: [string(name: 'configuration', value: '${configuration}')]
If you want to declare a Parameter in this job you need to declare your parameter in a "parameters" block.
pipeline {
agent any
parameters {
string(defaultValue: '', description: '', name: 'configuration')
}
stages {
stage('Doing stuff') {
steps {
sh "~/scripts/myScript ${configuration}"
}
}
}
}
To explain the issue, consider that I have 2 jenkins jobs.
Job1 : PARAM_TEST1
it accepts a parameterized value called 'MYPARAM'
Job2: PARAM_TEST2
it also accepts a parameterized value called 'MYPARAM'
Sometimes I am in need of running these 2 jobs in sequence - so i created a separate pipeline job as shown below. It works just fine.
it also accepts a parameterized value called 'MYPARAM' to simply pass it to the build job steps.
pipeline {
agent any
stages {
stage("PARAM 1") {
steps {
build job: 'PARAM_TEST1', parameters: [string(name: 'MYPARAM', value: "${params.MYPARAM}")]
}
}
stage("PARAM 2") {
steps {
build job: 'PARAM_TEST2', parameters: [string(name: 'MYPARAM', value: "${params.MYPARAM}")]
}
}
}
}
My question:
This example is simple. Actually I have 20 jobs. I do not want to repeat parameters: [string(name: 'MYPARAM', value: "${params.MYPARAM}")] in every single stage.
Is there any way to set the parameters for all the build job steps in one single place?
What you could do is place the common params on the pipeline level and add specific ones to those in the stages
pipeline {
agent any
parameters {
string(name: 'PARAM1', description: 'Param 1?')
string(name: 'PARAM2', description: 'Param 2?')
}
stages {
stage('Example') {
steps {
echo "${params}"
script {
def myparams = params + string(name: 'MYPARAM', value: "${params.MYPARAM}")
build job: 'downstream-pipeline-with-params', parameters: myparams
}
}
}
}
}
I am new to Jenkins. I have spent the last few weeks creating jobs to execute chains of shell commands, but now when I tried to find out how to chain jobs together, I have failed to find the answer I was looking for.
I have a CreateStack job, and if it fails somehow, I'd like to run DeleteStack to remove the stuff that CreateStack left behind while failing. If CreateStack does not fail, build the rest of the jobs.
Something like this:
b = build(job: "CreateStack", propagate: false, parameters: [string(name: 'TASVersion', value: "$TASVersion"), string(name: 'CloudID', value: "$CloudID"), string(name: 'StackName', value: "$StackName"), booleanParam(name: 'Swap partition required', value: true)]).result
if(b == 'FAILURE') {
echo "CreateStack has failed. Running DeleteStack."
build(job: "DeleteStack", parameters: [string(name: 'CloudID', value: "$CloudID"), string(name: 'StackName', value: "$StackName")]
}
else {
build job: 'TAS Deploy', parameters: [string(name: 'FT_NODE_IP', value: "$FT-NodeIP"), string(name: 'TASVersion', value: "RawTASVersion")]
}
Could somebody help me out with this, please?
Also, can I use variables in a pipeline script like this? I set the project to be parameterized and added the necessary choice parameters, e.g.: $StackName
You can try something like this in a scripted pipeline:
node {
try {
stage('CreateStack') {
build(job: 'CreateStack', parameters: [<parameters>])
}
stage('OtherJobs') {
#build the rest of the jobs
}
} catch (error) {
build(job: 'DeleteStack', parameters: [<parameters>])
currentBuild.result = "FAILURE"
throw error
} finally {
build(job: 'LastJob', parameters: [<parameters>])
}
}
Please note, that the catch block is executed if any job fails. There you have to implent a little additional logic.