Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow yml and yaml files #107

Merged
merged 7 commits into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ENV \

ARG BUILD_ARCH
ARG VCN_VERSION
ARG YQ_VERSION
ludeeus marked this conversation as resolved.
Show resolved Hide resolved

RUN \
set -x \
Expand Down Expand Up @@ -40,6 +41,19 @@ RUN \
\
&& rm -rf /root/go /root/.cache \
&& mv vcn /usr/bin/vcn \
&& if [ "${BUILD_ARCH}" = "armhf" ] || [ "${BUILD_ARCH}" = "armv7" ]; then \
wget -q -O /usr/bin/yq https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_arm; \
elif [ "${BUILD_ARCH}" = "aarch64" ]; then \
wget -q -O /usr/bin/yq https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_arm64; \
elif [ "${BUILD_ARCH}" = "i386" ]; then \
wget -q -O /usr/bin/yq https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_386; \
elif [ "${BUILD_ARCH}" = "amd64" ]; then \
wget -q -O /usr/bin/yq https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_amd64; \
else \
exit 1; \
fi \
&& chmod +x /usr/bin/yq \
&& yq --help \
ludeeus marked this conversation as resolved.
Show resolved Hide resolved
\
&& apk del .build-dependencies \
&& rm -rf /usr/src/vcn
Expand Down
18 changes: 0 additions & 18 deletions build.json

This file was deleted.

14 changes: 14 additions & 0 deletions build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
image: "homeassistant/{arch}-builder"
shadow_repository: "ghcr.io/home-assistant"
build_from:
aarch64: "ghcr.io/home-assistant/aarch64-base:3.13"
armv7: "ghcr.io/home-assistant/armv7-base:3.13"
armhf: "ghcr.io/home-assistant/armhf-base:3.13"
amd64: "ghcr.io/home-assistant/amd64-base:3.13"
i386: "ghcr.io/home-assistant/i386-base:3.13"
args:
VCN_VERSION: "0.9.6"
YQ_VERSION: "v4.13.2"
labels:
io.hass.type: builder
org.opencontainers.image.source: "https://github.com/home-assistant/builder"
107 changes: 68 additions & 39 deletions builder.sh
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ function run_build() {
bashio::log.info "Generate repository shadow images"
docker tag "${repository}/${image}:${version}" "${shadow_repository}/${image}:${version}"
for tag_image in "${docker_tags[@]}"; do
bashio::log.info "Create shadow-image tag: ${tag_image}"
bashio::log.info "Create shadow-image tag: ${shadow_repository}/${image}:${tag_image}"
docker tag "${repository}/${image}:${version}" "${shadow_repository}/${image}:${tag_image}"
push_images+=("${shadow_repository}/${image}:${tag_image}")
done
Expand Down Expand Up @@ -339,6 +339,28 @@ function run_build() {
fi
}

function convert_to_json() {
# Convert [build|config].[yml|yaml] to json in a temp directory
for file in config build; do
if bashio::fs.file_exists "${TARGET}/${file}.yml"; then
yq e -N -M -o=json "${TARGET}/${file}.yml" > "/tmp/build_config/${file}.json"
elif bashio::fs.file_exists "${TARGET}/${file}.yaml"; then
yq e -N -M -o=json "${TARGET}/${file}.yaml" > "/tmp/build_config/${file}.json"
fi
done
}

function copy_config_tmp() {
# Copy [build|config].json to a temp directory
mkdir -p /tmp/build_config
for file in config build; do
if bashio::fs.file_exists "${TARGET}/${file}.json"; then
cp "${TARGET}/${file}.json" "/tmp/build_config/${file}.json"
fi
done
}



#### Build functions ####

Expand All @@ -356,13 +378,14 @@ function build_base() {
local docker_tags=()

# Read build.json
if bashio::fs.file_exists "${TARGET}/build.json"; then
build_from="$(jq --raw-output ".build_from.${build_arch} // empty" "${TARGET}/build.json")"
args="$(jq --raw-output '.args // empty | keys[]' "${TARGET}/build.json")"
labels="$(jq --raw-output '.labels // empty | keys[]' "${TARGET}/build.json")"
raw_image="$(jq --raw-output '.image // empty' "${TARGET}/build.json")"
version_tag="$(jq --raw-output '.version_tag // false' "${TARGET}/build.json")"
shadow_repository="$(jq --raw-output '.shadow_repository // empty' "${TARGET}/build.json")"
if bashio::fs.file_exists "/tmp/build_config/build.json"; then
build_from="$(jq --raw-output ".build_from.${build_arch} // empty" "/tmp/build_config/build.json")"
args="$(jq --raw-output '.args // empty | keys[]' "/tmp/build_config/build.json")"
labels="$(jq --raw-output '.labels // empty | keys[]' "/tmp/build_config/build.json")"
raw_image="$(jq --raw-output '.image // empty' "/tmp/build_config/build.json")"
version_tag="$(jq --raw-output '.version_tag // false' "/tmp/build_config/build.json")"
shadow_repository="$(jq --raw-output '.shadow_repository // empty' "/tmp/build_config/build.json")"

fi

# Set defaults build things
Expand Down Expand Up @@ -392,15 +415,15 @@ function build_base() {
# Additional build args
if bashio::var.has_value "${args}"; then
for arg in ${args}; do
value="$(jq --raw-output ".args.${arg}" "${TARGET}/build.json")"
value="$(jq --raw-output ".args.${arg}" "/tmp/build_config/build.json")"
docker_cli+=("--build-arg" "${arg}=${value}")
done
fi

# Additional build labels
if bashio::var.has_value "${labels}"; then
for label in ${labels}; do
value="$(jq --raw-output ".labels.\"${label}\"" "${TARGET}/build.json")"
value="$(jq --raw-output ".labels.\"${label}\"" "/tmp/build_config/build.json")"
docker_cli+=("--label" "${label}=${value}")
done
fi
Expand Down Expand Up @@ -439,10 +462,10 @@ function build_addon() {
local docker_tags=()

# Read addon build.json
if bashio::fs.file_exists "$TARGET/build.json"; then
build_from="$(jq --raw-output ".build_from.$build_arch // empty" "$TARGET/build.json")"
args="$(jq --raw-output '.args // empty | keys[]' "$TARGET/build.json")"
shadow_repository="$(jq --raw-output '.shadow_repository // empty' "${TARGET}/build.json")"
if bashio::fs.file_exists "/tmp/build_config/build.json"; then
build_from="$(jq --raw-output ".build_from.$build_arch // empty" "/tmp/build_config/build.json")"
args="$(jq --raw-output '.args // empty | keys[]' "/tmp/build_config/build.json")"
shadow_repository="$(jq --raw-output '.shadow_repository // empty' "/tmp/build_config/build.json")"
fi

# Set defaults build things
Expand All @@ -453,23 +476,23 @@ function build_addon() {
# Additional build args
if [ -n "$args" ]; then
for arg in $args; do
value="$(jq --raw-output ".args.$arg" "$TARGET/build.json")"
value="$(jq --raw-output ".args.$arg" "/tmp/build_config/build.json")"
docker_cli+=("--build-arg" "$arg=$value")
done
fi

# Read addon config.json
name="$(jq --raw-output '.name // empty' "$TARGET/config.json" | sed "s/'//g")"
description="$(jq --raw-output '.description // empty' "$TARGET/config.json" | sed "s/'//g")"
url="$(jq --raw-output '.url // empty' "$TARGET/config.json")"
raw_image="$(jq --raw-output '.image // empty' "$TARGET/config.json")"
mapfile -t supported_arch < <(jq --raw-output '.arch // empty' "$TARGET/config.json")
name="$(jq --raw-output '.name // empty' "/tmp/build_config/config.json" | sed "s/'//g")"
description="$(jq --raw-output '.description // empty' "/tmp/build_config/config.json" | sed "s/'//g")"
url="$(jq --raw-output '.url // empty' "/tmp/build_config/config.json")"
raw_image="$(jq --raw-output '.image // empty' "/tmp/build_config/config.json")"
mapfile -t supported_arch < <(jq --raw-output '.arch // empty' "/tmp/build_config/config.json")

# Read version from config.json when VERSION is not set
if [ -n "$VERSION" ]; then
version="$VERSION"
else
version="$(jq --raw-output '.version' "$TARGET/config.json")"
version="$(jq --raw-output '.version' "/tmp/build_config/config.json")"
fi

# Check arch
Expand Down Expand Up @@ -513,13 +536,13 @@ function build_generic() {
local docker_tags=()

# Read build.json
if bashio::fs.file_exists "$TARGET/build.json"; then
build_from="$(jq --raw-output ".build_from.$build_arch // empty" "$TARGET/build.json")"
args="$(jq --raw-output '.args // empty | keys[]' "$TARGET/build.json")"
labels="$(jq --raw-output '.labels // empty | keys[]' "$TARGET/build.json")"
raw_image="$(jq --raw-output '.image // empty' "$TARGET/build.json")"
version_tag="$(jq --raw-output '.version_tag // false' "$TARGET/build.json")"
shadow_repository="$(jq --raw-output '.shadow_repository // empty' "${TARGET}/build.json")"
if bashio::fs.file_exists "/tmp/build_config/build.json"; then
build_from="$(jq --raw-output ".build_from.$build_arch // empty" "/tmp/build_config/build.json")"
args="$(jq --raw-output '.args // empty | keys[]' "/tmp/build_config/build.json")"
labels="$(jq --raw-output '.labels // empty | keys[]' "/tmp/build_config/build.json")"
raw_image="$(jq --raw-output '.image // empty' "/tmp/build_config/build.json")"
version_tag="$(jq --raw-output '.version_tag // false' "/tmp/build_config/build.json")"
shadow_repository="$(jq --raw-output '.shadow_repository // empty' "/tmp/build_config/build.json")"
fi

# Set defaults build things
Expand All @@ -539,15 +562,15 @@ function build_generic() {
# Additional build args
if bashio::var.has_value "$args"; then
for arg in $args; do
value="$(jq --raw-output ".args.$arg" "$TARGET/build.json")"
value="$(jq --raw-output ".args.$arg" "/tmp/build_config/build.json")"
docker_cli+=("--build-arg" "$arg=$value")
done
fi

# Additional build labels
if bashio::var.has_value "$labels"; then
for label in $labels; do
value="$(jq --raw-output ".labels.\"$label\"" "$TARGET/build.json")"
value="$(jq --raw-output ".labels.\"$label\"" "/tmp/build_config/build.json")"
docker_cli+=("--label" "$label=$value")
done
fi
Expand Down Expand Up @@ -584,13 +607,13 @@ function build_machine() {
local docker_tags=()

# Read build.json
if bashio::fs.file_exists "${TARGET}/build.json"; then
build_from="$(jq --raw-output ".build_from.${build_arch} // empty" "${TARGET}/build.json")"
args="$(jq --raw-output '.args // empty | keys[]' "${TARGET}/build.json")"
labels="$(jq --raw-output '.labels // empty | keys[]' "${TARGET}/build.json")"
raw_image="$(jq --raw-output '.image // empty' "${TARGET}/build.json")"
version_tag="$(jq --raw-output '.version_tag // false' "${TARGET}/build.json")"
shadow_repository="$(jq --raw-output '.shadow_repository // empty' "${TARGET}/build.json")"
if bashio::fs.file_exists "/tmp/build_config/build.json"; then
build_from="$(jq --raw-output ".build_from.${build_arch} // empty" "/tmp/build_config/build.json")"
args="$(jq --raw-output '.args // empty | keys[]' "/tmp/build_config/build.json")"
labels="$(jq --raw-output '.labels // empty | keys[]' "/tmp/build_config/build.json")"
raw_image="$(jq --raw-output '.image // empty' "/tmp/build_config/build.json")"
version_tag="$(jq --raw-output '.version_tag // false' "/tmp/build_config/build.json")"
shadow_repository="$(jq --raw-output '.shadow_repository // empty' "/tmp/build_config/build.json")"
fi

# Modify build_from
Expand All @@ -614,15 +637,15 @@ function build_machine() {
# Additional build args
if bashio::var.has_value "${args}"; then
for arg in ${args}; do
value="$(jq --raw-output ".args.${arg}" "${TARGET}/build.json")"
value="$(jq --raw-output ".args.${arg}" "/tmp/build_config/build.json")"
docker_cli+=("--build-arg" "${arg}=${value}")
done
fi

# Additional build labels
if bashio::var.has_value "${labels}"; then
for label in ${labels}; do
value="$(jq --raw-output ".labels.\"${label}\"" "${TARGET}/build.json")"
value="$(jq --raw-output ".labels.\"${label}\"" "/tmp/build_config/build.json")"
docker_cli+=("--label" "${label}=${value}")
done
fi
Expand Down Expand Up @@ -904,6 +927,12 @@ mkdir -p /data
init_crosscompile
start_docker

# Convert configuration files to json if needed
convert_to_json

# Copy configuration files to tmp
copy_config_tmp

# Login into dockerhub & setup CodeNotary
if [ -n "$DOCKER_USER" ] && [ -n "$DOCKER_PASSWORD" ]; then
docker login -u "$DOCKER_USER" -p "$DOCKER_PASSWORD"
Expand Down