Update config automatically if Travis CI build succeeds

Hi all,

Inspired by the thread, I wanted to automate the deployment workflow for my HASS config.

My goal was to have HASS automatically pull the config from Github, once the Travis CI build check succeeded.

I added two sensors for getting information from Travis CI regarding the latest build:

Whenever a build is passed in Travis CI, the “sensor.travis_build_label” will return a new label. This state change can be used as a tigger in an automation.

The following automation is used to trigger a new config update and to check if the latest build was passed by Travis CI
https://github.com/TribuneX/home_assistant/blob/master/automation/update_config.yaml

This script eventually executes the following bash script:
https://github.com/TribuneX/home_assistant/blob/master/bash/git_pull.sh

As a result, I just need to push my configuration changes to Github. If the changes will pass the Travis CI check, the new version of the configurations is automatically deployed.

2 Likes

Just some food for thought, but you might benefit from using RESTful Sensors instead of Command Line Sensors :slight_smile:

In which you could have a RESTful Binary Sensor for the status:

binary_sensor:
  - platform: rest
    name: Travis Status
    resource: https://api.travis-ci.org/repos/TribuneX/home_assistant/cc.json?branch=master
    scan_interval: 60
    value_template: "{{ value_json.last_build_status|int(1) == 0 }}"

…and a RESTful Sensor for the label:

sensor:
  - platform: rest
    name: Travis Status Label
    resource: https://api.travis-ci.org/repos/TribuneX/home_assistant/cc.json?branch=master
    scan_interval: 60
    value_template: "{{ value_json.last_build_number }}"

This is a great idea!

I switched now to the new Travis sensor for this automation:

- alias: 'Misc: Update config if travis build is successfull'
  trigger:
    platform: state
    entity_id: sensor.tribunexhome_assistant_last_build_state
    to: 'passed'
  action:
    - service: script.get_latest_config

I recently migrated my home assistant setup to run inside a docker container. Therefore, it is not so easy anymore to call git pull from within my container. I wonder how other people are updating their repo from their home assistant automatically?

The only solution so far could be to ssh from the container to the actual host and execute git pull in this way. But this looks more like a hack, so I am curious if there is a cleaner solution to that.

So, I am using a container as well with a volume mounted from the host (per the setup guide) and I have this running as a CRON job every minute on the host machine.

#!/bin/sh

if [ ! -f /tmp/last_build.txt ]; then
    touch /tmp/last_build.txt
fi

current_build=`curl -H "Travis-API-Version: 3" -s https://api.travis-ci.org/repo/14706137/branch/master | jq .last_build.number`
current_build="${current_build%\"}"
current_build="${current_build#\"}"

current_status=`curl -H "Travis-API-Version: 3" -s https://api.travis-ci.org/repo/14706137/branch/master | jq .last_build.state`
current_status="${current_status%\"}"
current_status="${current_status#\"}"

last_build=`cat /tmp/last_build.txt`

if [[ "$current_status" == "passed" && "$current_build" != "$last_build" ]] ; then
  cd /data/home-assistant ; git pull
  rancher restart --env 1a5 automation/home-assistant
  echo $current_build > /tmp/last_build.txt
fi;

Note: I use Rancher to manage my docker environment so you can take out that like with the “rancher restart” in it.

1 Like

Trying to implement this but when I run the script I get:

./update_ha_config.sh: 17: ./update_ha_config.sh: [[: not found

Ps: find your repo-id with this command:

 curl -L http://api.travis-ci.org/repos/user_name/repo_name

I did a rewrite of your script in Python using the Docker API and the Travis API. I’m not a developer, comments are welcome :wink:

It can be found on Github.

1 Like

Thanks for sharing. I‘ll take a look at your script. Do you call it periodically via a cron job?

Yes, I run it as a cron job. I have to find out about the 403 I get regularly from Travis, I guess it must be possible to get the build status without authenticating.

Fixed the 403. Dropped the travispy package, now using requests to get the build-info.