executable_jenkins 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env bash
  2. set -e
  3. if [[ $PWD == $HOME/src/gitlab.intr/* ]]
  4. then
  5. JENKINS_URL="https://jenkins.intr"
  6. JENKINS_USER="admin"
  7. JENKINS_PASSWORD="$(pass majordomo/public/jenkins.intr/admin)"
  8. else
  9. JENKINS_URL="${JENKINS_URL:-https://jenkins.wugi.info}"
  10. JENKINS_USER="${JENKINS_USER:-admin}"
  11. JENKINS_PASSWORD="${JENKINS_PASSWORD:-$(pass show jenkins.wugi.info/token-admin)}"
  12. fi
  13. help_main()
  14. {
  15. echo "\
  16. Usage: connect COMMANDS ARGS...
  17. Run COMMANDS with ARGS
  18. COMMAND must be one of the sub-commands listed below:
  19. build
  20. stop
  21. Report bugs to: go.wigust@gmail.com."
  22. }
  23. case "$1" in
  24. --help | -h)
  25. help_main
  26. exit 0
  27. ;;
  28. log)
  29. curl --silent "$JENKINS_URL"/job/"$(basename "$(dirname "$PWD")")"/job/"$(basename "$PWD")"/job/"$(git branch --show-current)"/lastBuild/consoleText
  30. ;;
  31. console)
  32. project="$(basename "$PWD")"
  33. group="$(basename "$(dirname "$PWD")")"
  34. "$BROWSER" "${JENKINS_URL}/job/${group}/job/${group}%252F${project}/job/$(git branch --show-current)/lastBuild/console"
  35. ;;
  36. build)
  37. curl --request POST --user "$JENKINS_USER:$JENKINS_PASSWORD" "$JENKINS_URL/job/$2/job/$3/build"
  38. ;;
  39. stop)
  40. data="$(cat << 'EOF'
  41. // https://jenkins.intr/script
  42. import java.util.ArrayList
  43. import hudson.model.*;
  44. import jenkins.model.Jenkins
  45. // Remove everything which is currently queued
  46. def q = Jenkins.instance.queue
  47. for (queued in Jenkins.instance.queue.items) {
  48. q.cancel(queued.task)
  49. }
  50. // stop all the currently running jobs
  51. for (job in Jenkins.instance.items) {
  52. stopJobs(job)
  53. }
  54. def stopJobs(job) {
  55. if (job in com.cloudbees.hudson.plugins.folder.Folder) {
  56. for (child in job.items) {
  57. stopJobs(child)
  58. }
  59. } else if (job in org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) {
  60. for (child in job.items) {
  61. stopJobs(child)
  62. }
  63. } else if (job in org.jenkinsci.plugins.workflow.job.WorkflowJob) {
  64. if (job.isBuilding()) {
  65. for (build in job.builds) {
  66. build.doKill()
  67. }
  68. }
  69. }
  70. }
  71. EOF
  72. )"
  73. curl --user "$JENKINS_USER:$JENKINS_PASSWORD" "$JENKINS_URL/scriptText" --data-urlencode "script=$data"
  74. ;;
  75. *)
  76. help_main
  77. exit 0
  78. ;;
  79. esac
  80. # curl --request POST --user "$JENKINS_USER:$JENKINS_PASSWORD" \
  81. # --data-urlencode json='{"parameter": [{"name":"CHEZMOI_APPLY", "value":"true"},{"name":"INVOKE_GUIX_PULL", "value":"true"}]}' \
  82. # https://jenkins.wugi.info/job/wigust/job/dotfiles/job/master/build