Skip to content

Commit

Permalink
Add build caching (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesserockz authored Feb 29, 2024
1 parent b2737ec commit ed76b28
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ ARG VERSION=latest

FROM ghcr.io/esphome/esphome:${VERSION}

ENV PLATFORMIO_GLOBALLIB_DIR=

COPY entrypoint.py /entrypoint.py

ENTRYPOINT ["/entrypoint.py"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Name | Default | Description
`yaml_file` | _None_ | The YAML file to be compiled.
`version` | `latest` | The ESPHome version to build using.
`platform` | `linux/amd64` | The docker platform to use during build. (linux/amd64, linux/arm64, linux/arm/v7)
`cache` | `false` | Whether to cache the build folder.

## Outputs

Expand Down
35 changes: 33 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ inputs:
description: Platform (OS/Arch) to use
required: false
default: linux/amd64
cache:
description: Cache build directory
required: false
default: false

outputs:
name:
Expand All @@ -39,6 +43,32 @@ runs:
tags: esphome:${{ inputs.version }}
build-args: VERSION=${{ inputs.version }}
platforms: ${{ inputs.platform }}
- if: ${{ inputs.cache == 'true' }}
id: data-dir
shell: bash
run: |
data_dir=$(dirname ${{ inputs.yaml_file }})/.esphome
echo "data_dir=$data_dir" >> $GITHUB_OUTPUT
- if: ${{ inputs.cache == 'true' }}
name: Cache platformio directory
uses: actions/[email protected]
with:
path: ~/.platformio
key: ${{ runner.os }}-esphome-${{ inputs.yaml_file }}-${{ inputs.version }}-platformio
- if: ${{ inputs.cache == 'true' }}
name: Cache build directory
uses: actions/[email protected]
with:
path: ${{ steps.data-dir.outputs.data_dir }}/build
key: ${{ runner.os }}-esphome-${{ inputs.yaml_file }}-${{ inputs.version }}-build
save-always: true
- if: ${{ inputs.cache == 'true' }}
name: Cache storage directory
uses: actions/[email protected]
with:
path: ${{ steps.data-dir.outputs.data_dir }}/storage
key: ${{ runner.os }}-esphome-${{ inputs.yaml_file }}-${{ inputs.version }}-storage
save-always: true
- name: Run container
shell: bash
id: build-step
Expand All @@ -47,6 +77,7 @@ runs:
--platform ${{ inputs.platform }} \
--workdir /github/workspace \
-v "$(pwd)":"/github/workspace" -v "$HOME:$HOME" \
--user $(id -u):$(id -g) \
-e INPUT_YAML_FILE -e HOME \
-e GITHUB_JOB -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY \
-e GITHUB_REPOSITORY_OWNER -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER \
Expand All @@ -56,5 +87,5 @@ runs:
${{ inputs.yaml_file }}
branding:
icon: 'archive'
color: 'white'
icon: "archive"
color: "white"
32 changes: 14 additions & 18 deletions entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import re
import yaml

GH_RUNNER_USER_UID = 1001
GH_RUNNER_USER_GID = 121

ESP32_CHIP_FAMILIES = {
"ESP32": "ESP32",
Expand All @@ -27,11 +25,15 @@
filename = Path(sys.argv[1])

print("::group::Compile firmware")
rc = subprocess.run(["esphome", "compile", filename], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
rc = subprocess.run(
["esphome", "compile", filename],
stdout=sys.stdout,
stderr=sys.stderr,
check=False,
)
if rc.returncode != 0:
sys.exit(rc)

print(rc.stdout.decode("utf-8"))
print("::endgroup::")

print("::group::Get ESPHome version")
Expand All @@ -42,7 +44,7 @@
version = version.decode("utf-8")
print(version)
version = version.split(" ")[1]
with open(os.environ["GITHUB_OUTPUT"], "a") as github_output:
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as github_output:
print(f"esphome-version={version}", file=github_output)
print("::endgroup::")

Expand Down Expand Up @@ -70,15 +72,17 @@

name += f"-{platform}"

with open(os.environ["GITHUB_OUTPUT"], "a") as github_output:
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as github_output:
print(f"name={name}", file=github_output)
print("::endgroup::")

file_base = Path(name)

print("::group::Get IDEData")
try:
idedata = subprocess.check_output(["esphome", "idedata", filename], stderr=sys.stderr)
idedata = subprocess.check_output(
["esphome", "idedata", filename], stderr=sys.stderr
)
except subprocess.CalledProcessError as e:
sys.exit(e.returncode)

Expand All @@ -97,12 +101,7 @@
print("::group::Copy firmware file(s) to folder")
file_base.mkdir(parents=True, exist_ok=True)

if os.environ.get("GITHUB_JOB") is not None:
shutil.chown(file_base, GH_RUNNER_USER_UID, GH_RUNNER_USER_GID)

shutil.copyfile(source_bin, dest_bin)
if os.environ.get("GITHUB_JOB") is not None:
shutil.chown(dest_bin, GH_RUNNER_USER_UID, GH_RUNNER_USER_GID)

print("::endgroup::")

Expand All @@ -117,7 +116,7 @@
if define == "USE_ESP8266":
chip_family = "ESP8266"
break
elif m := re.match(r"USE_ESP32_VARIANT_(\w+)", define):
if m := re.match(r"USE_ESP32_VARIANT_(\w+)", define):
chip_family = m.group(1)
if chip_family not in ESP32_CHIP_FAMILIES:
raise Exception(f"Unsupported chip family: {chip_family}")
Expand All @@ -135,13 +134,10 @@
],
}

print(f"Writing manifest file:")
print("Writing manifest file:")
print(json.dumps(manifest, indent=2))

with open(file_base / "manifest.json", "w") as f:
with open(file_base / "manifest.json", "w", encoding="utf-8") as f:
json.dump(manifest, f, indent=2)

if os.environ.get("GITHUB_JOB") is not None:
shutil.chown(file_base / "manifest.json", GH_RUNNER_USER_UID, GH_RUNNER_USER_GID)

print("::endgroup::")

0 comments on commit ed76b28

Please sign in to comment.