Add build helpers and improve configuration example.

This commit is contained in:
Stanislav Nikitin 2021-09-28 08:35:03 +05:00
parent bad5cc1895
commit 82bd1efe2a
Signed by: pztrn
GPG Key ID: 1E944A0F0568B550
9 changed files with 143 additions and 28 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*DS_Store*
.idea
config.yaml
release

View File

@ -1,9 +1,12 @@
variables:
# Docker service related things.
DOCKER_IMAGE: registry.gitlab.pztrn.name/containers/mirror/docker:20.10.8-dind
DOCKER_HOST: tcp://docker:2375
DOCKER_TCP_PORT: 2375
DOCKER_TLS_CERTDIR: ""
LINTTEST_IMAGE: registry.gitlab.pztrn.name/containers/mirror/golangci/golangci-lint:v1.42.1
CURL_IMAGE: registry.gitlab.pztrn.name/containers/mirror/curlimages/curl:7.79.1
# Docker images names that we will push to registry.
REGISTRY_IMAGE_LATEST: ${CI_REGISTRY_IMAGE}:latest
REGISTRY_IMAGE_TAGGED: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}
@ -23,26 +26,29 @@ lint:
script:
- golangci-lint run
# Docker images building.
build_master_image:
stage: release
only:
- master
tags:
- docker
image: ${DOCKER_IMAGE}
script:
- docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
- docker build --pull -t ${REGISTRY_IMAGE_LATEST} --build-arg CI_COMMIT_TAG=${CI_COMMIT_TAG} --build-arg CI_COMMIT_REF_NAME=${CI_COMMIT_REF_NAME} --build-arg CI_COMMIT_SHA=${CI_COMMIT_SHA} --build-arg CI_PROJECT_NAME=${CI_PROJECT_NAME} .
- docker push ${REGISTRY_IMAGE_LATEST}
stage: release
only:
- master
tags:
- docker
image: ${DOCKER_IMAGE}
script:
- scripts/get_version.sh generate
- docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
- docker build --pull -t ${REGISTRY_IMAGE_LATEST} --build-arg CI_COMMIT_TAG=${CI_COMMIT_TAG} --build-arg CI_COMMIT_REF_NAME=${CI_COMMIT_REF_NAME} --build-arg CI_COMMIT_SHA=${CI_COMMIT_SHA} --build-arg CI_PROJECT_NAME=${CI_PROJECT_NAME} .
- docker push ${REGISTRY_IMAGE_LATEST}
build_tag_image:
stage: release
only:
- tags
tags:
- docker
image: ${DOCKER_IMAGE}
script:
- docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
- docker build -t ${REGISTRY_IMAGE_TAGGED} --build-arg CI_COMMIT_TAG=${CI_COMMIT_TAG} --build-arg CI_COMMIT_REF_NAME=${CI_COMMIT_REF_NAME} --build-arg CI_COMMIT_SHA=${CI_COMMIT_SHA} --build-arg CI_PROJECT_NAME=${CI_PROJECT_NAME} .
- docker push ${REGISTRY_IMAGE_TAGGED}
stage: release
only:
- tags
tags:
- docker
image: ${DOCKER_IMAGE}
script:
- scripts/get_version.sh generate
- docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
- docker build -t ${REGISTRY_IMAGE_TAGGED} --build-arg CI_COMMIT_TAG=${CI_COMMIT_TAG} --build-arg CI_COMMIT_REF_NAME=${CI_COMMIT_REF_NAME} --build-arg CI_COMMIT_SHA=${CI_COMMIT_SHA} --build-arg CI_PROJECT_NAME=${CI_PROJECT_NAME} .
- docker push ${REGISTRY_IMAGE_TAGGED}

View File

@ -4,7 +4,7 @@ WORKDIR /go/src/go.dev.pztrn.name/periodicator
COPY . .
ENV CGO_ENABLED=0
RUN go build -o periodicator .
RUN apk add make && make build
FROM alpine:latest
LABEL maintainer="Stanislav N. <pztrn@pztrn.name>"

18
Makefile Normal file
View File

@ -0,0 +1,18 @@
LINKER_FLAGS = "-X 'go.dev.pztrn.name/periodicator/internal/config.Version=$(shell scripts/get_version.sh)'"
CONFIG ?= "./config.example.yaml"
build:
go build -ldflags $(LINKER_FLAGS) -o periodicator .
generate-version:
scripts/get_version.sh generate
run:
go build -ldflags $(LINKER_FLAGS) -o periodicator .
GPT_CONFIG=$(CONFIG) ./periodicator
rm periodicator
run-version:
go build -ldflags $(LINKER_FLAGS) -o periodicator .
GPT_CONFIG=$(CONFIG) ./periodicator -version
rm periodicator

View File

@ -15,7 +15,7 @@ tasks:
task
body
# Tags (or labels) to attach.
# Tags (or labels) to attach. This is a list.
tags: ["test label"]
# Task's starting point. From this timestamp "internal cron" will start to calculate task creation timestamp and other
# things.
@ -27,11 +27,10 @@ tasks:
due_in: "24h"
- title: "Another Test"
body: |
This is a multiline
task
body
tags: ["test label"]
body: "This is a single-line body.<br><br>Markdown might be supported, or not ``¯\_(ツ)_/¯``"
# Another way to specify tags or labels.
tags:
- "test label"
execution_start: "2021-09-01 00:00:00"
cron: "00 5 * * sat"
due_in: "24h"

View File

@ -10,6 +10,9 @@ import (
"gopkg.in/yaml.v2"
)
// Version automagically fills when building or running using Makefile.
var Version = "unknown"
// Config is a global configuration structure.
type Config struct {
Gitlab gitlab.Config `yaml:"gitlab"`

15
main.go
View File

@ -1,15 +1,28 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"go.dev.pztrn.name/periodicator/internal/config"
"go.dev.pztrn.name/periodicator/internal/gitlab"
"go.dev.pztrn.name/periodicator/internal/tasks"
)
var showVersion = flag.Bool("version", false, "Show version information and exit")
func main() {
log.Println("Starting periodic tasks creator...")
flag.Parse()
if *showVersion {
// nolint:forbidigo
fmt.Println(config.Version)
os.Exit(0)
}
log.Println("Starting periodic tasks creator, version " + config.Version + "...")
cfg := config.Parse()

30
scripts/build.sh Executable file
View File

@ -0,0 +1,30 @@
#!/usr/bin/env bash
# This script is using for building release binaries.
OSES=("darwin" "linux" "windows")
ARCHES=("amd64" "arm64")
GO=$(which go)
if [ "${GO}" == "" ]; then
echo "Golang is not installed, cannot continue"
fi
if [ ! -d "release" ]; then
mkdir -p release
fi
for OS in "${OSES[@]}"; do
for ARCH in "${ARCHES[@]}"; do
echo "Building for ${OS} ${ARCH}..."
CGO_ENABLED=0 GOOS=${OS} GOARCH=${ARCH} go build -o release/periodicator-${OS}-${ARCH} .
done
done
for file in release/*windows*; do
mv ${file} ${file}.exe
done
for file in release/*; do
zip -m ${file}.zip ${file}
done

45
scripts/get_version.sh Executable file
View File

@ -0,0 +1,45 @@
#!/usr/bin/env bash
# This is a branch helper script that can be used to determine what we're using
# or deploying.
# Defaulting to version 0.1.0 if we can't get version in any other way.
# We might already have version file (e.g. this script was launched with "generate"
# parameter when building a Docker image). In that case - just output it.
if [ -f "version" ]; then
cat version
exit 0
fi
DELIMITER=" "
VERSION="0.1.0"
# Try to get last available version tag. This tag will replace version if found.
TAG=$(git tag | tail -n 1)
if [ "${TAG}" != "" ]; then
VERSION="${TAG}"
fi
# We should figure out should we add branch or not. For that we should take
# last commit hash and last tag hash for comparison. We should add branch
# name only if hashes aren't same.
LAST_COMMIT_HASH=$(git log --format="%h" 2>/dev/null | head -n 1)
LAST_TAG_COMMIT_HASH=$(git log "${TAG}" --format="%h" 2>/dev/null | head -n 1)
if [ "${LAST_COMMIT_HASH}" != "${LAST_TAG_COMMIT_HASH}" ]; then
VERSION+="${DELIMITER}$(git branch | grep "*" | awk {' print $2 '})"
fi
# Add commit hash.
VERSION+="${DELIMITER}${LAST_COMMIT_HASH}"
# Add build date.
VERSION+="${DELIMITER}$(date +'%Y%m%d-%H%M')"
case $1 in
generate)
echo "${VERSION}" > version
;;
*)
echo "${VERSION}"
;;
esac