From 2236aa2d39c5dab80b3298511b9b0dd90a178abe Mon Sep 17 00:00:00 2001 From: Fl_GUI Date: Sun, 13 Apr 2025 18:30:19 +0200 Subject: [PATCH] add deploy script --- Containerfile | 1 - compose.yaml | 2 +- deploy.sh | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 2 +- 4 files changed, 99 insertions(+), 3 deletions(-) create mode 100755 deploy.sh diff --git a/Containerfile b/Containerfile index 09bd2b5..a0e70ad 100644 --- a/Containerfile +++ b/Containerfile @@ -6,7 +6,6 @@ COPY *.go . RUN go mod tidy RUN go install -ldflags="-s" -RUN pwd # run image FROM scratch as run diff --git a/compose.yaml b/compose.yaml index 2fb2a45..9626b14 100644 --- a/compose.yaml +++ b/compose.yaml @@ -3,7 +3,7 @@ services: yampl: image: yampl container_name: yampl - image: containers.openfl.eu/local/yampl:dev + image: ${IMAGE:-localhost/yampl:latest} build: dockerfile: Containerfile ports: diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 0000000..ac4c8a6 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,97 @@ +#!/usr/bin/bash +# build the image and push to container registry + +# helpers + +section="" ## Formatted as +function echoSection() { + if [ -z "$1" ] ; then + echo"" + echo "--- $0: $section ---" + else + echo "--- $0: $section $1 ---" + fi +} + +## intro +section="Printing Info" +echoSection "" +echo "Deploying yampl, a toml to html site generator." +echo "commit id: $(git rev-list --max-count=1 HEAD)" +echo "go version: $(go version)" +echo "podman version: $(podman --version)" +echo "buildah version: $(buildah --version)" + +echoSection "end" + + +## check codebase +section="Checking Codebase" +echoSection "" + +set -x +fmtout=$(go run cmd/gofmt -e -l -d .) +set +x +# TODO go fix +# TODO go vet +# TODO golangcli lint +if [ -n "$fmtout" ] ; then + echo "$fmtout" + echoSection "failure" + exit -1 +fi + +echoSection "success" + +## build Containerfile +section="Building Containerfile" +echoSection "" + +image="localhost/yampl" + +buildah build -f Containerfile -t $image +if [ $? -eq 0 ] ; then + echoSection "success" +else + echoSection "failure" + exit -2 +fi + +## push container to the registry +section="push image" +echoSection "" + +if [ -z "$CONTAINER_REGISTRY_USERNAME$CONTAINER_REGISTRY_PASSWORD"] ; then + echoSection "skip" +else + gitbranch="git rev-pars --abrev-ref HEAD" + + newimage="containers.openfl.eu/local/yampl:$gitbranch" + podman tag $image $newimage + image=$newimage + podman login --username $CONTAINER_REGISTRY_USERNAME --password $CONTAINER_REGISTRY_PASSWORD containers.openfl.eu + podman push "containers.openfl.eu/local/yampl:$gitbranch" + if [ $? -ne 0 ] ; then + echoSection "failure" + exit -3 + fi +fi + +echoSection "success" + +## update the content of the volumes +section="update data" +echoSection "" +echoSection "success" + +## restart the service +section="restart container" +echoSection "" +podman compose pull && \ +podman compose down && \ +podman compose up -d +if [ $? -ne 0 ] ; then + echoSection "failure" + exit -4 +fi +echoSection "success" diff --git a/main.go b/main.go index d9717c4..35be7b5 100644 --- a/main.go +++ b/main.go @@ -45,7 +45,7 @@ func listRespond(w http.ResponseWriter, r *http.Request) { log.Println(err) return } - + for i, it := range data.Items { it["_index"] = i it["_url"] = fmt.Sprintf("/items/%d", i) -- 2.47.1