Creating a Jenkins Pipeline build from a pipeline











up vote
0
down vote

favorite












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


























    up vote
    0
    down vote

    favorite












    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
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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 19 hours ago









      James

      2811312




      2811312
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          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
            19 hours ago










          • See github.com/jenkinsci/job-dsl-plugin/wiki/…
            – ben5556
            18 hours ago










          • 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
            15 hours ago










          • 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
            8 hours ago












          • Please note though that you need to approve the script as described here - jenkins.io/doc/book/managing/script-approval/#script-approval
            – ben5556
            7 hours ago











          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',
          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








          up vote
          1
          down vote













          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
            19 hours ago










          • See github.com/jenkinsci/job-dsl-plugin/wiki/…
            – ben5556
            18 hours ago










          • 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
            15 hours ago










          • 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
            8 hours ago












          • Please note though that you need to approve the script as described here - jenkins.io/doc/book/managing/script-approval/#script-approval
            – ben5556
            7 hours ago















          up vote
          1
          down vote













          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
            19 hours ago










          • See github.com/jenkinsci/job-dsl-plugin/wiki/…
            – ben5556
            18 hours ago










          • 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
            15 hours ago










          • 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
            8 hours ago












          • Please note though that you need to approve the script as described here - jenkins.io/doc/book/managing/script-approval/#script-approval
            – ben5556
            7 hours ago













          up vote
          1
          down vote










          up vote
          1
          down vote









          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 8 hours ago

























          answered 19 hours ago









          ben5556

          1,265138




          1,265138












          • 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
            19 hours ago










          • See github.com/jenkinsci/job-dsl-plugin/wiki/…
            – ben5556
            18 hours ago










          • 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
            15 hours ago










          • 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
            8 hours ago












          • Please note though that you need to approve the script as described here - jenkins.io/doc/book/managing/script-approval/#script-approval
            – ben5556
            7 hours ago


















          • 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
            19 hours ago










          • See github.com/jenkinsci/job-dsl-plugin/wiki/…
            – ben5556
            18 hours ago










          • 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
            15 hours ago










          • 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
            8 hours ago












          • Please note though that you need to approve the script as described here - jenkins.io/doc/book/managing/script-approval/#script-approval
            – ben5556
            7 hours ago
















          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
          19 hours ago




          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
          19 hours ago












          See github.com/jenkinsci/job-dsl-plugin/wiki/…
          – ben5556
          18 hours ago




          See github.com/jenkinsci/job-dsl-plugin/wiki/…
          – ben5556
          18 hours ago












          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
          15 hours ago




          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
          15 hours ago












          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
          8 hours ago






          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
          8 hours ago














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




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


















           

          draft saved


          draft discarded



















































           


          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







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules