#!/usr/bin/env bash set -euo pipefail image="${1:-fingerprint-chromium}" release_api="https://api.github.com/repos/adryfish/fingerprint-chromium/releases" oci_revision="${OCI_REVISION:-$(git rev-parse HEAD)}" oci_source="${OCI_SOURCE:-https://git.ipao.vip/rogee/fingerprint-chromium}" if [[ -n "${CHROME_VERSION:-}" ]]; then release_url="$release_api/tags/$CHROME_VERSION" else release_url="$release_api/latest" fi release="$(curl --fail --location --retry 3 --silent --show-error "$release_url")" version="$(jq -er '.tag_name | select(test("^[0-9]+(\\.[0-9]+){3}$"))' <<<"$release")" asset_url="$(jq -er --arg version "$version" ' [.assets[] | select(.name | startswith("ungoogled-chromium-\($version)-") and endswith("-x86_64_linux.tar.xz"))] | if length == 1 then .[0].browser_download_url else error("expected exactly one Linux x86_64 archive") end ' <<<"$release")" tag="$image:$version" if [[ "${PUSH:-false}" == "true" ]] && docker manifest inspect "$tag" >/dev/null 2>&1; then printf 'Image %s already exists; skipping build and push\n' "$tag" exit 0 fi docker build --pull --platform linux/amd64 \ --build-arg "CHROME_VERSION=$version" \ --build-arg "CHROME_URL=$asset_url" \ --build-arg "OCI_REVISION=$oci_revision" \ --build-arg "OCI_SOURCE=$oci_source" \ --tag "$tag" . if [[ "${PUSH:-false}" == "true" ]]; then docker push "$tag" fi printf 'Built %s\n' "$tag"