Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load dynamic Jenkinsfile during a build

I'm trying to create a Jenkins pipeline job that uses different Jenkinsbuild files depending on the job parameter. Is there a way to load the Jenkins build file during the execution of the job, something like:

node {
   stage("Determine build parameter") {
      String jenkinsFile = .....
   }
   // here the Jenkins build file should be loaded
   loadSomeHowBuildFile jenkinsFile

   // ... and then the pipeline steps defined in the jenkinsFile are executed 
}

It would be really great is this will work...

like image 993
Niko Avatar asked Aug 16 '17 10:08

Niko


People also ask

Why Jenkinsfile changes are not reflected in PR build?

This is due to the fact the author of the Pull Request is not trusted. In that case Jenkins falls back to the Jenkinsfile of the base branch. Changing the Jenkinsfile is equivalent to changing the job configuration. Only users with a certain level of trust should be allowed to do it.

Can Jenkins build be triggered automatically?

Follow the steps as mentioned below to trigger a Jenkins job automatically based on GitHub's webhook configurations: Step 1: Go to the Configuration page of the respective job and under the build trigger section, check the "GitHub hook trigger for GITScm polling" checkbox and click on the Save button.


1 Answers

I found the solution which is quite simple. Jenkins only needs to load the file

node {
   stage("Determine build file") {
      String jenkinsFile = /path/to/Jenkins/build/file
   }
   // Here the Jenkins build file is loaded and executed
   load jenkinsFile
}
like image 132
Niko Avatar answered Oct 19 '22 02:10

Niko