[#solution-on-git-push]
= GitOps (run actions on Git Push)
image::gitops.png[]
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.
This guide assumes that you are using a self-hosted Git repository, and uses a standard Git `post-receive` hook to trigger OliveTin actions.
[TIP]
====
**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.
====
To set up OliveTin to run actions on Git push, you will need to:
1. Create a new OliveTin action that you want to run on push.
2. Set up a Git `post-receive` hook to trigger the OliveTin action.
== Create a New OliveTin Action
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:
[source,yaml]
.OliveTin `config.yaml`
----
actions:
- title: Run on Git Push
id: gitops
icon:
shell: |
echo "You just pushed commit $COMMIT to git, running action..."
date
arguments:
- name: commit
type: ascii
----
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.
== Add a Git hook script
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.
First, create a new file at `/opt/myrepo.git/hooks/post-receive` with the following contents:
[source,bash]
.Script: `myrepo.git/hooks/post-receive`
----
#!/bin/bash
read OLDREV NEWREV REFNAME
CHANGED_FILES=$(git diff --name-only $OLDREV $NEWREV)
commit_contains_path() {
local filename=$1
if echo "$CHANGED_FILES" | grep -q "$filename"; then
return 0 # True
else
return 1 # False
fi
}
function run_olivetin_action() {
local ACTION_NAME=$1
echo "Requesting OliveTin job $ACTION_NAME"
OLIVETIN_REQUEST="$(cat <