Skip to content

Commit ddf6870

Browse files
authored
Unify Docker Repos (#212)
* Merge docker repos Removes base image -> use docker caching Removes s6-overlay-base -> Include files here * Cleanup * Move cache back * Remove base trigger * Use inline cache * Remove inline * Rename final * Debug cache miss * Move label args * Test build
1 parent 255baee commit ddf6870

File tree

8 files changed

+264
-53
lines changed

8 files changed

+264
-53
lines changed

.github/workflows/docker-build.yaml

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,29 @@ jobs:
1414
include:
1515
- arch: linux/386
1616
arch_friendly: i386
17+
hass_arch: i386
18+
s6_arch: x86
19+
rust_arch: i686-unknown-linux-gnu
1720
- arch: linux/amd64
1821
arch_friendly: amd64
22+
hass_arch: amd64
23+
s6_arch: amd64
24+
rust_arch: x86_64-unknown-linux-gnu
1925
- arch: linux/arm/v6
2026
arch_friendly: armv6
27+
hass_arch: armv6
28+
s6_arch: arm
29+
rust_arch: arm-unknown-linux-gnueabi
2130
- arch: linux/arm/v7
2231
arch_friendly: armv7
32+
hass_arch: armv7
33+
s6_arch: armhf
34+
rust_arch: armv7-unknown-linux-gnueabihf
2335
- arch: linux/arm64
2436
arch_friendly: arm64
37+
hass_arch: aarch64
38+
s6_arch: aarch64
39+
rust_arch: aarch64-unknown-linux-gnu
2540

2641
steps:
2742
- name: Checkout
@@ -35,17 +50,31 @@ jobs:
3550
with:
3651
install: true
3752

53+
- name: Cache Docker layers
54+
uses: actions/cache@v2
55+
continue-on-error: true
56+
with:
57+
path: /tmp/.buildx-cache
58+
key: ${{ matrix.arch }}-${{ github.sha }}
59+
restore-keys: |
60+
${{ matrix.arch }}-
61+
3862
# label version as branch-run_number
3963
- name: Build Branch
4064
if: ${{ startsWith(github.ref, 'refs/heads/') }}
4165
run: |
4266
set -x
4367
docker build \
68+
--build-arg HASS_ARCH=${{ matrix.hass_arch }} \
69+
--build-arg S6_ARCH=${{ matrix.s6_arch }} \
70+
--build-arg RUST_ARCH=${{ matrix.rust_arch }} \
4471
--build-arg BUILD_VERSION=${GITHUB_REF##*/}-${{ github.run_number }} \
4572
--tag ci:${{ github.run_number }} \
4673
--platform ${{ matrix.arch }} \
4774
--progress plain \
48-
--file ./Dockerfile \
75+
--file ./docker/Dockerfile \
76+
--cache-from type=local,src=/tmp/.buildx-cache \
77+
--cache-to type=local,dest=/tmp/.buildx-cache-new \
4978
--load \
5079
.
5180
@@ -55,18 +84,32 @@ jobs:
5584
run: |
5685
set -x
5786
docker build \
87+
--build-arg HASS_ARCH=${{ matrix.hass_arch }} \
88+
--build-arg S6_ARCH=${{ matrix.s6_arch }} \
89+
--build-arg RUST_ARCH=${{ matrix.rust_arch }} \
5890
--build-arg BUILD_VERSION=${GITHUB_REF##*/} \
5991
--tag ci:${{ github.run_number }} \
6092
--platform ${{ matrix.arch }} \
6193
--progress plain \
62-
--file ./Dockerfile \
94+
--file ./docker/Dockerfile \
95+
--cache-from type=local,src=/tmp/.buildx-cache \
96+
--cache-to type=local,dest=/tmp/.buildx-cache-new \
6397
--load \
6498
.
6599
100+
# Temp fix
101+
# https://github.com/docker/build-push-action/issues/252
102+
# https://github.com/moby/buildkit/issues/1896
103+
- name: Move cache
104+
run: |
105+
rm -rf /tmp/.buildx-cache
106+
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
107+
66108
- name: Inspect
67109
run: |
68110
set -x
69111
docker image inspect ci:${{ github.run_number }}
112+
docker history --no-trunc ci:${{ github.run_number }}
70113
71114
- name: Save tarball
72115
run: |

.github/workflows/update-base.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

Dockerfile

Lines changed: 0 additions & 30 deletions
This file was deleted.

docker/Dockerfile

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# syntax=docker/dockerfile:experimental
2+
ARG HASS_ARCH=amd64
3+
ARG S6_ARCH=amd64
4+
ARG RUST_ARCH=x86_64-unknown-linux-gnu
5+
ARG BUILD_VERSION=latest
6+
7+
#####################################################################
8+
# #
9+
# Build Wheels #
10+
# #
11+
#####################################################################
12+
FROM python:3.9-slim as wheels-builder
13+
ARG RUST_ARCH
14+
15+
ENV PIP_EXTRA_INDEX_URL=https://www.piwheels.org/simple
16+
ENV PATH="${PATH}:/root/.cargo/bin"
17+
18+
# Install buildtime packages
19+
RUN set -x \
20+
&& apt-get update \
21+
&& apt-get install -y --no-install-recommends \
22+
build-essential \
23+
ca-certificates \
24+
curl \
25+
gcc \
26+
git \
27+
libffi-dev \
28+
libssl-dev
29+
30+
RUN set -x \
31+
&& curl -o rustup-init https://static.rust-lang.org/rustup/dist/${RUST_ARCH}/rustup-init \
32+
&& chmod +x rustup-init \
33+
&& ./rustup-init -y --no-modify-path --profile minimal --default-host ${RUST_ARCH}
34+
35+
WORKDIR /wheels
36+
COPY requirements.txt .
37+
38+
# build python wheels
39+
RUN set -x \
40+
&& pip wheel -r requirements.txt
41+
42+
#####################################################################
43+
# #
44+
# Download and extract s6 overlay #
45+
# #
46+
#####################################################################
47+
FROM alpine:latest as s6downloader
48+
# Required to persist build arg
49+
ARG S6_ARCH
50+
WORKDIR /s6downloader
51+
52+
RUN set -x \
53+
&& OVERLAY_VERSION=$(wget --no-check-certificate -qO - https://api.github.com/repos/just-containers/s6-overlay/releases/latest | awk '/tag_name/{print $4;exit}' FS='[""]') \
54+
&& wget -O /tmp/s6-overlay.tar.gz "https://github.com/just-containers/s6-overlay/releases/download/${OVERLAY_VERSION}/s6-overlay-${S6_ARCH}.tar.gz" \
55+
&& mkdir -p /tmp/s6 \
56+
&& tar zxvf /tmp/s6-overlay.tar.gz -C /tmp/s6 \
57+
&& mv /tmp/s6/* .
58+
59+
#####################################################################
60+
# #
61+
# Download and extract bashio #
62+
# #
63+
#####################################################################
64+
FROM alpine:latest as bashiodownloader
65+
WORKDIR /bashio
66+
67+
RUN set -x \
68+
&& wget -O /tmp/bashio.tar.gz "https://github.com/hassio-addons/bashio/archive/v0.13.1.tar.gz" \
69+
&& mkdir -p /tmp/bashio \
70+
&& tar zxvf /tmp/bashio.tar.gz --strip 1 -C /tmp/bashio \
71+
&& mv /tmp/bashio/lib/* .
72+
73+
#####################################################################
74+
# #
75+
# Final Image #
76+
# #
77+
#####################################################################
78+
FROM python:3.9-slim AS final-build
79+
WORKDIR /app
80+
81+
ENV DEBIAN_FRONTEND="noninteractive"
82+
83+
RUN set -x \
84+
&& apt-get update \
85+
&& apt-get install -y --no-install-recommends \
86+
ca-certificates \
87+
curl \
88+
git \
89+
jq \
90+
openssl \
91+
tzdata \
92+
# cleanup
93+
&& rm -rf /tmp/* \
94+
&& rm -rf /var/lib/apt/lists/*
95+
96+
# Install bashio
97+
COPY --from=bashiodownloader /bashio /usr/lib/bashio
98+
RUN ln -s /usr/lib/bashio/bashio /usr/bin/bashio
99+
100+
# Install s6 overlay
101+
COPY --from=s6downloader /s6downloader /
102+
103+
# https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/syntax.md#build-mounts-run---mount
104+
# Install pip dependencies with built wheels
105+
RUN --mount=type=bind,target=/tmp/wheels,source=/wheels,from=wheels-builder,rw \
106+
set -x \
107+
&& pip install --no-cache-dir -f /tmp/wheels -r /tmp/wheels/requirements.txt
108+
109+
# Copy root filesystem
110+
COPY docker/rootfs /
111+
112+
# Copy app
113+
COPY emulated_hue emulated_hue
114+
115+
ENV S6_BEHAVIOUR_IF_STAGE2_FAILS=2
116+
117+
# Required to persist build arg
118+
ARG BUILD_VERSION
119+
ARG HASS_ARCH
120+
LABEL \
121+
io.hass.version=${BUILD_VERSION} \
122+
io.hass.name="Hass Emulated Hue" \
123+
io.hass.description="Hue Emulation for Home Assistant" \
124+
io.hass.arch="${HASS_ARCH}" \
125+
io.hass.type="addon"
126+
127+
CMD ["/init"]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/with-contenv bashio
2+
# ==============================================================================
3+
# Community Hass.io Add-ons: Emulated Hue
4+
# This file installs the Emulated Hue version if specified
5+
# ==============================================================================
6+
7+
declare release_version
8+
9+
if bashio::config.has_value 'tag_commit_or_branch'; then
10+
release_version=$(bashio::config 'tag_commit_or_branch')
11+
else
12+
release_version=${TAG_COMMIT_OR_BRANCH:-master}
13+
fi
14+
15+
colon_count=$(tr -dc ':' <<<"$release_version" | awk '{ print length; }')
16+
repo_name="core"
17+
18+
if [[ "$colon_count" == 1 ]]; then
19+
IFS=':' read -r -a array <<< "$release_version"
20+
username=${array[0]}
21+
ref=${array[1]}
22+
elif [ "$colon_count" == 2 ]; then
23+
IFS=':' read -r -a array <<< "$release_version"
24+
username=${array[0]}
25+
repo_name=${array[1]}
26+
ref=${array[2]}
27+
else
28+
username="hass-emulated-hue"
29+
ref=$release_version
30+
fi
31+
full_url="https://github.com/${username}/${repo_name}/archive/${ref}.tar.gz"
32+
bashio::log.info "Installing Emulated Hue version '${release_version}' (${full_url})..."
33+
curl -Lo /tmp/emulator.tar.gz "${full_url}"
34+
mkdir -p /tmp/emulator
35+
tar zxvf /tmp/emulator.tar.gz --strip 1 -C /tmp/emulator
36+
mv /tmp/emulator/emulated_hue .
37+
bashio::log.info "Installed successfully!"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/with-contenv bashio
2+
# ==============================================================================
3+
# Community Hass.io Add-ons: Emulated Hue
4+
# This file defines environment variables based on user specified config options
5+
# This overrides native environment variables with options.json
6+
# ==============================================================================
7+
8+
9+
if bashio::fs.file_exists '/data/options.json'; then
10+
11+
if bashio::config.has_value 'data'; then
12+
echo $(bashio::config 'data') > /var/run/s6/container_environment/DATA_DIR
13+
fi
14+
15+
if bashio::config.has_value 'http_port'; then
16+
echo $(bashio::config 'http_port') > /var/run/s6/container_environment/HTTP_PORT
17+
fi
18+
19+
if bashio::config.has_value 'https_port'; then
20+
echo $(bashio::config 'https_port') > /var/run/s6/container_environment/HTTPS_PORT
21+
fi
22+
23+
if bashio::config.has_value 'token'; then
24+
echo $(bashio::config 'token') > /var/run/s6/container_environment/HASS_TOKEN
25+
fi
26+
27+
if bashio::config.has_value 'url'; then
28+
echo $(bashio::config 'url') > /var/run/s6/container_environment/HASS_URL
29+
fi
30+
31+
if bashio::config.has_value 'use_default_ports_for_discovery'; then
32+
echo $(bashio::config 'use_default_ports_for_discovery') > /var/run/s6/container_environment/USE_DEFAULT_PORTS
33+
fi
34+
35+
if bashio::config.has_value 'verbose'; then
36+
echo $(bashio::config 'verbose') > /var/run/s6/container_environment/VERBOSE
37+
fi
38+
39+
fi
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/execlineb -S0
2+
# ==============================================================================
3+
# Community Hass.io Add-ons: Emulated Hue
4+
# Take down the S6 supervision tree when Emulated Hue fails to start/crashes
5+
# ==============================================================================
6+
7+
s6-svscanctl -t /var/run/s6/services
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/with-contenv bashio
2+
# ==============================================================================
3+
# Community Hass.io Add-ons: Emulated Hue
4+
# Runs Emulated Hue
5+
# ==============================================================================
6+
7+
bashio::log.info "Starting Emulated Hue..."
8+
cd /app
9+
exec python3 -m emulated_hue

0 commit comments

Comments
 (0)