From 6ba8822305595b6336ba1d762737dde12cc74338 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Fri, 30 Oct 2020 16:50:40 +0100 Subject: [PATCH] How to autocommit config changes to github? --- ...to-autocommit-config-changes-to-github?.md | 171 ++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 How-to-autocommit-config-changes-to-github?.md diff --git a/How-to-autocommit-config-changes-to-github?.md b/How-to-autocommit-config-changes-to-github?.md new file mode 100644 index 0000000..58a6453 --- /dev/null +++ b/How-to-autocommit-config-changes-to-github?.md @@ -0,0 +1,171 @@ +# **💡 How to autocommit config changes to github?** + +**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](https://github.com/th33xitus/kiauh/blob/master/docs/gcode_shell_command.md).\ +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. +```shell +#!/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=8 HEAD) + m1="Klipper on commit: $klipper_commit" + cd ${HOME} + fi + if [ ! -z "$moonraker_folder" ]; then + cd "$moonraker_folder" + moonraker_commit=$(git rev-parse --short=8 HEAD) + m2="Moonraker on commit: $moonraker_commit" + cd ${HOME} + fi + if [ ! -z "$mainsail_folder" ] && [ -f $mainsail_folder/version ]; then + mainsail_ver=$(head -n 1 $mainsail_folder/version) + m3="Mainsail version: $mainsail_ver" + fi + if [ ! -z "$fluidd_folder" ] && [ -f $mainsail_folder/version ]; 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](https://abload.de/img/20-10-30_16-26-18_firq5jte.png)](https://abload.de/image.php?img=20-10-30_16-26-18_firq5jte.png) + +--- + +## **🟡 Step 3:** +We do not want any username or password prompts from github because we can't react to them.\ +Therefore shell command and macro needs to be able to run without any further interaction. +That means we need to make git not ask for 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 + +Personally, i went with sending a `git config --global credential.helper store`.\ +In this case i needed to make one push manually to make git prompt for username and password so it can save those. + +After we made sure we can commit and push to github without having to re-enter username and password 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: +```c++ +[gcode_shell_command backup_cfg] +command: sh +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 ` **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