Automation to run shell script in Terminal add-on (SSH & Web Terminal) fails

I want to run an automation to push my HA config to GitHub.
I have basically followed this approach: https://peyanski.com/automatic-home-assistant-backup-to-github/

Setup:
-HA OS 8.4
-GitHub account and repository
-Add-on: SSH & Web Terminal

  1. Automation
alias: Push HA configuration to GitHub
trigger:
  - at: "23:23:23"
    platform: time
action:
  - data:
      addon: a0d7b954_ssh
      input: /config/ha_gitpush.sh
    service: hassio.addon_stdin
  1. Shell script (ha_gitpush.sh)
cd /config
git add .
git commit -m "test"
git push origin main
exit  

If I run the commands above directly in terminal (add-on: SSH & Web Terminal), everything works fine (files are pushed to GitHub) as expected.
When I execute the automation - it does not work.

Any help highly appreciated!

Your script doesn’t run in the HA Container if you are in the SSH & Web Terminal.

1 Like

Thanks! But I am not able to follow your advise :frowning:

I have disabled protection mode. What do I need to do to access the Docker instance running HA? Do I need to change the add-on configuration?
This is my config:

init_commands: []
packages: []
share_sessions: false
ssh:
  allow_agent_forwarding: false
  allow_remote_port_forwarding: false
  allow_tcp_forwarding: false
  authorized_keys:
    - >-
      (secret)
  compatibility_mode: false
  password: ""
  sftp: false
  username: hassio
zsh: true

Open the SSH & Web Terminal and type

docker exec -it homeassistant bash

EDIT: The SSH & Web Terminal needs to be restarted after setting Protection Mode off.

1 Like

hassio.addon_stdin is not supported on the ssh & web terminal. It was removed some time ago.

Thanks for the hint. This explains why I’m not successful here.

I will then need to find another solution to automate GitHub commits


Yes. I think you could configure a commandline to run the command via ssh.

looking the the linked script, the only package you use is git which is already installed in HA. This can be verified by:

docker exec -it homeassistant bash
git --version

You should be able to run the script ‘natively’ using a shell command. Perhaps something like:

shell_command:
  git_pushha: ./config/ha_gitpush.sh

automation:
  - alias: Push HA configuration to GitHub
    trigger:
      - at: "23:23:23"
        platform: time
    action:
      - service: shell_command.git_pushha

If you’re wanting an overengineered solution to allow for non-included packages using the SSH & Web Terminal addon, then check out (OP does a great job!):

[edit: found correct link]

1 Like

Yes, I must have been overthinking it.

You’re right that I don’t think SSH’ing is necessary here for simple git pulls, pushes and commits. However I would note that depending on how the user sets the permissions in the repo in github they may still have a challenge here. If they want a private repo or a public repo with restrictions on who can publish changes to main then I believe they’ll need git credentials stored in a file somewhere. I remember going through the git CLI login process before and I believe it stores that file in the home directory. If so, that will be a problem as its not persistent (hence why that guide helps people move all their SSH related files elsewhere).

SSH’ing to the SSH addon offers one solution to this problem. The author could add a start up script to the addon’s config that copies their credential file from somewhere persistent (like /config) to wherever git expects it to be. Another option would be to see what configuration options git has for location of file(s) like this. Or simply make the repo public and don’t add any restrictions on who can push changes to main.