Skip to content

How to autocommit config changes to github?

th33xitus edited this page Jul 12, 2022 · 7 revisions

On this wiki page i will briefly explain how to create a G-Code macro in Klipper for creating commits and pushing them to github. This will enable you to backup your printers configuration with a simple g-code macro and/or the click of a single button.

Disclaimer:

The usual disclaimer also applies here. Usage of this guide happens at your own risk.
I will not be responsible for any issues that arise during or after this procedure.


What do we need:

  • Github account
  • Github repository
  • G-Code Shell Command extension
  • Mainsail/Fluidd (optional)

⚠️ Please note, that i will not explain how to create a github account and repository. If you don't know how to do that, you need to do some research before continuing.


Let's start...

I find it really usefull to always commit my config changes to a dedicated github repository. Since i am Mainsail/Fluidd, i need to have a seperate folder for my config-files anyways to make use of their config editor.
I picked the same name for the configuration folder and the github repository: klipper_config. This makes me able to git clone the whole set of my configuration files in one go, and push them to github to make a backup. In case i need to create a fresh sdcard image for my Raspberry Pi, i can then simply install git, do a git clone of that repo and i am back in business again. All config files are at their correct location again.

Doing this by hand is a possible way. It's not big of a deal and if you are familiar with some basic git commands, you can do all of this manually each time.
But why not automating this?


Step 1:

First of all we need to install the G-Code Shell Command extension for Klipper.
Please have a look at the G-Code Shell Command Extension Doc.
You can either do this by copying the gcode_shell_command.py from the KIAUH resources folder to klipper/klippy/extras.
Don't forget to restart the Klipper service then.
Alternatively just use KIAUH for completing this task for you: [Main Menu] --> 4)[Advanced] --> 7)[Shell Command]

After we installed the G-Code Shell Command extension, we are able to run shell commands with G-Code macros.


Step 2:

Now we need a suitable shell script which does all the tasks we usually need to do manually when commiting and pushing to github.
An example script can be seen below. Feel free to use it or write your own.
Then upload the script to your Raspberry Pi.

You can also find the script here

#!/bin/bash

#####################################################################
### Please set the paths accordingly. In case you don't have all  ###
### the listed folders, just keep that line commented out.        ###
#####################################################################
### Path to your config folder you want to backup
#config_folder=~/klipper_config

### Path to your Klipper folder, by default that is '~/klipper'
#klipper_folder=~/klipper

### Path to your Moonraker folder, by default that is '~/moonraker'
#moonraker_folder=~/moonraker

### Path to your Mainsail folder, by default that is '~/mainsail'
#mainsail_folder=~/mainsail

### Path to your Fluidd folder, by default that is '~/fluidd'
#fluidd_folder=~/fluidd

#####################################################################
#####################################################################


#####################################################################
################ !!! DO NOT EDIT BELOW THIS LINE !!! ################
#####################################################################
grab_version(){
  if [ ! -z "$klipper_folder" ]; then
    cd "$klipper_folder"
    klipper_commit=$(git rev-parse --short=7 HEAD)
    m1="Klipper on commit: $klipper_commit"
    cd ..
  fi
  if [ ! -z "$moonraker_folder" ]; then
    cd "$moonraker_folder"
    moonraker_commit=$(git rev-parse --short=7 HEAD)
    m2="Moonraker on commit: $moonraker_commit"
    cd ..
  fi
  if [ ! -z "$mainsail_folder" ]; then
    mainsail_ver=$(head -n 1 $mainsail_folder/.version)
    m3="Mainsail version: $mainsail_ver"
  fi
  if [ ! -z "$fluidd_folder" ]; then
    fluidd_ver=$(head -n 1 $fluidd_folder/.version)
    m4="Fluidd version: $fluidd_ver"
  fi
}

push_config(){
  cd $config_folder
  git pull
  git add .
  current_date=$(date +"%Y-%m-%d %T")
  git commit -m "Autocommit from $current_date" -m "$m1" -m "$m2" -m "$m3" -m "$m4"
  git push
}

grab_version
push_config

If you decide to use the script above, there are some important things you need to customize. Specifying the path to your config folder is mandatory of course. All other paths are optional but filling them in gives you a more verbose commit message such as displaying the current Klipper or Moonraker commit you are on. It is also possible to write the current Mainsail and/or Fluidd version to that message. For this to work you need to have a source where the script can reads the interface version from. If you installed the interface through KIAUH, then there will be a file called version in the respective interface installation folder.\

Example of how a commit message could look like:
example commit msg


Step 3:

We do not want any username or password prompts from github because we can't react to them.
That means we need to make git not ask for our user credentials, otherwise all of this will not work.

There are different ways on how to achive that, i suggest taking a look here and pick the method you prefere:
https://www.freecodecamp.org/news/how-to-fix-git-always-asking-for-user-credentials

I went with git config --global credential.helper store.
In this case i need manually push one time to make git prompt for username and password. For a while now, Github does not allow the use of a regular password anymore. Now you need to enter a so called personal access token. You have to create one first here: https://github.com/settings/tokens
The token basically represents the password.

After we made sure we can commit and push to github, without having to re-enter username and token all the time, we are almost done.


Step 4:

The last step is to create a shell command and a G-Code macro. Here is an example on how those could look:

[gcode_shell_command backup_cfg]
command: sh <path to your shell script>
timeout: 30.
verbose: True

[gcode_macro BACKUP_CFG]
gcode:
    RUN_SHELL_COMMAND CMD=backup_cfg

A few things we need to pay attention to:

  • command: sh <path to your shell script> must point to the location of your script
  • the tilde as shorthand sign for your home directory is NOT a valid character
  • the script must be made executable -> chmod +x <script>

Example:
Let's say the script is located in your home directory, command has to look something like this:
command: sh /home/<username>/<script.sh>
The timeout of 30 seconds is just an example but i suggest not to use a way too short timeout.
Otherwise the execution of the script will be stopped forcefully before it even completes.

Step 5:

You are now able to call the G-Code macro you created and backup your config files with a single command.
Or if you use Mainsail like me even with a single click of a button:

Macro button in Mainsail: Console output after sending:
example mainsail example mainsail console

Done!

Congratulations!
You can now backup your configuration files with ease!

If you find any mistakes or have suggestions/improvements, please file an issue.