Skip to content

Commit

Permalink
Add GitHub actions (#55)
Browse files Browse the repository at this point in the history
* Add GitHub action to set the version in heatpump.yaml when a pull request is merged to master

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Mosibi and github-actions[bot] authored Oct 20, 2024
1 parent ca66ad2 commit da1b59d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
Binary file added .github/scripts/.set_version.sh.swp
Binary file not shown.
53 changes: 53 additions & 0 deletions .github/scripts/set_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash

# This script extracts the new version from the
# file CHANGELOG.md and uses that version to update
# .esphome.project.version in heatpump.yaml


function Message() {
local LEVEL="${1:=INFO}"
local MESSAGE="${2}"
echo "$(basename $0) [${LEVEL}]: ${MESSAGE}"
}

function Info() {
local MESSAGE="${1}"
Message "INFO" "${MESSAGE}"
}

function Warning() {
local MESSAGE="${1}"
Message "WARNGIN" "${MESSAGE}"
}

function Error() {
local MESSAGE="${1}"
Message "ERROR" "${MESSAGE}"
exit 101
}

VERSION_FROM_CHANGELOG=$(grep '^## \[[0-9]' CHANGELOG.md | sed -E 's/^##\ \[([0-9].*)\]\ -.*/\1/g' | head -1)
VERSION_IN_YAML=$(awk '/^ {4}version:/ {print $NF}' heatpump.yaml)

if [[ ! -z "${VERSION_FROM_CHANGELOG}" ]] && [[ "${VERSION_FROM_CHANGELOG}" =~ ^[0-9].*$ ]]; then
Info "Version extracted from changelog is ${VERSION_FROM_CHANGELOG}"
Info "Current version in heatpump.yaml is ${VERSION_IN_YAML}"

if [[ "${VERSION_FROM_CHANGELOG}" == "${VERSION_IN_YAML}" ]]; then
Info "Versions are the same, nothing has to be done now"
exit 0
fi

# Check if version already exists
git describe "${VERSION_FROM_CHANGELOG}" --tags 1>/dev/null 2>/dev/null

if [[ $? -eq 0 ]]; then
Error "Version (tag) ${VERSION_FROM_CHANGELOG} already exists"
else
sed -i -E "s/(^\ {4}version:)(.*$)/\1 ${VERSION_FROM_CHANGELOG}/g" heatpump.yaml || Error "Could not update version in heatpump.yaml"
Info "Version in heatpump.yaml is successful updated to ${VERSION_FROM_CHANGELOG}"
fi
else
Warning "Version extracted from CHANGELOG.md does not seem to be a version tag: ${VERSION_FROM_CHANGELOG}, version will not be updated"
fi
33 changes: 33 additions & 0 deletions .github/workflows/update_version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
name: Update version in heatpump.yaml

on:
pull_request:
branches:
- master
types: [closed]

jobs:
update-version:
if: ${{ github.event.pull_request.merged }}
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Update version
run: |
cat heatpump.yaml |grep -A7 ^esphome
bash .github/scripts/set_version.sh
cat heatpump.yaml |grep -A7 ^esphome
- name: Commit files
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git commit -a -m "Updated version in heatpump.yaml"
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}
3 changes: 3 additions & 0 deletions heatpump.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ esphome:
name: "${devicename}"
comment: "${description}"
friendly_name: "${description}"
project:
name: "${devicename}.${description}"
version: 4.1.2

esp32:
board: esp32dev
Expand Down

0 comments on commit da1b59d

Please sign in to comment.