#!/bin/bash
# hooks/build
# https://docs.docker.com/docker-cloud/builds/advanced/

# $IMAGE_NAME var is injected into the build so the tag is correct.
echo "[***] Build hook running"

VERSION=$(git ls-remote --tags -q https://github.com/rspamd/rspamd | sed -n "s/^[[:xdigit:]]\{40\}[[:blank:]]refs\/tags\/\([0-9]\{1\}\.[0-9]\{1,2\}\($\|\.[0-9]\{1,2\}$\)\)/\1/p" | sort --version-sort | tail -1)

IMAGE_NAME=docker-rspamd

docker build \
  --no-cache \
  --build-arg VERSION=${VERSION} \
  --build-arg COMMIT=$(git ls-remote --tags -q https://github.com/rspamd/rspamd | sed -n "s/^\([[:xdigit:]]\{40\}\)[[:blank:]]refs\/tags\/${VERSION}^{}$/\1/p" |  xargs git rev-parse --short) \
  --build-arg BRANCH=$(git rev-parse --abbrev-ref HEAD) \
  --build-arg DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ") \
  -t ${IMAGE_NAME} .