Skip to content

Instantly share code, notes, and snippets.

@joaquinco
Last active April 14, 2023 15:05
Show Gist options
  • Save joaquinco/c512adaf37222e7d0671dddc3c639c33 to your computer and use it in GitHub Desktop.
Save joaquinco/c512adaf37222e7d0671dddc3c639c33 to your computer and use it in GitHub Desktop.
Run remote scripts through git and push the result back
#!/bin/bash
: "
Executes what's on a script, put its stdout and stderr
in an output file, update the .script_checksum file and commits.
## Dependencies
Requires bash, sha256sum and git.
## Configuration
By default it looks in this file's directory, the script is taken from the `script`
file and output is put on `output` file.
Before running the script, it pulls from remote.
After running the script, if applies, it commit and push to the remote.
Env vars:
- RUN_DIR: defaults to this file's dir
- GIT_BRANCH: defaults to main
- GIT_REMOTE: defaults to origin
## Scheduling
Add to crontab:
*/10 * * * * /path/to/gitsecute/gitsecute.sh
"
unwrap_err() {
if [ ! $? ]; then
echo "command errored: !!"
exit 1
fi
}
gitorigin=${GIT_ORIGIN:-origin}
gitbranch=${GIT_BRANCH:-main}
repo_path=${RUN_DIR:-$(dirname $BASH_SOURCE)}
cd $repo_path ; unwrap_err
git checkout $gitbranch && git pull --rebase $gitorigin $gitbranch ; unwrap_err
checksum_path=.script_checksum
script_path=script
output_path=ouput
shouldrun="si"
currchecksum=$(sha256sum $script_path | cut -f1 -d" ")
unwrap_err
if [ -f "$checksum_path" ]; then
lastchecksum=$(cat "$checksum_path")
if [ $lastchecksum = $currchecksum ]; then
shouldrun=""
fi
fi
if [ -z $shouldrun ]; then
exit 0
fi
bash $script_path 2>&1 > $output_path
echo $currchecksum > $checksum_path
git add $checksum_path $output_path
git commit -m "$(date)"
git push $gitorigin $gitbranch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment