From 1830273f024c301ef474a727d763e07c43ad1b28 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Wed, 5 Oct 2022 15:37:23 -0300 Subject: [PATCH 01/10] Refactor docker image --- .dockerignore | 8 +++++ .github/workflows/builder.yml | 8 ++--- Dockerfile | 42 ++++++++++++++++++++++ build/hassio/Dockerfile | 41 --------------------- build/hassio/run.sh | 14 -------- build/hassio/config.yaml => config.yaml | 0 docker/rootfs/etc/services.d/go2rtc/finish | 12 +++++++ docker/rootfs/etc/services.d/go2rtc/run | 23 ++++++++++++ 8 files changed, 89 insertions(+), 59 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile delete mode 100644 build/hassio/Dockerfile delete mode 100644 build/hassio/run.sh rename build/hassio/config.yaml => config.yaml (100%) create mode 100755 docker/rootfs/etc/services.d/go2rtc/finish create mode 100755 docker/rootfs/etc/services.d/go2rtc/run diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..b03cbe4b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +.git/ +assets/ +examples/ + +.dockerignore +config.yaml +Dockerfile +README.md diff --git a/.github/workflows/builder.yml b/.github/workflows/builder.yml index 60b0fb48..f8884b9b 100644 --- a/.github/workflows/builder.yml +++ b/.github/workflows/builder.yml @@ -30,22 +30,22 @@ jobs: - name: Build amd64 uses: home-assistant/builder@master with: - args: --amd64 --target build/hassio --version $TAG-amd64 --no-latest --docker-hub-check + args: --amd64 --target . --version $TAG-amd64 --no-latest --docker-hub-check - name: Build i386 uses: home-assistant/builder@master with: - args: --i386 --target build/hassio --version $TAG-i386 --no-latest --docker-hub-check + args: --i386 --target . --version $TAG-i386 --no-latest --docker-hub-check - name: Build aarch64 uses: home-assistant/builder@master with: - args: --aarch64 --target build/hassio --version $TAG-aarch64 --no-latest --docker-hub-check + args: --aarch64 --target . --version $TAG-aarch64 --no-latest --docker-hub-check - name: Build armv7 uses: home-assistant/builder@master with: - args: --armv7 --target build/hassio --version $TAG-armv7 --no-latest --docker-hub-check + args: --armv7 --target . --version $TAG-armv7 --no-latest --docker-hub-check - name: Docker manifest run: | diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..25346a0b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,42 @@ +# https://github.com/hassio-addons/addon-base-python/releases +ARG BASE_VERSION="9.0.1" +# https://hub.docker.com/_/golang/tags?page=1&name=-alpine +ARG GO_VERSION="1.19.2" +# https://hub.docker.com/r/ngrok/ngrok/tags?page=1&name=-alpine +ARG NGROK_VERSION="3.1.0" + + +FROM ghcr.io/hassio-addons/base-python:${BASE_VERSION} AS base + + +FROM golang:${GO_VERSION}-alpine AS go + + +FROM ngrok/ngrok:${NGROK_VERSION}-alpine AS ngrok + + +# Build go2rtc binary +FROM go AS build + +WORKDIR /workspace + +COPY . . + +RUN CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath + + +# Collect all files +FROM scratch AS rootfs + +COPY --from=build /workspace/go2rtc /usr/local/bin/ +COPY --from=ngrok /bin/ngrok /usr/local/bin/ +COPY ./docker/rootfs/ / + + +# Final stage +FROM base + +# Install ffmpeg +RUN apk add --no-cache ffmpeg + +COPY --from=rootfs / / diff --git a/build/hassio/Dockerfile b/build/hassio/Dockerfile deleted file mode 100644 index 312909f0..00000000 --- a/build/hassio/Dockerfile +++ /dev/null @@ -1,41 +0,0 @@ -ARG BUILD_FROM - -FROM $BUILD_FROM as build - -# 1. Build go2rtc -RUN apk add --no-cache git go - -RUN git clone https://github.com/AlexxIT/go2rtc \ - && cd go2rtc \ - && CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath - -# 2. Download ngrok -ARG BUILD_ARCH - -# https://github.com/home-assistant/docker-base/blob/master/alpine/Dockerfile -RUN if [ "${BUILD_ARCH}" = "aarch64" ]; then BUILD_ARCH="arm64"; \ - elif [ "${BUILD_ARCH}" = "armv7" ]; then BUILD_ARCH="arm"; fi \ - && cd go2rtc \ - && curl $(curl -s "https://raw.githubusercontent.com/ngrok/docker-ngrok/main/releases.json" | jq -r ".${BUILD_ARCH}.url") -o ngrok.zip \ - && unzip ngrok - - - -# https://devopscube.com/reduce-docker-image-size/ -FROM $BUILD_FROM - -# 3. Copy go2rtc and ngrok to release -COPY --from=build /go2rtc/go2rtc /usr/local/bin -COPY --from=build /go2rtc/ngrok /usr/local/bin - -# 4. Install ffmpeg -# apk base OK: 22 MiB in 40 packages -# ffmpeg OK: 113 MiB in 110 packages -# python3 OK: 161 MiB in 114 packages -RUN apk add --no-cache ffmpeg python3 - -# 5. Copy run to release -COPY run.sh / -RUN chmod a+x /run.sh - -CMD [ "/run.sh" ] diff --git a/build/hassio/run.sh b/build/hassio/run.sh deleted file mode 100644 index d59564ca..00000000 --- a/build/hassio/run.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/with-contenv bashio - -set +e - -# set cwd for go2rtc (for config file, Hass integration, etc) -cd /config - -# add the feature to override go2rtc binary from Hass config folder -export PATH="/config:$PATH" - -while true; do - go2rtc - sleep 5 -done \ No newline at end of file diff --git a/build/hassio/config.yaml b/config.yaml similarity index 100% rename from build/hassio/config.yaml rename to config.yaml diff --git a/docker/rootfs/etc/services.d/go2rtc/finish b/docker/rootfs/etc/services.d/go2rtc/finish new file mode 100755 index 00000000..f73fe4ba --- /dev/null +++ b/docker/rootfs/etc/services.d/go2rtc/finish @@ -0,0 +1,12 @@ +#!/command/with-contenv bashio +# ============================================================================== +# Take down the S6 supervision tree when go2rtc fails +# ============================================================================== +# shellcheck shell=bash + +if [[ "${1}" -ne 0 ]] && [[ "${1}" -ne 256 ]]; then + bashio::log.warning "go2rtc crashed, halting add-on" + /run/s6/basedir/bin/halt +fi + +bashio::log.info "go2rtc stopped, restarting..." diff --git a/docker/rootfs/etc/services.d/go2rtc/run b/docker/rootfs/etc/services.d/go2rtc/run new file mode 100755 index 00000000..99fd8fc2 --- /dev/null +++ b/docker/rootfs/etc/services.d/go2rtc/run @@ -0,0 +1,23 @@ +#!/command/with-contenv bashio +# ============================================================================== +# Runs go2rtc +# ============================================================================== +# shellcheck shell=bash + +bashio::log.info 'Starting go2rtc...' + +declare config_path +declare binary_path + +config_path="/config" + +binary_path="/usr/local/bin/go2rtc" +if [[ -x "${config_path}/go2rtc" ]]; then + binary_path="${config_path}/go2rtc" + bashio::log.warning "Using go2rtc binary from '${binary_path}'" +fi + +# set cwd for go2rtc (for config file, Hass integration, etc) +cd "${config_path}" || bashio::exit.nok "Could not change working directory" + +exec "${binary_path}" From b09bbd79c4ff6e087b61ac3ab1e935d551078585 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Sun, 6 Nov 2022 21:12:53 -0300 Subject: [PATCH 02/10] Fix VERSION --- .github/workflows/builder.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/builder.yml b/.github/workflows/builder.yml index f8884b9b..95b8b10d 100644 --- a/.github/workflows/builder.yml +++ b/.github/workflows/builder.yml @@ -23,6 +23,7 @@ jobs: - name: Branch name run: | VERSION="${GITHUB_REF#refs/tags/v}" + VERSION="${VERSION#refs/heads/}" echo "REPO=alexxit/go2rtc" >> $GITHUB_ENV echo "TAG=${VERSION}" >> $GITHUB_ENV echo "IMAGE=alexxit/go2rtc:${VERSION}" >> $GITHUB_ENV From 15d9d4ebf45e531b1428fa6e74ad2b2026b41412 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Fri, 18 Nov 2022 14:49:37 -0300 Subject: [PATCH 03/10] Use official python as base and add tini --- .dockerignore | 8 ------- Dockerfile | 28 +++++++++++++++------- docker/rootfs/etc/services.d/go2rtc/finish | 12 ---------- docker/rootfs/etc/services.d/go2rtc/run | 23 ------------------ docker/run.sh | 20 ++++++++++++++++ 5 files changed, 39 insertions(+), 52 deletions(-) delete mode 100644 .dockerignore delete mode 100755 docker/rootfs/etc/services.d/go2rtc/finish delete mode 100755 docker/rootfs/etc/services.d/go2rtc/run create mode 100755 docker/run.sh diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index b03cbe4b..00000000 --- a/.dockerignore +++ /dev/null @@ -1,8 +0,0 @@ -.git/ -assets/ -examples/ - -.dockerignore -config.yaml -Dockerfile -README.md diff --git a/Dockerfile b/Dockerfile index 25346a0b..ffb0aca7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,12 @@ -# https://github.com/hassio-addons/addon-base-python/releases -ARG BASE_VERSION="9.0.1" +# https://hub.docker.com/_/python/tags?page=1&name=-alpine +ARG PYTHON_VERSION="3.10.8" # https://hub.docker.com/_/golang/tags?page=1&name=-alpine -ARG GO_VERSION="1.19.2" +ARG GO_VERSION="1.19.3" # https://hub.docker.com/r/ngrok/ngrok/tags?page=1&name=-alpine ARG NGROK_VERSION="3.1.0" -FROM ghcr.io/hassio-addons/base-python:${BASE_VERSION} AS base +FROM python:${PYTHON_VERSION}-alpine AS base FROM golang:${GO_VERSION}-alpine AS go @@ -20,8 +20,15 @@ FROM go AS build WORKDIR /workspace -COPY . . +# Cache dependencies +COPY go.mod go.sum ./ +RUN go mod download +# Build binary +COPY cmd cmd +COPY pkg pkg +COPY www www +COPY main.go . RUN CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath @@ -30,13 +37,16 @@ FROM scratch AS rootfs COPY --from=build /workspace/go2rtc /usr/local/bin/ COPY --from=ngrok /bin/ngrok /usr/local/bin/ -COPY ./docker/rootfs/ / +COPY ./docker/run.sh /run.sh -# Final stage +# Final image FROM base -# Install ffmpeg -RUN apk add --no-cache ffmpeg +RUN apk add --no-cache bash tini ffmpeg COPY --from=rootfs / / + +ENTRYPOINT ["/sbin/tini", "--"] + +CMD ["/run.sh"] diff --git a/docker/rootfs/etc/services.d/go2rtc/finish b/docker/rootfs/etc/services.d/go2rtc/finish deleted file mode 100755 index f73fe4ba..00000000 --- a/docker/rootfs/etc/services.d/go2rtc/finish +++ /dev/null @@ -1,12 +0,0 @@ -#!/command/with-contenv bashio -# ============================================================================== -# Take down the S6 supervision tree when go2rtc fails -# ============================================================================== -# shellcheck shell=bash - -if [[ "${1}" -ne 0 ]] && [[ "${1}" -ne 256 ]]; then - bashio::log.warning "go2rtc crashed, halting add-on" - /run/s6/basedir/bin/halt -fi - -bashio::log.info "go2rtc stopped, restarting..." diff --git a/docker/rootfs/etc/services.d/go2rtc/run b/docker/rootfs/etc/services.d/go2rtc/run deleted file mode 100755 index 99fd8fc2..00000000 --- a/docker/rootfs/etc/services.d/go2rtc/run +++ /dev/null @@ -1,23 +0,0 @@ -#!/command/with-contenv bashio -# ============================================================================== -# Runs go2rtc -# ============================================================================== -# shellcheck shell=bash - -bashio::log.info 'Starting go2rtc...' - -declare config_path -declare binary_path - -config_path="/config" - -binary_path="/usr/local/bin/go2rtc" -if [[ -x "${config_path}/go2rtc" ]]; then - binary_path="${config_path}/go2rtc" - bashio::log.warning "Using go2rtc binary from '${binary_path}'" -fi - -# set cwd for go2rtc (for config file, Hass integration, etc) -cd "${config_path}" || bashio::exit.nok "Could not change working directory" - -exec "${binary_path}" diff --git a/docker/run.sh b/docker/run.sh new file mode 100755 index 00000000..93cbf082 --- /dev/null +++ b/docker/run.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# ============================================================================== +# Runs go2rtc +# ============================================================================== + +echo 'Starting go2rtc...' >&2 + +readonly config_path="/config" + +if [[ -x "${config_path}/go2rtc" ]]; then + readonly binary_path="${config_path}/go2rtc" + echo "Using go2rtc binary from '${binary_path}' instead of the embedded one" >&2 +else + readonly binary_path="/usr/local/bin/go2rtc" +fi + +# set cwd for go2rtc (for config file, Hass integration, etc) +cd "${config_path}" || echo "Could not change working directory to '${config_path}'" >&2 + +exec "${binary_path}" From 72823af9d0160b913215eef740c254e05dbe27ab Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Fri, 18 Nov 2022 15:05:42 -0300 Subject: [PATCH 04/10] Refactor docker workflow --- .github/workflows/builder.yml | 60 ----------------------------------- .github/workflows/ci.yml | 50 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 60 deletions(-) delete mode 100644 .github/workflows/builder.yml create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/builder.yml b/.github/workflows/builder.yml deleted file mode 100644 index 95b8b10d..00000000 --- a/.github/workflows/builder.yml +++ /dev/null @@ -1,60 +0,0 @@ -# https://github.com/home-assistant/builder -name: 'Builder' - -on: - push: - tags: [ 'v*' ] - workflow_dispatch: - -jobs: - hassio: - name: Hassio Addon - runs-on: ubuntu-latest - steps: - - name: Checkout the repository - uses: actions/checkout@v3 - - - name: Login to DockerHub - uses: docker/login-action@v2 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Branch name - run: | - VERSION="${GITHUB_REF#refs/tags/v}" - VERSION="${VERSION#refs/heads/}" - echo "REPO=alexxit/go2rtc" >> $GITHUB_ENV - echo "TAG=${VERSION}" >> $GITHUB_ENV - echo "IMAGE=alexxit/go2rtc:${VERSION}" >> $GITHUB_ENV - - - name: Build amd64 - uses: home-assistant/builder@master - with: - args: --amd64 --target . --version $TAG-amd64 --no-latest --docker-hub-check - - - name: Build i386 - uses: home-assistant/builder@master - with: - args: --i386 --target . --version $TAG-i386 --no-latest --docker-hub-check - - - name: Build aarch64 - uses: home-assistant/builder@master - with: - args: --aarch64 --target . --version $TAG-aarch64 --no-latest --docker-hub-check - - - name: Build armv7 - uses: home-assistant/builder@master - with: - args: --armv7 --target . --version $TAG-armv7 --no-latest --docker-hub-check - - - name: Docker manifest - run: | - # thanks to https://github.com/aler9/rtsp-simple-server/blob/main/Makefile - docker manifest create "${IMAGE}" \ - "${IMAGE}-amd64" "${IMAGE}-i386" "${IMAGE}-aarch64" "${IMAGE}-armv7" - docker manifest push "${IMAGE}" - - docker manifest create "${REPO}:latest" \ - "${IMAGE}-amd64" "${IMAGE}-i386" "${IMAGE}-aarch64" "${IMAGE}-armv7" - docker manifest push "${REPO}:latest" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..e6f9d181 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,50 @@ +name: ci + +on: + workflow_dispatch: + push: + branches: + - 'master' + tags: + - 'v*' + pull_request: + branches: + - 'master' + +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v4 + with: + images: alexxit/go2rtc + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to DockerHub + if: github.event_name != 'pull_request' + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and push + uses: docker/build-push-action@v3 + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} From f120db71a376cff67da1d02392388a1609707616 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Fri, 18 Nov 2022 15:16:21 -0300 Subject: [PATCH 05/10] Add all platforms --- .github/workflows/ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e6f9d181..6aba9810 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,6 +45,12 @@ jobs: uses: docker/build-push-action@v3 with: context: . + platforms: | + linux/amd64 + linux/386 + linux/arm/v6 + linux/arm/v7 + linux/arm64/v8 push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} From 6f48131e4dfeba4c1b4e0c62a16b316d42e88019 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Fri, 18 Nov 2022 15:20:04 -0300 Subject: [PATCH 06/10] Remove armv6 which was never supported --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6aba9810..de602701 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -48,7 +48,6 @@ jobs: platforms: | linux/amd64 linux/386 - linux/arm/v6 linux/arm/v7 linux/arm64/v8 push: ${{ github.event_name != 'pull_request' }} From 6feb63552207a8112f74460d1caab6dc128da72c Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Fri, 18 Nov 2022 15:22:33 -0300 Subject: [PATCH 07/10] Delete config.yaml not used anymore --- config.yaml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 config.yaml diff --git a/config.yaml b/config.yaml deleted file mode 100644 index 2447a06b..00000000 --- a/config.yaml +++ /dev/null @@ -1,6 +0,0 @@ -# https://github.com/home-assistant/builder/blob/master/builder.sh -name: go2rtc -description: Ultimate camera streaming application -url: https://github.com/AlexxIT/go2rtc -image: alexxit/go2rtc -arch: [ amd64, aarch64, i386, armv7 ] \ No newline at end of file From 270fc7c1b6b622cade2b37e21becaff4f06f938c Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Fri, 18 Nov 2022 15:26:02 -0300 Subject: [PATCH 08/10] Allow to run container without mounting /config --- Dockerfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Dockerfile b/Dockerfile index ffb0aca7..1053d593 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,11 +31,14 @@ COPY www www COPY main.go . RUN CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath +RUN mkdir -p /config # Collect all files FROM scratch AS rootfs COPY --from=build /workspace/go2rtc /usr/local/bin/ +# Ensure an empty /config folder exists so that the container can be run without a volume +COPY --from=build /config /config COPY --from=ngrok /bin/ngrok /usr/local/bin/ COPY ./docker/run.sh /run.sh From 46a278c067c2615b2a99897e905edf277ce8e520 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Fri, 18 Nov 2022 15:27:38 -0300 Subject: [PATCH 09/10] Add curl and exit on error run.sh --- Dockerfile | 2 +- docker/run.sh | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 1053d593..608e22bf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -46,7 +46,7 @@ COPY ./docker/run.sh /run.sh # Final image FROM base -RUN apk add --no-cache bash tini ffmpeg +RUN apk add --no-cache bash tini curl ffmpeg COPY --from=rootfs / / diff --git a/docker/run.sh b/docker/run.sh index 93cbf082..f9211d17 100755 --- a/docker/run.sh +++ b/docker/run.sh @@ -3,6 +3,8 @@ # Runs go2rtc # ============================================================================== +set -euo pipefail + echo 'Starting go2rtc...' >&2 readonly config_path="/config" From 562872beb8ddd0d20968e49cc55a05590aac7692 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Fri, 18 Nov 2022 15:29:47 -0300 Subject: [PATCH 10/10] Add jq --- Dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 608e22bf..aa148243 100644 --- a/Dockerfile +++ b/Dockerfile @@ -46,7 +46,9 @@ COPY ./docker/run.sh /run.sh # Final image FROM base -RUN apk add --no-cache bash tini curl ffmpeg +# Install ffmpeg, bash (for run.sh), tini (for signal handling), +# and other common tools for the echo source. +RUN apk add --no-cache ffmpeg bash tini curl jq COPY --from=rootfs / /