# syntax=docker/dockerfile:1
# custom-gitea-runner — self-owned Gitea Actions runner
# All dependencies installed at build time; no runtime apt-get.

ARG UBUNTU_VERSION=22.04
FROM ubuntu:${UBUNTU_VERSION}

ARG ACT_RUNNER_VERSION=0.2.11
ARG NODE_MAJOR=20
ARG BUILD_DATE
ARG VCS_REF
ARG VERSION=dev

LABEL org.opencontainers.image.title="custom-gitea-runner" \
      org.opencontainers.image.description="Self-owned Gitea Actions runner with bundled tooling" \
      org.opencontainers.image.version="${VERSION}" \
      org.opencontainers.image.created="${BUILD_DATE}" \
      org.opencontainers.image.revision="${VCS_REF}"

ENV DEBIAN_FRONTEND=noninteractive \
    IMAGE_VERSION="${VERSION}" \
    GIT_REVISION="${VCS_REF}" \
    RUNNER_CONFIG_PATH=/config/runner.yaml \
    RUNNER_WORKDIR=/work

# ─── Base packages ────────────────────────────────────────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
    bash \
    ca-certificates \
    curl \
    git \
    gnupg \
    jq \
    tini \
    tzdata \
    && rm -rf /var/lib/apt/lists/*

# ─── Node.js (NodeSource — Ubuntu 22.04 default is 12.x, too old) ─────────────
RUN curl -fsSL https://deb.nodesource.com/setup_${NODE_MAJOR}.x | bash - \
    && apt-get install -y --no-install-recommends nodejs \
    && rm -rf /var/lib/apt/lists/*

# ─── Docker CLI ───────────────────────────────────────────────────────────────
RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
      | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg \
    && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
        https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" \
      > /etc/apt/sources.list.d/docker.list \
    && apt-get update \
    && apt-get install -y --no-install-recommends docker-ce-cli \
    && rm -rf /var/lib/apt/lists/*

# ─── act_runner binary (pinned) ───────────────────────────────────────────────
# To update: bump ACT_RUNNER_VERSION in the build command or here.
# Releases: https://gitea.com/gitea/act_runner/releases
RUN ARCH=$(dpkg --print-architecture) \
    && curl -fsSL \
        "https://gitea.com/gitea/act_runner/releases/download/v${ACT_RUNNER_VERSION}/act_runner-${ACT_RUNNER_VERSION}-linux-${ARCH}" \
        -o /usr/local/bin/act_runner \
    && chmod +x /usr/local/bin/act_runner

# ─── Runtime directories and scripts ─────────────────────────────────────────
RUN mkdir -p /config /work /config/trusted-ca

COPY docker/runner/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
COPY docker/runner/healthcheck.sh       /usr/local/bin/healthcheck.sh
COPY docker/runner/smoke-test.sh        /usr/local/bin/smoke-test.sh

RUN chmod +x \
    /usr/local/bin/docker-entrypoint.sh \
    /usr/local/bin/healthcheck.sh \
    /usr/local/bin/smoke-test.sh

VOLUME ["/config", "/work"]

HEALTHCHECK \
    --interval=30s \
    --timeout=10s \
    --start-period=60s \
    --retries=3 \
    CMD ["/usr/local/bin/healthcheck.sh"]

ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/docker-entrypoint.sh"]
