onwebhook_github.adoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. [#exec-webhook-github]
  2. = GitHub Webhooks
  3. OliveTin includes built-in templates for GitHub webhooks that simplify configuration. Instead of manually configuring header matching and argument extraction, you can use a template that handles all the common settings for you.
  4. == Supported GitHub Templates
  5. OliveTin supports the following GitHub webhook templates:
  6. * `github-push` - Triggered on push events
  7. * `github-pr` or `github-pull-request` - Triggered on pull request events
  8. * `github-release` - Triggered on release events
  9. * `github-workflow` - Triggered on workflow run events
  10. == Setting Up GitHub Webhooks
  11. === 1. Configure the Webhook in GitHub
  12. 1. Go to your GitHub repository → **Settings** → **Webhooks** → **Add webhook**
  13. 2. Set the **Payload URL** to `http://your-olivetin-server:1337/webhooks`
  14. 3. Set **Content type** to `application/json`
  15. 4. Enter a **Secret** (you'll use this in your OliveTin config)
  16. 5. Choose which events to trigger the webhook:
  17. - Select **Just the push event** for push triggers
  18. - Or select **Let me select individual events** for more control
  19. 6. Click **Add webhook**
  20. === 2. Configure OliveTin
  21. Use the `template` property to apply GitHub-specific settings:
  22. [source,yaml]
  23. .`config.yaml`
  24. ----
  25. actions:
  26. - title: Deploy on Push
  27. shell: |
  28. echo "Deploying commit {{ git_commit }} to {{ git_branch }}"
  29. /opt/scripts/deploy.sh "{{ git_branch }}"
  30. arguments:
  31. - name: git_commit
  32. type: ascii
  33. - name: git_branch
  34. type: ascii
  35. execOnWebhook:
  36. - template: github-push
  37. secret: your-github-webhook-secret
  38. ----
  39. == GitHub Push Template
  40. The `github-push` template is designed for push events. It automatically:
  41. * Sets authentication to HMAC-SHA256 with the `X-Hub-Signature-256` header
  42. * Matches the `X-GitHub-Event: push` header
  43. * Extracts common push event data
  44. === Extracted Arguments
  45. The following arguments are automatically extracted and available in your action:
  46. [cols="1,2,2"]
  47. |===
  48. |Argument Name |Description |JSONPath
  49. |`git_repository`
  50. |Full repository name (owner/repo)
  51. |`$.repository.full_name`
  52. |`git_ref`
  53. |Full git reference (e.g., refs/heads/main)
  54. |`$.ref`
  55. |`git_commit`
  56. |The HEAD commit SHA
  57. |`$.head_commit.id`
  58. |`git_branch`
  59. |The branch reference
  60. |`$.ref`
  61. |`git_message`
  62. |The commit message
  63. |`$.head_commit.message`
  64. |`git_author`
  65. |The commit author's name
  66. |`$.head_commit.author.name`
  67. |===
  68. === Example: Deploy on Push to Main
  69. [source,yaml]
  70. ----
  71. actions:
  72. - title: Deploy to Production
  73. shell: |
  74. if [ "{{ git_ref }}" = "refs/heads/main" ]; then
  75. echo "Deploying {{ git_commit }} by {{ git_author }}"
  76. /opt/scripts/deploy.sh production
  77. else
  78. echo "Ignoring push to non-main branch"
  79. fi
  80. arguments:
  81. - name: git_ref
  82. type: ascii
  83. - name: git_commit
  84. type: ascii
  85. - name: git_author
  86. type: ascii
  87. execOnWebhook:
  88. - template: github-push
  89. secret: your-secret
  90. ----
  91. === Example: Filter by Branch
  92. To only trigger on specific branches, add a `matchPath` condition:
  93. [source,yaml]
  94. ----
  95. actions:
  96. - title: Deploy Staging
  97. shell: /opt/scripts/deploy.sh staging
  98. execOnWebhook:
  99. - template: github-push
  100. secret: your-secret
  101. matchPath: '$.ref="refs/heads/develop"'
  102. ----
  103. == GitHub Pull Request Template
  104. The `github-pr` (or `github-pull-request`) template handles pull request events.
  105. === Extracted Arguments
  106. [cols="1,2,2"]
  107. |===
  108. |Argument Name |Description |JSONPath
  109. |`pr_number`
  110. |Pull request number
  111. |`$.number`
  112. |`pr_title`
  113. |Pull request title
  114. |`$.pull_request.title`
  115. |`pr_author`
  116. |PR author's username
  117. |`$.pull_request.user.login`
  118. |`pr_action`
  119. |Event action (opened, closed, synchronize, etc.)
  120. |`$.action`
  121. |`git_repository`
  122. |Full repository name
  123. |`$.repository.full_name`
  124. |`pr_state`
  125. |PR state (open, closed)
  126. |`$.pull_request.state`
  127. |`pr_head_sha`
  128. |HEAD commit SHA of the PR branch
  129. |`$.pull_request.head.sha`
  130. |===
  131. === Example: Run Tests on PR
  132. [source,yaml]
  133. ----
  134. actions:
  135. - title: Run PR Tests
  136. shell: |
  137. echo "Running tests for PR #{{ pr_number }}: {{ pr_title }}"
  138. echo "Action: {{ pr_action }}, Author: {{ pr_author }}"
  139. /opt/scripts/run-tests.sh "{{ pr_head_sha }}"
  140. arguments:
  141. - name: pr_number
  142. type: ascii
  143. - name: pr_title
  144. type: ascii
  145. - name: pr_action
  146. type: ascii
  147. - name: pr_author
  148. type: ascii
  149. - name: pr_head_sha
  150. type: ascii
  151. execOnWebhook:
  152. - template: github-pr
  153. secret: your-secret
  154. ----
  155. === Example: Only on PR Open or Synchronize
  156. [source,yaml]
  157. ----
  158. actions:
  159. - title: CI Build
  160. shell: /opt/scripts/ci-build.sh "{{ pr_head_sha }}"
  161. arguments:
  162. - name: pr_head_sha
  163. type: ascii
  164. execOnWebhook:
  165. - template: github-pr
  166. secret: your-secret
  167. matchPath: '$.action="opened"'
  168. - template: github-pr
  169. secret: your-secret
  170. matchPath: '$.action="synchronize"'
  171. ----
  172. == GitHub Release Template
  173. The `github-release` template handles release events.
  174. === Extracted Arguments
  175. [cols="1,2,2"]
  176. |===
  177. |Argument Name |Description |JSONPath
  178. |`release_action`
  179. |Release action (published, created, etc.)
  180. |`$.action`
  181. |`release_tag`
  182. |Release tag name
  183. |`$.release.tag_name`
  184. |`release_name`
  185. |Release name/title
  186. |`$.release.name`
  187. |`git_repository`
  188. |Full repository name
  189. |`$.repository.full_name`
  190. |`release_author`
  191. |Release author's username
  192. |`$.release.author.login`
  193. |===
  194. === Example: Deploy on Release
  195. [source,yaml]
  196. ----
  197. actions:
  198. - title: Deploy Release
  199. shell: |
  200. echo "Deploying release {{ release_tag }}: {{ release_name }}"
  201. /opt/scripts/deploy-release.sh "{{ release_tag }}"
  202. arguments:
  203. - name: release_tag
  204. type: ascii
  205. - name: release_name
  206. type: ascii
  207. execOnWebhook:
  208. - template: github-release
  209. secret: your-secret
  210. matchPath: '$.action="published"'
  211. ----
  212. == GitHub Workflow Template
  213. The `github-workflow` template handles workflow run events, useful for triggering actions when GitHub Actions workflows complete.
  214. === Extracted Arguments
  215. [cols="1,2,2"]
  216. |===
  217. |Argument Name |Description |JSONPath
  218. |`workflow_name`
  219. |Name of the workflow
  220. |`$.workflow_run.name`
  221. |`workflow_status`
  222. |Workflow status
  223. |`$.workflow_run.status`
  224. |`workflow_conclusion`
  225. |Workflow conclusion (success, failure, etc.)
  226. |`$.workflow_run.conclusion`
  227. |`git_repository`
  228. |Full repository name
  229. |`$.repository.full_name`
  230. |`git_commit`
  231. |HEAD commit SHA
  232. |`$.workflow_run.head_sha`
  233. |`git_branch`
  234. |Branch that triggered the workflow
  235. |`$.workflow_run.head_branch`
  236. |===
  237. === Example: Deploy After CI Success
  238. [source,yaml]
  239. ----
  240. actions:
  241. - title: Deploy After CI
  242. shell: |
  243. echo "CI workflow '{{ workflow_name }}' completed with {{ workflow_conclusion }}"
  244. if [ "{{ workflow_conclusion }}" = "success" ]; then
  245. /opt/scripts/deploy.sh "{{ git_branch }}" "{{ git_commit }}"
  246. fi
  247. arguments:
  248. - name: workflow_name
  249. type: ascii
  250. - name: workflow_conclusion
  251. type: ascii
  252. - name: git_branch
  253. type: ascii
  254. - name: git_commit
  255. type: ascii
  256. execOnWebhook:
  257. - template: github-workflow
  258. secret: your-secret
  259. matchPath: '$.action="completed"'
  260. ----
  261. == Customizing Templates
  262. Templates provide default values, but you can override or extend them:
  263. [source,yaml]
  264. ----
  265. actions:
  266. - title: Custom Push Handler
  267. shell: echo "Push from {{ custom_field }}"
  268. arguments:
  269. - name: custom_field
  270. type: ascii
  271. - name: git_commit
  272. type: ascii
  273. execOnWebhook:
  274. - template: github-push
  275. secret: your-secret
  276. # Add additional extractions
  277. extract:
  278. custom_field: "$.sender.login"
  279. # Add additional match criteria
  280. matchPath: '$.repository.private=false'
  281. ----
  282. Custom `extract` values are merged with template defaults, so you can add extra fields without losing the standard ones.
  283. == Security Considerations
  284. 1. **Always use a secret** - Without a secret, anyone can trigger your webhooks
  285. 2. **Use HTTPS** - When exposing OliveTin to the internet, use a reverse proxy with TLS
  286. 3. **Limit webhook events** - Only subscribe to the events you actually need in GitHub
  287. 4. **Validate in your scripts** - Add additional validation in your shell scripts for sensitive operations
  288. == Troubleshooting
  289. === Webhook Not Triggering
  290. 1. Check the OliveTin logs with `logLevel: DEBUG` for webhook processing details
  291. 2. Verify the webhook is being received (check GitHub webhook delivery history)
  292. 3. Ensure the secret matches exactly between GitHub and OliveTin config
  293. 4. Verify the `template` name is spelled correctly
  294. === Signature Verification Failed
  295. 1. Ensure the secret in OliveTin matches the one configured in GitHub
  296. 2. Check that you're using the correct template (GitHub uses HMAC-SHA256 by default)
  297. 3. Make sure the webhook Content-Type is set to `application/json`
  298. === Arguments Not Extracted
  299. 1. Verify the argument names match exactly (case-sensitive)
  300. 2. Check that arguments are defined in the action's `arguments` list
  301. 3. Use `logLevel: DEBUG` to see extracted values in the logs
  302. == See Also
  303. * xref:action_execution/onwebhook.adoc[Webhooks Overview] - General webhook configuration
  304. * xref:solutions/on-git-push/index.adoc[GitOps Solution] - Alternative approach using Git hooks
  305. * https://docs.github.com/en/webhooks[GitHub Webhooks Documentation^] - Official GitHub webhook documentation