No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

automate-it.sh 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env bash
  2. set -ex
  3. # set -o pipefail
  4. ####################
  5. # Variables: #
  6. # REPO_NAME #
  7. # REPO_DESCRIPTION #
  8. # CLONE_ADDRESS #
  9. # DRONE_TOKEN #
  10. # DRONE_HOST #
  11. # XMPP_ROOM #
  12. # XMPP_USER #
  13. # XMPP_PASSWORD #
  14. # CLONE_ADDRESS #
  15. # GITEA_URL #
  16. # GITEA_TOKEN #
  17. # DOCKER_PASSWORD #
  18. # DOCKER_HOST #
  19. # DOCKER_USERNAME #
  20. ####################
  21. GITEA_URL="${GITEA_URL:-https://git.daemons.it}"
  22. DRONE_CLI_VERSION="0.8-alpine"
  23. function create_normal_repository {
  24. echo "Creating drymer/$REPO_NAME repository..."
  25. curl --silent -X POST "$GITEA_URL/api/v1/user/repos" \
  26. -H "accept: application/json" \
  27. -H "Content-Type: application/json" \
  28. -H "Authorization: token $GITEA_TOKEN" \
  29. -d "{\"auto_init\": true, \"license\": \"GPL-3.0-only\", \"readme\": \"Default\", \"name\": \"$REPO_NAME\", \"description\": \"$REPO_DESCRIPTION\"}"
  30. }
  31. function create_mirror_repository {
  32. echo "Creating drymer/$REPO_NAME mirror..."
  33. curl --silent -X POST "$GITEA_URL/api/v1/repos/migrate" \
  34. -H "accept: application/json" \
  35. -H "Content-Type: application/json" \
  36. -H "Authorization: token $GITEA_TOKEN" \
  37. -d "{\"clone_addr\": \"$CLONE_ADDRESS\", \"description\": \"$REPO_DESCRIPTION\", \"mirror\": true, \"repo_name\": \"$REPO_NAME\", \"uid\": 1}"
  38. }
  39. function drone_set_xmpp_secrets {
  40. echo "Creating drymer/$REPO_NAME XMPP credentials..."
  41. docker run -i \
  42. -e DRONE_SERVER=$DRONE_SERVER \
  43. -e DRONE_TOKEN=$DRONE_TOKEN \
  44. drone/cli:$DRONE_CLI_VERSION secret add \
  45. --repository "drymer/$REPO_NAME" \
  46. --name xmpp_room \
  47. --value $XMPP_ROOM \
  48. --event push \
  49. --event pull_request
  50. sleep 1
  51. docker run -i \
  52. -e DRONE_SERVER=$DRONE_SERVER \
  53. -e DRONE_TOKEN=$DRONE_TOKEN \
  54. drone/cli:$DRONE_CLI_VERSION secret add \
  55. --repository "drymer/$REPO_NAME" \
  56. --name xmpp_password \
  57. --value $XMPP_PASSWORD \
  58. --event push \
  59. --event pull_request
  60. docker run -i \
  61. -e DRONE_SERVER=$DRONE_SERVER \
  62. -e DRONE_TOKEN=$DRONE_TOKEN \
  63. drone/cli:$DRONE_CLI_VERSION secret add \
  64. --repository "drymer/$REPO_NAME" \
  65. --name xmpp_user \
  66. --value $XMPP_USER \
  67. --event push \
  68. --event pull_request
  69. }
  70. function drone_set_registry_secrets {
  71. echo "Creating drymer/$REPO_NAME docker credentials..."
  72. docker run -i \
  73. -e DRONE_SERVER=$DRONE_SERVER \
  74. -e DRONE_TOKEN=$DRONE_TOKEN \
  75. drone/cli:$DRONE_CLI_VERSION repo update \
  76. --trusted "drymer/$REPO_NAME"
  77. docker run -i \
  78. -e DRONE_SERVER=$DRONE_SERVER \
  79. -e DRONE_TOKEN=$DRONE_TOKEN \
  80. drone/cli:$DRONE_CLI_VERSION secret add \
  81. --repository "drymer/$REPO_NAME" \
  82. --name docker_user \
  83. --value $DOCKER_USERNAME
  84. docker run -i \
  85. -e DRONE_SERVER=$DRONE_SERVER \
  86. -e DRONE_TOKEN=$DRONE_TOKEN \
  87. drone/cli:$DRONE_CLI_VERSION secret add \
  88. --repository "drymer/$REPO_NAME" \
  89. --name docker_password \
  90. --value $DOCKER_PASSWORD
  91. docker run -i \
  92. -e DRONE_SERVER=$DRONE_SERVER \
  93. -e DRONE_TOKEN=$DRONE_TOKEN \
  94. drone/cli:$DRONE_CLI_VERSION secret add \
  95. --repository "drymer/$REPO_NAME" \
  96. --name docker_registry \
  97. --value $DOCKER_REGISTRY
  98. }
  99. function drone_set_main {
  100. echo "Syncing drone for drymer/$REPO_NAME..."
  101. docker run -i \
  102. -e DRONE_SERVER=$DRONE_SERVER \
  103. -e DRONE_TOKEN=$DRONE_TOKEN \
  104. drone/cli:$DRONE_CLI_VERSION repo sync > /dev/null
  105. echo "Creating drymer/$REPO_NAME drone pipeline..."
  106. docker run -i \
  107. -e DRONE_SERVER=$DRONE_SERVER \
  108. -e DRONE_TOKEN=$DRONE_TOKEN \
  109. drone/cli:$DRONE_CLI_VERSION repo add "drymer/$REPO_NAME"
  110. }
  111. NEW_FILES="$(git diff-tree --no-commit-id --name-only -r HEAD | grep projects)"
  112. for file in $NEW_FILES
  113. do
  114. # shellcheck disable=SC1090
  115. . "$file"
  116. if [[ -n $MIRROR ]]
  117. then
  118. CLONE_ADDRESS=$MIRROR
  119. create_mirror_repository
  120. else
  121. create_normal_repository
  122. fi
  123. if [[ -n $DRONE || -n $DOCKER || -n $XMPP ]]
  124. then
  125. drone_set_main
  126. fi
  127. if [[ -n $XMPP ]]
  128. then
  129. drone_set_xmpp_secrets
  130. fi
  131. if [[ -n $DOCKER ]]
  132. then
  133. drone_set_registry_secrets
  134. fi
  135. unset REPO_NAME
  136. unset REPO_DESCRIPTION
  137. unset DOCKER
  138. unset MIRROR
  139. done
  140. set +ex