0

I have Upstream job A, passing "workspace" parameter to upstream job A. So, if job A triggers job B, it should use the job A specified workspace. The job B also needs to run independently, in that case it should use its own workspace.

In job B, I have specified string parameter name UPWORKSPACE and default value "unspecified", which is for reading the upstream job workspace parameter.


Job A:

agent {
 stages {
    agent 
      {  label 'test' } 
    stages 
     {
          stage ('Invoke_pipelineA') {
            steps {
                
                build job: '/Job B', parameters: [
          string(name: 'UPWorkspace', value: "${env.WORKSPACE}") ], wait: true 
            }
         }

job B: This project is parameterized: String Parameter Name: UPWorkspace Default Value : notspecified.

 pipeline { 
    agent none
      stages {
        stage("checkout ") {
            
            agent {
                node{
                 label "test"
                 customWorkspace "${UPWorkspace}"
                }
               } 
            steps {
                echo  "WORKSPACE:  ${paramWorkspace}"
                }
              }
            }

when Job A, triggers job B , its working fine, i could able to use custom workspace path, But if Job run independently i to tell job B to use normal workspace ?

Ideal solution would be if i am able to specify default value: env.WORKSPACE , in Job B, its perfect, but seems its not working.


some thing like this might be helpful, but i can't make it work in pipeline.

 if (params.UPWORKSPACE != "notspecified")   //use custom workspace when 
                default is not  "notspecified"
         {  customWorkspace "${UPWORKSPACE}"
               
         }

Any help is appreciated. thanks!

chandu
  • 375
  • 3
  • 16
  • In Job1, you can simply save the `workspace` into a `groovy variable` and pass it as a parameter to the downstream job. How are you triggering the downstream job, you just need to add an `argument` there. – mdabdullah Jul 25 '20 at 10:08
  • I am already doing that. In job1 Iam passing the workspace as variable (UPWORKSPACE) to job2. For triggering downstreamjob something like this: node: { build job: 'job2', parameters: [[$class: 'StringParameterValue', name: 'UPWORKSPACE', value: /0pt/jenkins]] } – chandu Jul 25 '20 at 13:10
  • Unfortunately your question is not having complete details. Maybe you can post the complete pipelines from both jobs. This may help you: https://stackoverflow.com/questions/36306883/how-can-i-trigger-another-job-from-a-jenkins-pipeline-jenkinsfile-with-github – mdabdullah Jul 25 '20 at 15:49
  • i have updated my query for better understanding. may be its bit more clear . – chandu Jul 27 '20 at 13:08
  • The cleaner way to do it is, in your `String parameter` you can have a default value embedded. Whenever a null value is passed you can just consume the default parameter. – mdabdullah Jul 27 '20 at 17:26
  • yes, any idea , how can i have env Variable as default value. ? ex. env.WORKSPACE is not working – chandu Jul 28 '20 at 06:00
  • 1
    Don't take it as an `env.variable` define the `String Variable` on top as a parameter and it will be a parameterized build always. If parent job triggers it will use that workspace, else it will use whatever `default value` is specified `String Parameter`. Also, you should paste pic of your parameters sections if you want further help. Reference https://st-g.de/2016/12/parametrized-jenkins-pipelines – mdabdullah Jul 28 '20 at 08:21
  • node('test') { properties([ parameters([ stringParam( defaultValue: env.WORKSPACE, description: '', name: 'UPWORKSPACE' ), ]) ]) Its working in scripted pipeline, but mine is declarative pipeline. not sure how to fit this inside job B – chandu Jul 28 '20 at 22:00

0 Answers0