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* *DS_Store*
.idea .idea
config.yaml config.yaml
release

View File

@ -1,9 +1,12 @@
variables: variables:
# Docker service related things.
DOCKER_IMAGE: registry.gitlab.pztrn.name/containers/mirror/docker:20.10.8-dind DOCKER_IMAGE: registry.gitlab.pztrn.name/containers/mirror/docker:20.10.8-dind
DOCKER_HOST: tcp://docker:2375 DOCKER_HOST: tcp://docker:2375
DOCKER_TCP_PORT: 2375 DOCKER_TCP_PORT: 2375
DOCKER_TLS_CERTDIR: "" DOCKER_TLS_CERTDIR: ""
LINTTEST_IMAGE: registry.gitlab.pztrn.name/containers/mirror/golangci/golangci-lint:v1.42.1 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_LATEST: ${CI_REGISTRY_IMAGE}:latest
REGISTRY_IMAGE_TAGGED: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG} REGISTRY_IMAGE_TAGGED: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}
@ -23,6 +26,7 @@ lint:
script: script:
- golangci-lint run - golangci-lint run
# Docker images building.
build_master_image: build_master_image:
stage: release stage: release
only: only:
@ -31,6 +35,7 @@ build_master_image:
- docker - docker
image: ${DOCKER_IMAGE} image: ${DOCKER_IMAGE}
script: script:
- scripts/get_version.sh generate
- docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY} - 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 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} - docker push ${REGISTRY_IMAGE_LATEST}
@ -43,6 +48,7 @@ build_tag_image:
- docker - docker
image: ${DOCKER_IMAGE} image: ${DOCKER_IMAGE}
script: script:
- scripts/get_version.sh generate
- docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY} - 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 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} - docker push ${REGISTRY_IMAGE_TAGGED}

View File

@ -4,7 +4,7 @@ WORKDIR /go/src/go.dev.pztrn.name/periodicator
COPY . . COPY . .
ENV CGO_ENABLED=0 ENV CGO_ENABLED=0
RUN go build -o periodicator . RUN apk add make && make build
FROM alpine:latest FROM alpine:latest
LABEL maintainer="Stanislav N. <pztrn@pztrn.name>" 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 task
body body
# Tags (or labels) to attach. # Tags (or labels) to attach. This is a list.
tags: ["test label"] tags: ["test label"]
# Task's starting point. From this timestamp "internal cron" will start to calculate task creation timestamp and other # Task's starting point. From this timestamp "internal cron" will start to calculate task creation timestamp and other
# things. # things.
@ -27,11 +27,10 @@ tasks:
due_in: "24h" due_in: "24h"
- title: "Another Test" - title: "Another Test"
body: | body: "This is a single-line body.<br><br>Markdown might be supported, or not ``¯\_(ツ)_/¯``"
This is a multiline # Another way to specify tags or labels.
task tags:
body - "test label"
tags: ["test label"]
execution_start: "2021-09-01 00:00:00" execution_start: "2021-09-01 00:00:00"
cron: "00 5 * * sat" cron: "00 5 * * sat"
due_in: "24h" due_in: "24h"

View File

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

15
main.go
View File

@ -1,15 +1,28 @@
package main package main
import ( import (
"flag"
"fmt"
"log" "log"
"os"
"go.dev.pztrn.name/periodicator/internal/config" "go.dev.pztrn.name/periodicator/internal/config"
"go.dev.pztrn.name/periodicator/internal/gitlab" "go.dev.pztrn.name/periodicator/internal/gitlab"
"go.dev.pztrn.name/periodicator/internal/tasks" "go.dev.pztrn.name/periodicator/internal/tasks"
) )
var showVersion = flag.Bool("version", false, "Show version information and exit")
func main() { 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() 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