Create a Build Pipeline View based on Pipeline job using Job DSL plugin in Jenkins

1.6k views Asked by At

Since there's a limitation in Jenkins Pipeline's that you cannot add a manual build step without hanging the build (see for example this stackoverflow question) I'm experimenting with a combination of Jenkins Pipeline and Build Pipeline Plugin using the Job DSL plugin.

My plan was to create a Job DSL script that first executes the the Jenkins Pipeline (defined in a Jenkinsfile) and then create a downstream job that deploys to production (this is the manual step). I've created this Job DSL script as a test:

pipelineJob("${REPO_NAME} jobs") {
  logRotator(-1, 10)    
  def repo = "https://path-to-repo/${REPO_NAME}.git"

  triggers {
    scm('* * * * *')
  }
  description("Pipeline for $repo")

  definition {
    cpsScm {
      scm {
        git {
          remote { url(repo) }
          branches('master')
          scriptPath('Jenkinsfile')
          extensions { }  // required as otherwise it may try to tag the repo, which you may not want
        }
      }
    }
  }

  publishers {
     buildPipelineTrigger("${REPO_NAME} deploy to prod") {
        parameters {
            currentBuild()
        }
     }
 }
}

freeStyleJob("${REPO_NAME} deploy to prod") {   
}

buildPipelineView("$REPO_NAME Build Pipeline") {
   selectedJob("${REPO_NAME} jobs")
}

where REPO_NAME is defined as an environment variable. The Jenkinsfile looks like this:

node {
    stage('build'){
        echo "building"
    }

    stage('run tests'){
        echo "running tests"
    }
    stage('package Docker'){
        echo "packaging"
    }
    stage('Deploy to Test'){
        echo "Deploying to Test"
    }
}

The problem is that the selectedJob points to "${REPO_NAME} jobs" which doesn't seem to be a valid option as "Initial Job" in the Build Pipeline Plugin view (you can't select it manually either).

Is there a workaround for this? I.e. how can I use a Jenkins Pipeline as the "Initial Job" for the Build Pipeline Plugin?

1

There are 1 answers

0
Seun On

From the documentation on yourDomain.com/plugin/job-dsl/api-viewer/index.html#method/javaposse.jobdsl.dsl.views.NestedViewsContext.envDashboardView

It shows that buildPipelineView can only be used within a View block which is inside of a Folder block.

Folder {
  View {
    buildPipelineView {
    }
  }
}