Since I struggled to get this to work, I decided to post my solution here.
Happy to take input on how to do this better, more efficient, more secure, etc…
Assumption is that you know how to store your configuration on GitHub, set your .gitignore
properly, to exclude your secrets etc. Further assumption is that you know how to configure your docker container, including persistent storage.
This post purely explains how I automated sending my Home Assistant configuration to GitHub after I converted Home Assistant from running on a Pi with HA OS to running in a docker container.
1. Ensure persistent storage of `known_hosts`
While there are multiple (docker) options to ensure persistent storage and named volumes are preferred, I opted to map
/root/.ssh/
as a bind mount
- Create your bind mount or volume in your docker container
- Add github.com to your
known_hosts
manually through SSH shell:- From within docker container:
docker exec -it "container-name" bash
ssh-keyscan -H github.com >> ~/.ssh/known_hosts
- From host:
ssh-keyscan -H github.com >> '.ssh-bind-mount-folder/known_hosts'
Without adding github.com to
known_hosts
, your commit will fail due to a “Host Key Verification Failed” Git error
2. Enable input of commit comment
Create a text_input
helper to enable setting a commit comment:
input_text:
git_commit_comment:
name: Git Commit Comment
icon: mdi:git
min: 0
max: 50
mode: text
3. Create a shell script to execute your git update:
[git_update.sh]
#!/bin/bash
exec > /config/git_update.log 2>&1
set -x
git config --global --unset-all safe.directory
git config --global --add safe.directory /config
cd /config
git add .
git status
git commit -m "$1"
git push origin master
exit
Some explanations:
exec > /config/git_update.log 2>&1
set -x
Enables logging of what was executed in a log file
git config --global --unset-all safe.directory
git config --global --add safe.directory /config
Avoids “detected dubious ownership in repository” Git error
Removing all safe.directories first avoids creating an endlessly long list of duplicate safe.directories in the global git config file of the docker container
4. [if needed] Edit git config
If you loaded your configuration to GitHub from the host before, and therefore set your ssh keys according to your host’s path, make sure to edit the git config (typically @ /config/.git/config
). i.e. make sure sshCommand has the path from within the docker container for your ssh key (e.g. /config/.ssh/id_rsa
).
5. Create `shell_command`
Integrate your shell script into Home Assistant configuration.yaml
as a Shell Command
, so it can be called from an automation:
shell_command:
git_update: "bash /config/git_update.sh '{{ states.input_text.git_commit_comment.state }}'"
Notice that the
shell_command
uses thetext_input
as an argument to be passed to the script and to be used as commit comment.
6. Create a script to call the `shell_command`:
git_update:
alias: Git Update
sequence:
- service: shell_command.git_update
data: {}
mode: single
icon: mdi:github
7. Add to your HA dashboard:
Add the input_text
and script.git_update
to your lovelace dashboard:
type: vertical-stack
title: GitHub
cards:
- type: entities
entities:
- entity: input_text.git_commit_comment
name: Commit Comment
- entity: script.git_update
secondary_info: last-triggered
name: Update GitHub
show_header_toggle: false