From 2e11715194128fc4ff67dce672a6f7390828a940 Mon Sep 17 00:00:00 2001 From: djmil Date: Sun, 25 Aug 2024 17:47:32 +0200 Subject: [PATCH] Build release -vs- local sources This commit closes #5 ./docker/build.sh # build local sources ./docker/build.sh -r 0.1.0 -p # build known release --- Dockerfile | 16 ------- docker/build.sh | 98 +++++++++++++++++++++++++++++++++++++++ docker/local.dockerfile | 24 ++++++++++ docker/release.dockerfile | 19 ++++++++ make-release.sh | 39 ---------------- 5 files changed, 141 insertions(+), 55 deletions(-) delete mode 100644 Dockerfile create mode 100755 docker/build.sh create mode 100644 docker/local.dockerfile create mode 100644 docker/release.dockerfile delete mode 100755 make-release.sh diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 6d81b80..0000000 --- a/Dockerfile +++ /dev/null @@ -1,16 +0,0 @@ -FROM caddy:2.8.4-builder-alpine AS builder - -COPY pkg/gitea/* /module/gitea-pages/pkg/gitea/ -COPY gitea-pages.go go.mod go.sum /module/gitea-pages/ - -RUN xcaddy build \ - --with gitea.djmil.dev/djmil/gitea-pages \ - --replace gitea.djmil.dev/djmil/gitea-pages=/module/gitea-pages \ - --replace gitea.djmil.dev/djmil/gitea-pages/pkg/gitea=/module/gitea-pages/pkg/gitea - -FROM caddy:2.8.4 - -COPY --from=builder /usr/bin/caddy /usr/bin/caddy -COPY Caddyfile /etc/caddy/Caddyfile - -EXPOSE 3003/tcp diff --git a/docker/build.sh b/docker/build.sh new file mode 100755 index 0000000..bb922bd --- /dev/null +++ b/docker/build.sh @@ -0,0 +1,98 @@ +#!/bin/bash +set -e + +usage="$(basename "$0") [--release=] [--publish] [-h] + +Compile Caddy server with gitea-pages plugin inside docker container. Buy +default local sources will be used for build. + +You can also compile known release. The given release tag will be fetched from +the project git repo at https://gitea.djmil.dev/djmil/gitea-pages. + +Optionally, the script can be used to publish resulting docker image to the +https://hub.docker.com/repository/docker/djmil/gitea-pages. + +Params for the short options version shall be separated by space. For the long +options - equal sign shall be used. Example: + --release=0.1.0 + -r 0.0.1 + +Where: + -l | --local + Build local sources. Default behaviour. + + -r | --release + A major.minor.patch tag of the known release to build. + + -p | --publish + Publish resulting image to the docker hub. Typically shall be used + together with --release option. + + -h | --help + Show this message and exit. +" + +R="\033[0;31m" # RED +G="\033[0;32m" # GREEN +Y="\033[0;33m" # YELLOW +B="\033[1;34m" # BLUE +NC="\033[0;0m" # NO COLOR + +############################################################################### +# Parse long and short options, source: https://stackoverflow.com/a/28466267 + +# Defaults +tag='local' +publish=false + +die() { echo "$*" >&2; exit 2; } # complain to STDERR and exit with error +needs_arg() { if [ -z "$OPTARG" ]; then die "No arg for --$OPT option"; fi; } + +while getopts r:lph-: OPT; do # allow -a, -b with arg, -c, and -- "with arg" + # support long options: https://stackoverflow.com/a/28466267/519360 + if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG + OPT="${OPTARG%%=*}" # extract long option name + OPTARG="${OPTARG#"$OPT"}" # extract long option argument (may be empty) + OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=` + fi + case "$OPT" in + l | local ) tag='local' ;; + r | release ) needs_arg; tag="$OPTARG" ;; + p | publish ) publish=true ;; + h | help ) echo "$usage" >&2; exit 0 ;; + \? ) exit 2 ;; # bad short option (error reported via getopts) + * ) die "Illegal option --$OPT" ;; # bad long option + esac +done +shift $((OPTIND-1)) # remove parsed options and args from $@ list +############################################################################### + + +if [ $tag = 'local' ]; then + echo -e "${Y} -->> Local dev-mode build: compile local sources${NC}"; + + docker build \ + --file docker/local.dockerfile \ + --tag djmil/gitea-pages:local \ + . + + exit 0 +fi + + +echo -e "${Y} -->> Building release ${B}'$tag'${NC}"; + +docker build \ + --file docker/release.dockerfile \ + --build-arg RELEASE=$tag \ + --tag djmil/gitea-pages:$tag \ + . + + +if $publish; then + echo -e "${Y} -->> Publishing to the dockerhub"; + + docker login --username djmil #--password-stdin + + docker push djmil/gitea-pages:$tag +fi diff --git a/docker/local.dockerfile b/docker/local.dockerfile new file mode 100644 index 0000000..2ec5089 --- /dev/null +++ b/docker/local.dockerfile @@ -0,0 +1,24 @@ +FROM caddy:2.8.4-builder-alpine AS builder + +# +# mount root of the build context as /module/gitea-pages +# inside the container and run `xcaddy build` command +# also use cache to speed up subsequent builds +# +# $> go env GOCACHE <<-- Golang build cache +# $> go env GOMODCACHE <<-- Goland module download cache +# +RUN --mount=type=bind,target=/module/gitea-pages \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + xcaddy build \ + --with gitea.djmil.dev/djmil/gitea-pages \ + --replace gitea.djmil.dev/djmil/gitea-pages=/module/gitea-pages \ + --replace gitea.djmil.dev/djmil/gitea-pages/pkg/gitea=/module/gitea-pages/pkg/gitea + +FROM caddy:2.8.4 + +COPY --from=builder /usr/bin/caddy /usr/bin/caddy +COPY Caddyfile /etc/caddy/Caddyfile + +EXPOSE 3003/tcp diff --git a/docker/release.dockerfile b/docker/release.dockerfile new file mode 100644 index 0000000..2758437 --- /dev/null +++ b/docker/release.dockerfile @@ -0,0 +1,19 @@ +FROM caddy:2.8.4-builder-alpine AS builder + +ARG RELEASE + +# +# $> go env GOCACHE <<-- Golang build cache +# $> go env GOMODCACHE <<-- Goland module download cache +# +RUN --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + xcaddy build \ + --with gitea.djmil.dev/djmil/gitea-pages@$RELEASE + +FROM caddy:2.8.4 + +COPY --from=builder /usr/bin/caddy /usr/bin/caddy +COPY Caddyfile /etc/caddy/Caddyfile + +EXPOSE 3003/tcp diff --git a/make-release.sh b/make-release.sh deleted file mode 100755 index f5e225c..0000000 --- a/make-release.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash -set -e - -usage="$(basename "$0") [-h] [-t RELEASE_VERSION ] - -The script compiles Caddy server with GiteaPages plugin, and publishes the resulting image -to the https://hub.docker.com/u/djmil - -Where: - -t RELEASE_VERSION - A major.minor.patch tag for the image being publisdhed. -" - -tag='' - -while getopts ht: flag -do - case "${flag}" in - t) tag=${OPTARG};; - h) echo "$usage"; exit 0;; - *) echo "$usage" >&2; exit 1;; - esac -done - -if [ ! "$tag" ]; then - echo 'Missing: -t RELEASE_VERSION' >&2 - exit 1 -fi - -echo "Tag: $tag"; - -docker build --tag djmil/gitea-pages:$tag . - -#git tag -#git push tag - -docker login --username djmil #--password-stdin - -docker push djmil/gitea-pages:$tag