seedGogsProjects.groovy 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. def server = GOGS_SERVER
  2. def apikey = GOGS_API_KEY
  3. def api = "${server}/api/v1"
  4. def restExists(url) {
  5. def u = new URL(url)
  6. def req = u.openConnection();
  7. def getRC = req.getResponseCode();
  8. if(getRC.equals(200)) {
  9. return true;
  10. }
  11. return false;
  12. }
  13. def restJSONGet(url) {
  14. def u = new URL(url)
  15. return new groovy.json.JsonSlurper().parse(u.newReader())
  16. }
  17. def projects = restJSONGet("${api}/user/repos?token=${apikey}")
  18. projects.each {
  19. def project = it.full_name
  20. def repo = it.clone_url
  21. def branchName = "docker"
  22. def jenkinsFile = restExists("${api}/repos/${project}/raw/${branchName}/Jenkinsfile?token=${apikey}")
  23. def jobName = "${JOB_PREFIX}${project}".replaceAll('/','-')
  24. if (jenkinsFile) {
  25. pipelineJob(jobName) {
  26. triggers {
  27. scm('@hourly')
  28. }
  29. description("Pipeline for $repo")
  30. quietPeriod(5*60)
  31. environmentVariables(FOO: 'bar', TEST: '123')
  32. definition {
  33. cpsScm {
  34. scm {
  35. git {
  36. remote {
  37. url(repo)
  38. credentials(GOGS_CREDENTIALS)
  39. }
  40. branches('master', '**/feature*')
  41. scriptPath('Jenkinsfile')
  42. extensions { } // required as otherwise it may try to tag the repo, which you may not want
  43. }
  44. }
  45. }
  46. }
  47. wrappers {
  48. buildName('#${BUILD_NUMBER} on ${ENV,var="BRANCH"}')
  49. }
  50. }
  51. }
  52. }