index.adoc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. [#solution-on-git-push]
  2. = GitOps (run actions on Git Push)
  3. image::gitops.png[]
  4. A really helpful thing to do with OliveTin is to have it run actions when you push to a Git repository. This is a great way to automate things like running tests, building your project, or deploying your code - this turns OliveTin into a powerful GitOps tool, or even a Continuous Integration tool.
  5. This guide assumes that you are using a self-hosted Git repository, and uses a standard Git `post-receive` hook to trigger OliveTin actions.
  6. [TIP]
  7. ====
  8. **Using GitHub?** OliveTin has built-in support for GitHub webhooks with templates that make configuration simple. See xref:action_execution/onwebhook_github.adoc[GitHub Webhooks] for an easier approach that doesn't require writing hook scripts.
  9. ====
  10. To set up OliveTin to run actions on Git push, you will need to:
  11. 1. Create a new OliveTin action that you want to run on push.
  12. 2. Set up a Git `post-receive` hook to trigger the OliveTin action.
  13. == Create a New OliveTin Action
  14. First, you will need to create a new OliveTin action that you want to run when you push to your Git repository. This could be anything you like - for example, running tests, building your project, or deploying your code. The example below is a simple action that echoes a message to the console:
  15. [source,yaml]
  16. .OliveTin `config.yaml`
  17. ----
  18. actions:
  19. - title: Run on Git Push
  20. id: gitops
  21. icon: <iconify-icon icon="bi:git"></iconify-icon>
  22. shell: |
  23. echo "You just pushed commit $COMMIT to git, running action..."
  24. date
  25. arguments:
  26. - name: commit
  27. type: ascii
  28. ----
  29. Note that OliveTin will expose all arguments as environment variables in uppercase as shown in the example above. You can of course use the `{{ commit }}` syntax instead and it will do the same thing.
  30. == Add a Git hook script
  31. The following below assumes that you have a Git repository initialized as a bare repository, at `/opt/myrepo.git`. If you have a different repository location, you will need to adjust the paths accordingly.
  32. First, create a new file at `/opt/myrepo.git/hooks/post-receive` with the following contents:
  33. [source,bash]
  34. .Script: `myrepo.git/hooks/post-receive`
  35. ----
  36. #!/bin/bash
  37. read OLDREV NEWREV REFNAME
  38. CHANGED_FILES=$(git diff --name-only $OLDREV $NEWREV)
  39. commit_contains_path() {
  40. local filename=$1
  41. if echo "$CHANGED_FILES" | grep -q "$filename"; then
  42. return 0 # True
  43. else
  44. return 1 # False
  45. fi
  46. }
  47. function run_olivetin_action() {
  48. local ACTION_NAME=$1
  49. echo "Requesting OliveTin job $ACTION_NAME"
  50. OLIVETIN_REQUEST="$(cat <<EOF
  51. {
  52. "actionId": "$ACTION_NAME",
  53. "arguments": [
  54. {
  55. "name": "commit",
  56. "value": "$NEWREV"
  57. }
  58. ]
  59. }
  60. EOF
  61. )"
  62. exec curl -sS --json "$OLIVETIN_REQUEST" http://olivetin.example.com:1337/api/StartAction
  63. }
  64. if commit_contains_path "k8s-flux"; then
  65. run_olivetin_action "ServerConfiguration_Flux"
  66. exit
  67. fi
  68. if commit_contains_path "alert.rules"; then
  69. run_olivetin_action "ServerConfiguration_Prom"
  70. exit
  71. fi
  72. run_olivetin_action "gitops"
  73. ----
  74. Make sure you make the script executable, and edit the last line of the script to point to your OliveTin instance.
  75. [source,bash]
  76. ----
  77. chmod +x /opt/myrepo.git/hooks/post-receive
  78. ----
  79. Now, whenever you push to your Git repository, the `post-receive` hook will trigger the OliveTin action you created. You can see if the action was triggered by pushing to the git repository, and looking for output like this;
  80. ----
  81. remote: {"executionTrackingId":"55c7371d-7a67-42a4-87d8-d5677b878186"}
  82. ----
  83. This is the `executionTrackingId` of the action that was triggered. You can use this to track the progress of the action in the OliveTin UI.