diff --git a/Dockerfile b/Dockerfile index 9e5e3de..9dd2835 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] diff --git a/README.md b/README.md index 7ba3dc3..f92150f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/action.yml b/action.yml index a63fae6..9da875e 100644 --- a/action.yml +++ b/action.yml @@ -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: @@ -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/cache@v4.0.0 + with: + path: ~/.platformio + key: ${{ runner.os }}-esphome-${{ inputs.yaml_file }}-${{ inputs.version }}-platformio + - if: ${{ inputs.cache == 'true' }} + name: Cache build directory + uses: actions/cache@v4.0.0 + 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/cache@v4.0.0 + 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 @@ -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 \ @@ -56,5 +87,5 @@ runs: ${{ inputs.yaml_file }} branding: - icon: 'archive' - color: 'white' + icon: "archive" + color: "white" diff --git a/entrypoint.py b/entrypoint.py index bf95a85..c3cbe09 100755 --- a/entrypoint.py +++ b/entrypoint.py @@ -9,8 +9,6 @@ import re import yaml -GH_RUNNER_USER_UID = 1001 -GH_RUNNER_USER_GID = 121 ESP32_CHIP_FAMILIES = { "ESP32": "ESP32", @@ -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") @@ -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::") @@ -70,7 +72,7 @@ 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::") @@ -78,7 +80,9 @@ 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) @@ -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::") @@ -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}") @@ -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::")