Execute job from another job in the same workspace

I want to make a jenkinsfile that can build customized builds as i want them. Then i want to execute this jenkinsfile/job from other predefined jobs so that you dont have to input the same parameters everytime and the projects don’t share the same workspace because of Git reasons.

To make it more clear:

pipeline {
    agent {...}
    stages 
    {
        stage('Start') {
            steps {
                sh 'ls'
            }
        }
        stage ('Invoke_pipeline') {
            steps {
---------------> Decide Workspace and Branch for this pipeline call, ideally see the steps in the ui too
                build job: 'pipeline1', parameters: [
                string(name: 'param1', value: "value1")
                ]
            }
        }
    }
}

Is there a way to accomplish this?

Jenkins setup: [Jenkins 2.361.4]

I hope I understood things correct.
The above Jenkinsfile should run the job pipeline1 and this job should run on the same agent and in the same workspace that was used for executing the Jenkinsfile.

One thing to consider is that Jenkins doesn’t allow 2 jobs to use the same workspace at the same time on a node (except freestyle jobs that use a custom workspace but those are fixed and can’t be changed at runtime of the job afaik)

Might be possible but it is a bit tricky.
First you will need to pass the agent name and the workspace that is used to build above Jenkinsfile to pipeline1. In the Jenkinsfile of job pipeline1 you would need to use following construct:

node(agentNameparam) {
  lock(agentNameparam+workspaceParam) {
    ws(workspaceParam) {
      // do what needs to be done
    }
  }
}

You must avoid parallel execution of above Jenkinsfile and pipeline1 as the ws step will create workspaces with @2 appended (or higher numbers) when it is already in use. This means above pipeline can’t wait for the build of job pipeline1 to terminate as it would make the ws step allocate a @2 workspace.

One problem remains is that when pipeline1 runs and you run the above Jenkinsfile once more and it will run on the same agent it will for sure allocate an @2 workspace. You might need to use the same approach here with the steps lock and ws

This is just theory here, if this really works you would need to try out.

But is is potentially easier to do all this in the same pipeline script than splitting this in several jobs.

I see, well i decided to just go with the default workspace system now. I originally just wanted to execute a jenkinsfile from another jenkinsfile, but i don’t know if that works.

I put the jenkinsfile inside of a job now and am exeucting this job from another job. The only problem i’m facing now is that the process always takes up another executor of the agent, which isnt neccessary at all since there is nothing being ran in parallel. Is there a way to just have it happen on the same executor?