-
-
Save consoledotblog/864714c6d4478cfd588c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//IMPORTANT NOTE!!!: Name this file "job-generation.dsl". Github's gist tool uses the file extension to establish syntax highlighting rules | |
@GrabResolver(name="repo.jenkins-ci.org",root='http://repo.jenkins-ci.org/public/') | |
@Grapes([ | |
@Grab(group='org.kohsuke', module='github-api', version='1.59') | |
]) | |
import static hudson.security.ACL.SYSTEM | |
import com.cloudbees.plugins.credentials.CredentialsProvider | |
import com.cloudbees.plugins.credentials.common.StandardCredentials | |
import hudson.Plugin | |
import hudson.util.VersionNumber | |
import jenkins.model.Jenkins | |
import org.kohsuke.github.GitHub | |
import org.kohsuke.github.GHOrganization | |
// NOTE: CHANGE THESE!!! | |
GITHUB_ORGANIZATION_NAME = 'YOUR_ORGANIZATIONS_NAME' | |
CRED_PLUGIN_GITHUB_API_KEY_ID = '2c02fd38-eccb-4f95-b885-2d1461e4a6c0' // This ID can be found by | |
// adding your credential to | |
// a job then examining your job's | |
// configuration XML for the | |
// 'credentialsId' element | |
// Retrieve credentials from the Credentials Plugin | |
def getCredentials(credentialsId) { | |
Jenkins jenkins = Jenkins.getInstance(); | |
Plugin credentialsPlugin = jenkins.getPlugin("credentials"); | |
if (credentialsPlugin != null && !credentialsPlugin.getWrapper().getVersionNumber().isOlderThan(new VersionNumber("1.6"))) { | |
for (CredentialsProvider credentialsProvider : jenkins.getExtensionList(CredentialsProvider.class)) { | |
for (StandardCredentials credentials : credentialsProvider.getCredentials(StandardCredentials.class, jenkins, SYSTEM)) { | |
if (credentials.getDescription().equals(credentialsId) || credentials.getId().equals(credentialsId)) { | |
return credentials.getSecret().toString(); | |
} | |
} | |
} | |
throw new IllegalArgumentException("Unable to find credential with ID: " + credentialsId) | |
} | |
} | |
// Get repo information from Github | |
def getRepos(credentials, organizationName) { | |
GitHub github = GitHub.connectUsingOAuth(credentials) | |
GHOrganization org = github.getOrganization(organizationName) | |
return org.getRepositories() | |
} | |
CREDENTIALS=getCredentials(CRED_PLUGIN_GITHUB_API_KEY_ID) | |
REPOS=getRepos(CREDENTIALS, GITHUB_ORGANIZATION_NAME) | |
// Iterate through the repo information and create a job for each repo | |
for (repo in REPOS) { | |
repoDetails = repo.getValue() | |
repoName = repoDetails.getName() | |
repoSshUrl = repoDetails.getSshUrl() | |
// Create the job | |
job { | |
name "${repoName}" | |
scm { | |
git { | |
remote { | |
url("${repoSshUrl}") | |
branch("master") | |
} | |
} | |
} | |
steps { | |
shell("echo ${repoName}") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment