Creating a Jenkins Pipeline build from a pipeline












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?










share|improve this question



























    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?










    share|improve this question

























      0












      0








      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?










      share|improve this question














      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?







      jenkins jenkins-pipeline






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 9:53









      JamesJames

      2881312




      2881312
























          1 Answer
          1






          active

          oldest

          votes


















          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






          share|improve this answer


























          • 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 github.com/jenkinsci/job-dsl-plugin/wiki/…

            – ben5556
            Nov 19 '18 at 10:53






          • 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 - jenkins.io/doc/book/managing/script-approval/#script-approval

            – ben5556
            Nov 19 '18 at 21:51






          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372072%2fcreating-a-jenkins-pipeline-build-from-a-pipeline%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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






          share|improve this answer


























          • 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 github.com/jenkinsci/job-dsl-plugin/wiki/…

            – ben5556
            Nov 19 '18 at 10:53






          • 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 - jenkins.io/doc/book/managing/script-approval/#script-approval

            – ben5556
            Nov 19 '18 at 21:51






          • 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














          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






          share|improve this answer


























          • 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 github.com/jenkinsci/job-dsl-plugin/wiki/…

            – ben5556
            Nov 19 '18 at 10:53






          • 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 - jenkins.io/doc/book/managing/script-approval/#script-approval

            – ben5556
            Nov 19 '18 at 21:51






          • 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












          1








          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






          share|improve this answer















          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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 21:37

























          answered Nov 19 '18 at 10:28









          ben5556ben5556

          1,9522310




          1,9522310













          • 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 github.com/jenkinsci/job-dsl-plugin/wiki/…

            – ben5556
            Nov 19 '18 at 10:53






          • 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 - jenkins.io/doc/book/managing/script-approval/#script-approval

            – ben5556
            Nov 19 '18 at 21:51






          • 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



















          • 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 github.com/jenkinsci/job-dsl-plugin/wiki/…

            – ben5556
            Nov 19 '18 at 10:53






          • 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 - jenkins.io/doc/book/managing/script-approval/#script-approval

            – ben5556
            Nov 19 '18 at 21:51






          • 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

















          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





          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 github.com/jenkinsci/job-dsl-plugin/wiki/…

          – ben5556
          Nov 19 '18 at 10:53





          See github.com/jenkinsci/job-dsl-plugin/wiki/…

          – ben5556
          Nov 19 '18 at 10:53




          1




          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







          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




          1





          Please note though that you need to approve the script as described here - jenkins.io/doc/book/managing/script-approval/#script-approval

          – ben5556
          Nov 19 '18 at 21:51





          Please note though that you need to approve the script as described here - jenkins.io/doc/book/managing/script-approval/#script-approval

          – ben5556
          Nov 19 '18 at 21:51




          1




          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





          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




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372072%2fcreating-a-jenkins-pipeline-build-from-a-pipeline%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Academy of Television Arts & Sciences

          L'Équipe

          1995 France bombings