From 82bd1efe2ac4d7b24602d3fc310d921f1e8d5665 Mon Sep 17 00:00:00 2001 From: "Stanislav N. aka pztrn" Date: Tue, 28 Sep 2021 08:35:03 +0500 Subject: [PATCH] Add build helpers and improve configuration example. --- .gitignore | 1 + .gitlab-ci.yml | 46 ++++++++++++++++++++++----------------- Dockerfile | 2 +- Makefile | 18 +++++++++++++++ config.example.yaml | 11 +++++----- internal/config/config.go | 3 +++ main.go | 15 ++++++++++++- scripts/build.sh | 30 +++++++++++++++++++++++++ scripts/get_version.sh | 45 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 143 insertions(+), 28 deletions(-) create mode 100644 Makefile create mode 100755 scripts/build.sh create mode 100755 scripts/get_version.sh diff --git a/.gitignore b/.gitignore index 69dff01..8258fdd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *DS_Store* .idea config.yaml +release diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a4e9aa3..44e86f1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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} diff --git a/Dockerfile b/Dockerfile index 69c0818..42289ff 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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. " diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..76e2ab3 --- /dev/null +++ b/Makefile @@ -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 diff --git a/config.example.yaml b/config.example.yaml index db194c0..ba62fbd 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -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.

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" diff --git a/internal/config/config.go b/internal/config/config.go index 32f9821..67b035a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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"` diff --git a/main.go b/main.go index 980e04a..da1a48f 100644 --- a/main.go +++ b/main.go @@ -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() diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..87facd7 --- /dev/null +++ b/scripts/build.sh @@ -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 diff --git a/scripts/get_version.sh b/scripts/get_version.sh new file mode 100755 index 0000000..1540064 --- /dev/null +++ b/scripts/get_version.sh @@ -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