0

I'm trying to automate the creation of a Jenkins Pipeline build from within a pipeline.

I have a pipeline which creates a Bitbucket repository and commits some code to it, including a Jenkinsfile.

I need to add another step to this pipeline to then create the Pipeline build for it, which would run the steps in the Jenkinsfile.

I think the Jobs DSL should be able to handle this but the documentation I've found for it has been very sparse, and I'm still not entirely sure if it's possible or how to do it.

Any help would be appreciated. The generated Pipeline build I would imagine just needs to have a link to the repository and be told to run the Jenkinsfile there?

James
  • 749
  • 1
  • 9
  • 22

2 Answers2

1

Yes, Job DSL is what you need for your use case.

See this and this to help you get started.

EDIT

pipeline {
agent {
        label 'slave'
    }
    stages{
        stage('stage'){
            steps {
                // some other steps

                jobDsl scriptText: '''pipelineJob(\'new-job\') {

                    def repo = \'https://xxxxx@bitbucket.org/xxxx/dummyrepo.git\'

                    triggers {
                        scm(\'H/5 * * * *\')
                    }

                    definition {
                        cpsScm {
                            scm {
                                git {
                                    remote { 
                                        url(repo) 
                                        credentials('bitbucket-jenkins-access')
                                    }
                                    branches(\'master\')
                                    scriptPath(\'Jenkinsfile\')
                                    extensions { } 
                                }
                            }
                        }
                    }
                }'''                    
            }           
        }
    }
}

Documentation - https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob-scm-git

ben5556
  • 2,915
  • 2
  • 11
  • 16
  • Thanks!. What I can't seem to gather from this post, or the documentation, is where do I put this definition? Does it go after the Stages in the Pipeline, or after the pipeline section entirely? My current pipeline file is a 'pipeline', which has some parameters, environment variables, and stages to create a repo, run an archetype, then commit and push to the created repo's. This Jobs DSL will then create Jenkins builds to run against the generated repo's - so is this just another "stage" in my pipeline, or a separate block entirely from the 'pipeline' block? Hope this makes sense! – James Nov 19 '18 at 10:44
  • See https://github.com/jenkinsci/job-dsl-plugin/wiki/User-Power-Moves#use-job-dsl-in-pipeline-scripts – ben5556 Nov 19 '18 at 10:53
  • Ok so I've read through all of the links you supplied and I'm still at a loss. There is a concept of a Job DSL written as a groovy file, but this is not the way to run one from a pipeline. There seems to be some syntax to write one in a pipeline using the pipelineJob tag, but it's unclear if this is compatible to be used WITHIN an existing Jenkinsfile that already has a pipeline tag defined, and if so, whether the pipelineJob tag goes within the pineline tag, or separately. The pipeline specific syntax also doesn't seem to ref any Jenkinsfile or repository to create. – James Nov 19 '18 at 14:07
  • 1
    See my edit above. This creates a new jenkins pipeline job 'new-job' . The new-job when triggered will checkout the repo and run the code in Jenkinsfile found in the master branch of the repo configured above. Hope this helps! Change it to suit your requirements. – ben5556 Nov 19 '18 at 21:40
  • 1
    Please note though that you need to approve the script as described here - https://jenkins.io/doc/book/managing/script-approval/#script-approval – ben5556 Nov 19 '18 at 21:51
  • Hey Ben, thanks so much for your help! Do you know how to parameterise this script, and then call it, with parameters, from a Jenkinsfile? – James Nov 22 '18 at 10:02
  • @James hey please mark it as answer if it helped! I am not sure I understood your question RE: parameterise. Are you able to post it as a new question/ post and put the link here so I can try to answer on that. – ben5556 Nov 22 '18 at 11:03
  • I am still yet to make it work, it was very helpful from you but still I don't believe the answer I'm looking for! Maybe it was because of the phrasing of my question. I'm now of the belief that Jobs DSL isn't the way forward. I just need to create a new Jenkins pipeline job from WITHIN a pipeline. Jobs DSL seems to require external script files which I would be unable to access. – James Nov 22 '18 at 11:49
  • 1
    Your original post asked for “I need to add another step to this pipeline to then create the Pipeline build for it, which would run the steps in the Jenkinsfile” which is what the script I gave does. If your requirement is no longer that then I’d suggest you create a new post with what you are looking for! – ben5556 Nov 22 '18 at 20:05
1

By using this python library jenins-job-builder you can easily create your expected pipeline or free-style job from another pipeline or from any other remote location.

Example:

steps-1

python3 -m venv .venv
source .venv/bin/activate
pip install --user jenkins-job-builder

steps-2

Once you have done above, Create 2 file, one with name config.ini and the other one is job.yml. Please note - there are no strict rules about the file name. It can be up to you.

The config.ini file contain can looks like

[job_builder]
allow_duplicates = False
keep_descriptions = False
ignore_cache = True
recursive = False
update = all
[jenkins]
password = jenkins-password
query_plugins_info = False
url = http://jenkins-url.net
user = jenkins-username

If you are creating a pipeline job , then your job.yml file can look like

- job:
      name: pipeline01
      display-name: 'pipeline01'
      description: 'Do not edit this job through the web!'
      project-type: pipeline
      dsl: |
        node(){
          stage('hello') {
            sh 'echo "Hellow World!"'
          }
        }

steps-3

after all the above. Invoke below command

jenkins-jobs --conf config.ini update job.yml

Note- jenkins-jobs command can only be available if you have followed steps-1

Samit Kumar Patel
  • 1,932
  • 1
  • 13
  • 20