Question

I would like to set up some automation inside Jenkins that periodically polls the list of repos in our github organization and automatically sets up a jenkins job for each of that Git repos based on a job template.

What would be a possible solution to achieve this? Thanks!

Was it helpful?

Solution 2

Jenkins Pipeline is nowadays the way to go.

It defines pipelines using a Jenkinsfile, which you can check into your repos.

Which the best practice is a file like this

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building..'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
            }
        }
    }
}

As described in the documentation.

OTHER TIPS

You can use the Jenkins Job DSL plugin

which is a build step in jobs to create and modify other jobs

From the Wiki:

The job-dsl-plugin allows the programmatic creation of projects using a DSL. Pushing job creation into a script allows you to automate and standardize your Jenkins installation, unlike anything possible before.

An example would be:

def organization = 'jenkinsci'
repoApi = new URL("https://api.github.com/orgs/${organization}/repos")
repos = new groovy.json.JsonSlurper().parse(repoApi.newReader())
repos.each {
  def repoName = it.name
  job {
    name "${organization}-${repoName}".replaceAll('/','-')
    scm {
      git("git://github.com/${organization}/${repoName}.git", "master")
    }
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top