Mitto Job Versioning with Git and Github

Use Case

Currently (as of Mitto version 2.8.10) all Mitto job configs are stored in the file system as json files in the location /var/mitto/conf. Therefore in order to add versioning and keep track of any changes made to your jobs, all you need to do is create a git repository in /var/mitto/conf. Below we will outline the process:

Before you begin

Create a Github repo:

Add SSH key to the git account:

Add SSH key to the repository only (deploy key):

Create Shell Scripts and Upload files to Mitto

Create the following three shell scripts and upload them to Mitto using Mitto’s file manager UI

git_init.sh - Edit this script with your git account name and repo name.

#!/bin/bash

cd /var/mitto/conf
git init
git add --all
git commit -m "first commit from mitto"
git branch -M main
git remote add origin git@github.com:{account_name}/{repo_name}.git
git push -u origin main

git_refresh.sh - Edit this script if you want to edit the commit messages.

#!/bin/bash
  
cd /var/mitto/conf
git pull
git add --all
date_today=`date '+%Y-%m-%d %H:%M:%S'`
git commit -m "$date_today - refreshing mitto job configs"
git push

add_key.sh

#!/bin/bash

mkdir -p /var/mitto/etc/.ssh/
mv /var/mitto/data/$1 /var/mitto/etc/.ssh/
chmod 0400 /var/mitto/etc/.ssh/$1

Don’t forget to upload your git SSH key to Mitto as well. You can use the files page in the Mitto UI. There should be four files in total.

Move Key and Change Permissions

Create a Mitto command line job with the following command (change {key_name} to the name of the SSH key you uploaded):
bash /var/mitto/data/add_key.sh {key_name}

The full job config will look like this:

{
    "cmd": "bash /var/mitto/data/add_key.sh {key_name}",
    "cmd_env": {},
    "exec": false,
    "shell": true
}

You will only need to run this job once.

Init Git Repo and Push Files

Create another CMD job with a command that runs the git_init.sh script.
Command: bash /var/mitto/data/git_init.sh > /var/mitto/data/git_init.txt 2>&1

In addition to running the script, this command will print the output and/or error to a text file named git_init.txt

For this to work you also need to set some environment variables:

Environment Variable Value
GIT_SSH_COMMAND ssh -o StrictHostKeyChecking=no -i /var/mitto/etc/.ssh/{key_name}
GIT_COMMITTER_NAME mitto
GIT_AUTHOR_NAME mitto
EMAIL {your_email_address}

The first env variable tells git to use your SSH key, and tells SSH to automatically add your git provider’s hostname to the known_hosts file. The rest of the env variables set a git name and email.

After running the job check the output of the command using Mitto’s API file endpoint:
https://{mitto_url}/api/file/git_init.txt

If the job is successful you will see files in your git repository.

You will only need to run this init job once.

Refresh Git Repo

Finally, Create another CMD job that will pull any changes, add files with changes, commit and push to your git repo. You can put this job on a schedule to keep it up to date.
Command: bash /var/mitto/data/git_refresh.sh > /var/mitto/data/git_refresh.txt 2>&1

You will need the same environment variables as above:

Environment Variable Value
GIT_SSH_COMMAND ssh -o StrictHostKeyChecking=no -i /var/mitto/etc/.ssh/{key_name}
GIT_COMMITTER_NAME mitto
GIT_AUTHOR_NAME mitto
EMAIL {your_email_address}

Pro-Tip: when creating this job, duplicate the init job above, and edit the config with the refresh script name and output file.

After running the job check the output of the command using Mitto’s API file endpoint:
https://{mitto_url}/api/file/git_refresh.txt

3 Likes

Zuar has implemented this (thanks @Andy) and took it one step further by adding notifications from Github to Slack using the Slack Github app: https://slack.github.com/

1 Like