1
0

seedGogsProjects.groovy 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 jenkinsFile = restExists("${api}/repos/${project}/raw/master/Jenkinsfile?token=${apikey}")
  22. def jobName = "${JOB_PREFIX}${project}".replaceAll('/','-')
  23. if (jenkinsFile) {
  24. pipelineJob(jobName) {
  25. triggers {
  26. scm('@hourly')
  27. }
  28. description("Pipeline for $repo")
  29. quietPeriod(5*60)
  30. environmentVariables(FOO: 'bar', TEST: '123')
  31. definition {
  32. cpsScm {
  33. scm {
  34. git {
  35. remote {
  36. url(repo)
  37. credentials(GOGS_CREDENTIALS)
  38. }
  39. branches('master', '**/feature*')
  40. scriptPath('Jenkinsfile')
  41. extensions { } // required as otherwise it may try to tag the repo, which you may not want
  42. }
  43. }
  44. }
  45. }
  46. wrappers {
  47. buildName('#${BUILD_NUMBER} on ${ENV,var="BRANCH"}')
  48. }
  49. }
  50. }
  51. }